I am trying to load recyclerview with data from SQLite database contents. But it is not working
This is what i have done
Activity class
public class ViewMembership extends AppCompatActivity {
private RecyclerView recyclerView;
private membership_view_recycler_adapter m;
private DBHelper dbHelper;
public List<membership_recycler_model> membershipList=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_membership);
dbHelper=new DBHelper(this);
////////////////////////////////////////////////////////////////////////////
recyclerView=(RecyclerView) findViewById(R.id.View_Membership_Recycler);
m=new membership_view_recycler_adapter(membershipList);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(m);
prepare_membership();
//////////////////////////////////////////////////////////////////////////
}
public void prepare_membership()
{
System.out.println("#######################Prepare_membership called");
membershipList=dbHelper.membership_for_recycler_view();
m.notifyDataSetChanged();
}}
Adapter class
public class membership_view_recycler_adapter extends RecyclerView.Adapter<membership_view_recycler_adapter.MyViewHolder> {
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name,region;
public MyViewHolder(View itemView) {
super(itemView);
name=(TextView) itemView.findViewById(R.id.membership_name_textview);
region=(TextView) itemView.findViewById(R.id.membership_region_textview);
}
}
public membership_view_recycler_adapter(List<membership_recycler_model> membership_list) {
this.membership_list = membership_list;
}
private List<membership_recycler_model> membership_list;
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.membership_view_row,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
membership_recycler_model membership=membership_list.get(position);
holder.name.setText(membership.getMembership_name());
holder.region.setText(membership.getRegion());
}
#Override
public int getItemCount() {
return membership_list.size();
}}
I've got no compilation errors, application is not throwing any exceptions. dbHelper.membership_for_recycler_view() is returning appropriate values. I am kinda new in Android. I did this with the help of a tutorial. What have I done wrong?
I think the problem here is you are calling prepare_membership()
function later and passing data to your adapter before with an empty list. Try to make following changes :
Add a new method in your adapter -
public void refreshData(List<membership_recycler_model> membership_list){
this.membership_list = membership_list;
notifyDataSetChanged();
}
Inside your prepare_membership() function call this method after fetching the list from db i.e m.refreshData(membershipList)
`
Related
I have a recyclerview.I am trying to load images horizontally int o it but i keeping getting the foll error
You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class
I tried searching a lot but couldn't get an answer that would help me.Following is my code
public class Images {
public String[] imageUrl;
public String[] getImageUrl() {
return imageUrl;
}
public void setImageUrl(String[] imageUrl) {
this.imageUrl = imageUrl;
}
public Images(String[] imageUrl) {
this.imageUrl = imageUrl;
}
}
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private Context context;
private List<Images> imagesList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView recyclerimageView;
public MyViewHolder(View view) {
super(view);
recyclerimageView=(ImageView)view.findViewById(R.id.recyclerlistitemimageview);
}
}
public RecyclerAdapter(Context context,List<Images> imagesList){
this.context=context;
this.imagesList=imagesList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerlistitem, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// Glide.with(context).load(myarr).into(holder.recyclerimageView);
Images images=imagesList.get(position);
Glide.with(context).load(images.getImageUrl()).into(holder.recyclerimageView);
}
#Override
public int getItemCount() {
return imagesList.size();
// return myarr.length;
}
}
public class Arrayclass {
public static String activa3gimage3[]={"https://firebasestorage.googleapis.com/v0/b/venetian-honda-179411.appspot.....",
"https://firebasestorage.googleapis.......",
"https://firebasestorage.googleapis....",
"https://firebasestorage.googleapis......"};
}
I have uploaded my image to server and taking the uri of images in activa3gimage3 array
public class ModelLineUpInnerActivity extends AppCompatActivity {
private List<Images> imagesList = new ArrayList<>();
private RecyclerView recyclerView;
private RecyclerAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_model_line_up_inner);
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
Images images=new Images(Arrayclass.activa3gimage3);
imagesList.add(images);
mAdapter = new RecyclerAdapter(ModelLineUpInnerActivity.this,imagesList);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
}
I know that the error is beacause of Glide and i am trying to load array of images into it,but don't know how to resolve it.Any help will be appreciated.
You need to use single url to download and load image into single image using array with index so use
Glide.with(context).load(images.getImageUrl()[position]).into(holder.recyclerimageView);
// ^^^^^^^^^^
Update :
Your list is containing only single item so you need to modify list with all the items in it
Images images=new Images(Arrayclass.activa3gimage3);
imagesList.add(images); // single item so single view
Use List<String> and eliminate use of Images because your adapter only needs String url to load images.
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private Context context;
private List<String> imagesList;
// use String list
public RecyclerAdapter(Context context,List<String> imagesList){
this.context=context;
this.imagesList=imagesList;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Glide.with(context).load(imagesList.get(position)).into(holder.recyclerimageView);
}
#Override
public int getItemCount() {
return imagesList.size();
}
// ... code
and setup adapter like
private List<String> imagesList = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_model_line_up_inner);
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
imagesList.addAll(new ArrayList<>(Arrays.asList(Arrayclass.activa3gimage3)));
mAdapter = new RecyclerAdapter(ModelLineUpInnerActivity.this,imagesList);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
images.getImageUrl() is array, and you need a String with url of your image
Use List<String> instead of List<Images> (also update it in your RecyclerAdapter & ModelLineUpInnerActivity)
private List<String> imagesList = new ArrayList<>();
Add your urls in this list, like this:
imagesList.addAll(Arrays.asList(Arrayclass.activa3gimage3));
After that you can load your image:
String image = imagesList.get(position);
Glide.with(context).load(image).into(holder.recyclerimageView);
I'm trying to build a simple To Do List app using a RecyclerView with a custom Adapter. I've build a model data class to get and set my todos. My problem is that each time I click the button to add my item, nothing happens. I did call notifyDataSetChanged() method but it still doesn't work.
This is a practice app that I'm building to learn RecyclerViews and custom adapters so I'd appreciate it if someone could explain why it doesn't work and perhaps explain to me how I can fix it.
This is my code where I'm retrieving the user input from the EditText field and placing it in my data:
public class MainActivity extends AppCompatActivity {
private ToDoData toDoData;
private List<ToDoData> toDoList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ToDoAdapter toDoAdapter = new ToDoAdapter(toDoList, this);
toDoData = new ToDoData(this);
final EditText toDoInput = (EditText)findViewById(R.id.add_todo);
Button toDoAdd = (Button)findViewById(R.id.add_item);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
RecyclerView toDoDisplay = (RecyclerView) findViewById(R.id.toDoDisplayRecyclerView);
toDoDisplay.setAdapter(toDoAdapter);
toDoDisplay.setHasFixedSize(true);
toDoDisplay.setLayoutManager(layoutManager);
toDoAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toDoData.setToDo(toDoInput.getText().toString());
toDoAdapter.notifyDataSetChanged();
}
});
}
}
This is my model class with getters and setters. The list-item has only 1 field so its rather small:
public class ToDoData {
private String toDoString;
public ToDoData(String todoString){
this.toDoString = todoString;
}
public ToDoData(Context context){}
public String getToDo() {
return toDoString;
}
public void setToDo(String toDoString) {
this.toDoString = toDoString;
}
}
And this is my custom adapter. I followed through a tutorial while building this but I do however think that my problem stems from the adapter:
public class ToDoAdapter extends RecyclerView.Adapter<ToDoAdapter.ViewHolder> {
private List<ToDoData> toDoList;
private Context context;
public ToDoAdapter(List<ToDoData> todoList, Context context) {
this.toDoList = todoList;
this.context = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView todoView;
public ViewHolder(View itemView) {
super(itemView);
todoView = (TextView) itemView.findViewById(R.id.to_do_display);
}
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_do_list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
ToDoData todo = toDoList.get(position);
holder.todoView.setText(todo.getToDo());
}
#Override
public int getItemCount() {
return (toDoList == null) ? 0 : toDoList.size();
}
}
Add one setter method in your adapter class for setting the collection and then invoke notifyDataSetChanged.
Declare the ToDoAdapter as class member and then invoke the setter function on Button click as follows,
adapter.setTodoList(/* pass collection */);
adapter.notifyDataSetchanged();
I have an Android Activity that have a RecycleView. I want to add an event listener, so I have build this code:
public class ResultActivity extends AppCompatActivity {
private List<Result> lista;
private RecyclerView recyclerView;
private ResultsAdapter pAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.results_activity);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
//recupero la lista delle medication
ResultDAO manager = new ResultDAO(this);
lista=manager.getResults();
pAdapter = new ResultsAdapter(lista, new ResultsAdapter.OnItemClickListener() {
#Override
public void onItemClick(Result item) {
try{
//recupero i dati della lista in pagina
final Dialog dialog = new Dialog(ResultActivity.this);
//setting custom layout to dialog
dialog.setContentView(R.layout.result_modal);
dialog.setTitle("Parametri");
}catch(Exception e){
Log.e("","");
}
}
});
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(pAdapter);
}
}
This is my custom adapter:
public class ResultsAdapter extends RecyclerView.Adapter<ResultsAdapter.MyViewHolder> {
private List<Result> list;
private final OnItemClickListener listener;
public interface OnItemClickListener {
void onItemClick(Result item);
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView startDate, endDate,examination;
Result result;
public MyViewHolder(View view) {
super(view);
startDate = (TextView) view.findViewById(R.id.startDate);
endDate = (TextView) view.findViewById(R.id.endDate);
examination = (TextView) view.findViewById(R.id.examination);
}
}
public ResultsAdapter(List<Result> list,OnItemClickListener listener)
{
this.listener=listener;
this.list = list;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.results_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Result result = list.get(position);
holder.startDate.setText(result.getDateStart()!=null ? result.getDateStart() : "");
holder.endDate.setText(result.getDateEnd()!=null ? result.getDateEnd() : "");
holder.examination.setText(result.getInfo().getDisplayName());
}
#Override
public int getItemCount() {
return list.size();
}
}
Now if I try to start my application and I try to click on one or more item, the method OnClick, does not start.
How can I fixed it?
Read this answer for better understanding
Use below code in Adapter class :-
holher.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// your code
}
});
Check this blog post.
http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/
It allows you to add 'on-click-listeners' in a much simpler, reusable way.
Example:
ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
#Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
// Do your thing
}
});
I having a recyclerview with GridLayoutManager, it populating the grid properly but not updating the data.
Below is my code,
public class AppsGridViewAdapter extends RecyclerView.Adapter<AppsGridViewAdapter.GridViewHolder>{
private ArrayList<App> appArrayList;
private Context context;
public AppsGridViewAdapter(Context context,ArrayList<App> appArrayList){
this.appArrayList = appArrayList;
this.context = context;
}
public static class GridViewHolder extends RecyclerView.ViewHolder{
CompoundAppButton compoundAppButton;
public GridViewHolder(CompoundAppButton itemView){
super(itemView);
compoundAppButton = (CompoundAppButton)itemView;
}
}
#Override
public GridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CompoundAppButton appButton = new CompoundAppButton(context,null);
return new GridViewHolder(appButton);
}
#Override
public void onBindViewHolder(GridViewHolder holder, int position) {
App app = appArrayList.get(position);
holder.compoundAppButton.updateButtonConfig(app);
}
#Override
public int getItemCount() {
return appArrayList.size();
}
}
Having valid values in the list, but not updating. Where CustomAppButton is a customview. Anyone can help me why it doesnt update.
UPDATE:
I calling recyclerview from my activity, below is the specific code
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.addItemDecoration(new MarginDecoration(this));
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
AppsGridViewAdapter adapter = new AppsGridViewAdapter(this,appList);
recyclerView.setAdapter(adapter);
The activity contains only recyclerview.
updateButtonConfig is a function in CustomAppButton class, this is where view is updated. getting data to this function, have checked the log.
public void updateButtonConfig(App app){
Picasso.with(context)
.load(app.getAppIcon())
.placeholder(R.drawable.app_icon)
.into(appIcon);
appName.setText(app.getAppName());
category.setText(app.getAppCategory());
price.setText(app.getAppPrice());
appPackage = app.getAppPackage();
}
Androd SQLite display data read from a CursorAdapter class and a ListView in ViewHolder instead.
I wanted a CheckBox for each line, which can put specific updates the database record.
The thing does not work well because when I activate the first line CheckBox you change that appear on the screen last line record.
long _id = todo.getID(); //
checkBoxFavorite.setTag(_id);
checkBoxFavorite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
//Update database row
}
else if (!isChecked)
{
//Update database row
}
}
});
Try this.
public class MainActivity extends AppCompatActivity{
private RecyclerView recyclerView;
private ArrayList<String> list;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
TestAdapter adapter = new TestAdapter();
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
}
class TestViewHolder extends RecyclerView.ViewHolder
{
CheckBox checkBox;
public TestViewHolder(View view)
{
super(view);
checkBox=(CheckBox)view.findViewById(R.id.checkbox);
}
public void addDetail(int positon)
{
checkBox.setText(list.get(positon));
}
}
class TestAdapter extends RecyclerView.Adapter<TestViewHolder>
{
public TestAdapter()
{
}
#Override
public TestViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.recycler_item,null);
return new TestViewHolder(view);
}
#Override
public void onBindViewHolder(TestViewHolder viewHolder, int i)
{
viewHolder.addDetail(i);
}
#Override
public int getItemCount() {
return list.size();
}
}}
you have to use the setTag() and getTag(), on the view you can check out from this blog post Link
This is not a stright forward answer but it will get started.