问题
In my application I am getting data from sqlite database and showing them using RecyclerView. This is my adapter :-
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.Holder> {
private List<SentData> dataList = Collections.emptyList();
private LayoutInflater inflater;
public RecyclerViewAdapter(Context context, List<SentData> dataList) {
Log.d("array size", dataList.size()+"");
this.dataList = dataList;
inflater = LayoutInflater.from(context);
}
@Override
public RecyclerViewAdapter.Holder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.custom_list_row, viewGroup, false);
return new Holder(view);
}
@Override
public void onBindViewHolder(RecyclerViewAdapter.Holder viewHolder, int position) {
SentData current = dataList.get(position);
Log.d("Required data is", current.circleName + current.retailName);
viewHolder.circleName.setText(current.circleName);
viewHolder.retailName.setText(current.retailName);
viewHolder.retailAddress.setText(current.retailAddress);
viewHolder.captureOn.setText(current.captureDate);
}
@Override
public int getItemCount() {
return 0;
}
class Holder extends RecyclerView.ViewHolder {
private TextView retailName, retailAddress, circleName, captureOn;
public Holder(View itemView) {
super(itemView);
retailName = (TextView) itemView.findViewById(R.id.tvRetailerName);
retailAddress = (TextView) itemView.findViewById(R.id.tvRetailAddress);
circleName = (TextView) itemView.findViewById(R.id.tvCircleName);
captureOn = (TextView) itemView.findViewById(R.id.tvCaptureOn);
}
}
}
SentData is a class
public class SentData {
String circleName, retailName, retailAddress, captureDate, syncId, detailId;
}
I am not getting any error but the list is not showing. I am trying check with debugger, I found that the onBindViewHolder() method is not running.
In the RecyclerViewAdapter constructor I got the dataList size is 2 that means data is successfully retrieved from sqlite database.
Now I cant find out what is the problem and why its not working please help me. Thanks in advance :)
回答1:
Collections.emptyList() returns an empty immutable list, you should use new ArrayList<>() instead.
Then after you add/remove data from the list must call notifyDataSetChanged() for the changes to be seen.
Also getItemCount() should return dataList.size()
回答2:
You are returning 0 on getItemCount()
. Make sure you return the size of
list that backs the RecyclerView
回答3:
I also had the same problem..Your code is completely fine but it is not displaying any list because you are returning 0 in method getItemCount().Just return dataList.size() in your case and it wil work.
来源:https://stackoverflow.com/questions/28857213/recyclerview-is-not-showing-anything