[英]How to pass ArrayList size from onBindViewHolder to getItemCount() method in RecyclerView?
I have this array inside onBindViewHolder method like that.我在这样的 onBindViewHolder 方法中有这个数组。
checklistHelper = dbHelper.getChecklists(id);
String status = checklistHelper.getStatus();
String[] statusSplit = status.split("\n");
ArrayList<String> newStatusList = new ArrayList<>(Arrays.asList(statusSplit));
newStatusList.remove(position);
notifyItemRemoved(position);
int size = newStatusList.size();
I want to pass size to below method我想将大小传递给下面的方法
@Override
public int getItemCount() {
return size;
By doing that getItemCount won't get any values in it, this means I don't see any item.通过这样做 getItemCount 不会得到任何值,这意味着我看不到任何项目。 If I want to put that code in the Adapter constructor the items get showed, but after deleting a row, the row gets recreated again.
如果我想将该代码放入 Adapter 构造函数中,则会显示项目,但在删除一行后,该行将再次重新创建。 Because I can't put
因为放不下
newStatusList.remove(position);
notifyItemRemoved(position);
inside adapter constructor, since I can't access ViewHolder position inside adapter constructor.在适配器构造函数内,因为我无法访问适配器构造函数内的 ViewHolder 位置。
Any help is appreciated.任何帮助表示赞赏。
You can declare int size=0;
您可以声明
int size=0;
globally inside Adapter.全局内部适配器。 also, create this array list when the constructor gets called.
另外,在调用构造函数时创建此数组列表。 so you can have the list prepared before
getItemCount()
gets called.因此您可以在调用
getItemCount()
之前准备好列表。
Adaper(){
ArrayList<Integer> integers = new ArrayList<>();
integers.add(2);
integers.add(8);
integers.add(2);
integers.add(61);
integers.add(3);
integers.add(75);
size = integers.size();
}
checklistHelper = dbHelper.getChecklists(id);
String status = checklistHelper.getStatus();
String[] statusSplit = status.split("\n");
ArrayList<String> newStatusList = new ArrayList<>(Arrays.asList(statusSplit));
newStatusList.remove(position);
notifyItemRemoved(position);
int size = newStatusList.size();
@Override public int getItemCount() { @Override public int getItemCount() {
return newStatusList.size();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.