Paging library is not sending any data to my PagedListAdapter
Asked Answered
D

4

9

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);
...
Diversion answered 22/2, 2018 at 12:40 Comment(1)
@pskink the list is successfully passed to the PagedListAdapterHelper and getItemCount() returns the right value.Rhizocarpous
D
28

Problem found: I forgot to set layout manager!

Diversion answered 5/3, 2018 at 20:7 Comment(1)
Same here :) Thanks ...Area
T
16

I had the same problem but I accidentally added-

RecyclerView.setHasFixedSize(true);

After removing this, It worked.

Tjirebon answered 4/7, 2018 at 8:38 Comment(2)
You're a Life saver!Rochelrochell
Spent almost one month to understand that, thank you is finally working erasing this lineHarvester
R
4

Same issue I had and I just removed RecyclerView.setHasFixedSize(true) then it started working fine.

Riddance answered 29/6, 2019 at 9:18 Comment(0)
G
2

I had to set the adapter. recyclerView.setAdapter(adapter);

Grip answered 22/10, 2019 at 17:56 Comment(1)
It's there in the code. Good point anyway, someone might forgot.Rhizocarpous

© 2022 - 2024 — McMap. All rights reserved.