简体   繁体   English

使用EditText在RecyclerView中编辑重复项

[英]Editing duplicated item in RecyclerView with EditText

I have a list of elements that is editable: I can add/delete new elements to the list. 我有一个可编辑的元素列表:我可以在列表中添加/删除新元素。 Furthermore I can duplicate each Element - duplicated elements are appended to the end of the list. 此外,我可以复制每个元素-重复的元素将附加到列表的末尾。 Each element is displayed with a corresponding EditText where users can input quantity of the given element. 每个元素都显示有一个对应的EditText,用户可以在其中输入给定元素的数量。 The Problem: After duplicating an Element E1, editing the quantity of E1 also changes quantity of E2. 问题:复制元素E1后,编辑E1的数量也会更改E2的数量。

Every ListItem looks like this: TextView(ElementTitle) / EditText(ElementQuantity) Everything works flawlessly on lists of many elements - until I use my "duplicate" function. 每个ListItem看起来都像这样:TextView(ElementTitle)/ EditText(ElementQuantity)在许多元素的列表上,所有东西都可以正常使用-直到我使用“重复”功能。 I assume that the problem has something to do with the Recyclerview reusing the EditTextListeners. 我认为问题与Recyclerview重用EditTextListeners有关。 I am assigning these in onCreateViewHolder as described in this answer: https://stackoverflow.com/a/31860393/6551120 . 我按照此答案中的描述在onCreateViewHolder中分配这些: https : //stackoverflow.com/a/31860393/6551120

I tried adding notifydatasetchanged() wherever I could imagine any value. 我尝试在任何可以想象的值处添加notifydatasetchanged()。 In duplicatedSelected() I tried unregistering and clearing adapter and LayoutManager and creating a new Adapter - without any result. 在plicateddSelected()中,我尝试注销并清除适配器和LayoutManager并创建新的适配器-没有任何结果。

This is the method that duplicates my elements (In ListActivity): 这是复制我的元素的方法(在ListActivity中):

private void duplicateSelected(){
   List selectedItemPositions = mAdapter.getSelectedItems();
   for (int i = 0; i < selectedItemPositions.size(); i++) {
      int j =(int) selectedItemPositions.get(i);
      modulElements.add(modulElements.get(j));
   }
   mAdapter.notifyDataSetChanged();

In MyAdapter: 在MyAdapter中:

private class ModulElementEditTextListener implements TextWatcher {
        private int position;

        public void updatePosition(int position) {
            this.position = position;
        }

        //Other Override Methods cut out for simplicity    

        @Override
        public void afterTextChanged(Editable editable) {
            updatePosition(position);
            int timesMultiplied;
            if(editable.toString().equals("")){
                timesMultiplied=Integer.parseInt("0");
            }else{
                timesMultiplied = Integer.parseInt(editable.toString());
            }            

            modulElements.get(position)
                .setMultiplier(newModulElementMultiplier());
            modulElements.get(position)
                .getMultiplier().setTimesMultiplied(timesMultiplied);            
        }
    } 

Expected result when entering quantity for E1: Quantity for E1 changes 输入E1的数量时的预期结果:E1的数量更改

Actual result when entering quantity for E1: Quantity for E1 and E2 (And E3, E4,E5... when I duplicate multiple times) changes. 输入E1的数量时的实际结果:E1和E2的数量(以及当我重复多次时E3,E4,E5 ...的数量)发生变化。

If I save the list of elements to a database and reopen it I can flawlessy edit quantity of E1 and it does NOT change quantity of E2 - as I would expect it to happen in the first case. 如果我将元素列表保存到数据库中并重新打开,则可以完美地编辑E1的数量,并且它不会更改E2的数量-我希望在第一种情况下会发生这种情况。

Every hint or idea welcome, thank you so much! 欢迎任何提示或想法,非常感谢!

You must implement the cloneable interface for your data model and modify this line 您必须为数据模型实现可克隆的接口并修改此行

modulElements.add(modulElements.get(j).clone());

Now you have different objects in the list 现在列表中有不同的对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM