Android 在回收器视图对象中保存项目值

Android 在回收器视图对象中保存项目值,android,android-recyclerview,increment,Android,Android Recyclerview,Increment,我在“回收器”视图中工作,其中有递增和递减按钮。我的条件是最大不能加10,最小不能加0。当我在recycler视图中单击第一行项目的增量按钮时,它将添加1,但当我在recycler视图中单击第二行项目时,它也将影响列表的第一项。假设item1变为1加 当我执行任何递增和递减操作时,它不应该影响RecyclerView中的其他行项目 int counter = 0; @Override public void onBindViewHolder(final RecyclerView.ViewHol

我在“回收器”视图中工作,其中有递增和递减按钮。我的条件是最大不能加10,最小不能加0。当我在recycler视图中单击第一行项目的增量按钮时,它将添加1,但当我在recycler视图中单击第二行项目时,它也将影响列表的第一项。假设item1变为1加

当我执行任何递增和递减操作时,它不应该影响RecyclerView中的其他行项目

int counter = 0;

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder  holder,final int position) {
    final ParseObject parseObject = arrayList.get(position);
    boolean like = true, dislike = true;
    if (holder instanceof MemberViewHolder) {
        if (arrayList.size() > 0) {
            ((MemberViewHolder) holder).txtIncrement.setOnClickListener(new TextView.OnClickListener() {
                @Override
                public void onClick(View view) {
                            counter += 1;
                            ((MemberViewHolder) holder).txtCounter.setText(counter+"");
                    }
            });
            ((MemberViewHolder) holder).txtDecrement.setOnClickListener(new TextView.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if(counter == 0){

                    }else{
                        counter -= 1;
                        ((MemberViewHolder) holder).txtCounter.setText(counter+"");
                    }
                }
         });
     }
 }
当从第1项开始递减时,它也递减第2行中的值。我想保留每个项目的值


我建议使用一张地图

私有映射countOfItems=新HashMap

键是项目在适配器中的位置。该值是该位置的项目计数

因此,在onBindViewHolder中,您必须进行一些更改:

对于递增:

int currentCount = countOfItems.get(position);
currentCount++;
countOfItems.put(position, currentCount);
int currentCount = countOfItems.get(position);
if(currentCount > 0) {
  currentCount--;
  countOfItems.put(position, currentCount);
}
用于递减:

int currentCount = countOfItems.get(position);
currentCount++;
countOfItems.put(position, currentCount);
int currentCount = countOfItems.get(position);
if(currentCount > 0) {
  currentCount--;
  countOfItems.put(position, currentCount);
}
为适配器构造函数中的每个项初始化映射,使其计数为0

for(int i = 0; i < arrayList.size(); i++) {
  countOfItems.put(i, 0)
}
我是这样做的:

首先创建int[]类型的成员变量

这里我给了数组一个默认大小10

重要提示:如果您试图将项目增加到索引10以上,这将使您的应用程序崩溃。为了避免在适配器构造函数中填充arrayList时需要为其指定arrayList的大小

现在在onBindViewHolder中:


希望这有帮助

发布onBindViewHolder代码维护一个计数器数组,大小等于数据集大小,更新递减和递增的索引计数,为UI引用该数组updation@RahulChaudhary谢谢你的回复。我正在更新我的完整onBindViewHolder。请检查上面的内容。@VinaySasalatti感谢您的回复。你能根据你的解释给我一个代码片段吗。我已经在上面编辑了我的onBindViewHolder,您也可以查看。您是否有一个用于分别处理每个项目的recyclerview\u对象列表谢谢您的回答。我已经运行了这段代码,它是在counter=counterMap.getposition上给出null异常;在调试器上我检查了它,它给出了position=0和counterMap=0,在counterMap.getposition上有一个空指针异常。请帮我解决这个问题。你是在适配器构造函数中传递arrayList还是在其他地方设置arrayList?这是我的数字默认计数器。我的意思是我在做0到10的加法和减法运算。我没有从服务器获取这个0到10的值。@beep我只想在按下“添加/减去”按钮时显示0到10的值。该值应为每个项目,不应影响其他行项目值。谢谢您的回答。你的解决方案对我非常有效。