简体   繁体   English

如何将 ArrayList 大小从 onBindViewHolder 传递给 RecyclerView 中的 getItemCount() 方法?

[英]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.

相关问题 在 RecyclerView Android 的 OnBindViewHolder 方法中从列表中删除项目 - Removing item from list in the OnBindViewHolder method for RecyclerView Android 如何从RecyclerView.Adapter的OnBindViewHolder中的另一个列表中提取列表 - how to extract a list from another list in OnBindViewHolder in RecyclerView.Adapter 如何将arraylist从onClick传递给方法 - How to pass arraylist into a method from onClick Recyclerview 不调用任何适配器方法 :onCreateViewHolder,onBindViewHolder, - Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder, RecyclerView onBindViewHolder indexOutOfBounds尝试绘制ArrayList中的元素 - RecyclerView onBindViewHolder indexOutOfBounds trying to paint elements inside an ArrayList 从onBindViewHolder中的RecyclerView更改项目 - change item from RecyclerView out of onBindViewHolder 如何将数据从Adapter内部的onBindViewHolder传递到Tab - How to pass data from onBindViewHolder inside Adapter to Tab RecyclerView 不调用 getItemCount - RecyclerView not calling getItemCount 如何在android RecyclerView中使用带有多个项目的onBindViewHolder - how to use onBindViewHolder with multiple items in android RecyclerView 我们可以从RecyclerView的getItemCount()中获得两种返回类型吗? - Can we get two return types from getItemCount() in RecyclerView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM