RecyclerView没有显示任何内容 [英] RecyclerView is not showing anything
问题描述
在我的应用程序中,我正在从sqlite数据库中获取数据,并使用RecyclerView显示它们. 这是我的适配器:-
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是一个类
SentData is a class
public class SentData {
String circleName, retailName, retailAddress, captureDate, syncId, detailId;
}
我没有收到任何错误,但列表未显示.我正在尝试与调试器进行检查,发现onBindViewHolder()方法未运行.
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.
在RecyclerViewAdapter构造函数中,我得到的dataList大小为2,这意味着已成功从sqlite数据库检索数据.
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 :)
推荐答案
Collections.emptyList()返回空的不可变列表,您应该使用 new ArrayList< ;>().
然后,从列表中添加/删除数据后,必须调用 notifyDataSetChanged()才能看到更改.
同样, getItemCount()应该返回 dataList.size()
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()
这篇关于RecyclerView没有显示任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!