I have an Android To-do list app with SQLite database accessed using Room. Then I wanted to make a list of available lists in navigation drawer. So I used Paging Library. Now the RecyclerView is empty and I don't know why. BTW: none of my lists work. I am a beginner, so I stick to the official tutorial. I've already found out, that they are wrong and that ViewModel
needs an empty constructor. Now the situation looks like this: the method, that sets up the adapter is called successfully (8), ViewModel
is created (7) Adapter
is created (1), the setList()
method is called (9), but no Holder
is being created (4), no onCreateViewHolder
or onBindViewHolder
are called (2,3), no DiffCalback
methods are called (5,6) and just an empty Recycler view is displayed. The PagedList<SQLList>
contains right information, but no information is being passed to the adapter. Has anybody any idea why?
//Sorry for my English.
Adapter class:
//this is my Adapter. SQLList is my object (Equivalent to the User object in
the tutorial)
public class ListsAdapter extends PagedListAdapter<SQLList,ListsAdapter.Holder> {
private Context context;
public ListsAdapter(Context context) {
//1
super(DIFF_CALLBACK);
this.context = context;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
//2
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_item,null);
return new Holder(view);
}
@Override
public void onBindViewHolder(Holder holder, int position) {
//3
SQLList item = getItem(position);
if (item == null){
holder.clear();
}else {
holder.bindTo(item);
}
}
public static class Holder extends RecyclerView.ViewHolder {
public TextView textView;
public int id;
public TextView idView;
public Holder(View itemView) {
//4
super(itemView);
textView = itemView.findViewById(R.id.textView);
idView = itemView.findViewById(R.id.idTextView);
}
public void bindTo(SQLList list) {
textView.setText(list.getName());
id = list.getId();
idView.setText(Integer.toString(list.getId()));
}
public void clear() {
textView.setText("");
id = 0;
idView.setText("0");
}
}
private static final DiffCallback<SQLList> DIFF_CALLBACK = new DiffCallback<SQLList>() {
@Override
public boolean areItemsTheSame(@NonNull SQLList oldItem, @NonNull SQLList newItem) {
//5
return oldItem.getId() == newItem.getId();
}
@Override
public boolean areContentsTheSame(@NonNull SQLList oldItem, @NonNull SQLList newItem) {
//6
return oldItem.getName().equals(newItem.getName());
}
};
}
View Model:
public static class ListViewModel extends ViewModel {
public final LiveData<PagedList<SQLList>> listsList;
public ListViewModel(){
//7
listsList = new LivePagedListBuilder<>(DMO.getDao().loadListFactory(),20).build();
}
}
Part of Fragment the RecyclerView
is placed in:
...
//8
ListViewModel listViewModel = ViewModelProviders.of(this).get(ListViewModel.class);
adapter = new ListsAdapter(context);
listViewModel.listsList.observe(this, new Observer<PagedList<SQLList>>() {
@Override
public void onChanged(@Nullable PagedList<SQLList> sqlLists) {
//9
adapter.setList(sqlLists);
}
});
recyclerView.setAdapter(adapter);
...
Problem found: I forgot to set layout manager!
I had the same problem but I accidentally added-
RecyclerView.setHasFixedSize(true);
After removing this, It worked.
Same issue I had and I just removed RecyclerView.setHasFixedSize(true) then it started working fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With