简体   繁体   English

第一个列表消失在回收者视图中消失了

[英]First list disappears in recycler view disappears

I have a RecyclerView that shows different lists depending on which option I pick in my Spinner . 我有一个RecyclerView ,根据我在Spinner选择的选项显示不同的列表。 I can add items to a list dynamically, but when I add items to the top option on the Spinner , two items appear everytime I add, when it is only supposed to add just one and when I choose a different option in the Spinner and then back to the top option, all items that are supposed to be in it are gone. 我可以动态地将项添加到列表中,但是当我将项添加到Spinner的top选项时,每次添加时都会出现两个项,当它只添加一个时,我在Spinner选择一个不同的选项然后回到顶部选项,应该包含在其中的所有项目都消失了。 However, when I repeat the same actions in another option, one item is added at a time, and they are still there if i switch to another and back, so it is only in the first option where I experience these problems. 但是,当我在另一个选项中重复相同的操作时,一次添加一个项目,如果我切换到另一个项目并返回它们,它们仍然存在,所以它只在我遇到这些问题的第一个选项中。

Here are pictures that shows what happens. 这是显示情况的图片。 First picture shows the entire Spinner , second is the top option selected with items added, third is the second Spinner option selected and fourth is back to the top option and now the items are gone. 第一张图片显示整个Spinner ,第二张是选中添加项目的顶级选项,第三张是选中的第二个Spinner选项,第四张是返回顶部选项,现在项目已经消失。

BabouBabouBabouBabou

This method is used to add a Item to one of the lists. 此方法用于将项目添加到列表之一。 Each option in the Spinner is used to get an element in listOfLists where each element is an ArrayList<Item>() . Spinner中的每个选项用于获取listOfLists中的元素,其中每个元素都是ArrayList<Item>() mItemList is the list which the RecyclerView prints out. mItemListRecyclerView打印出的列表。

  public void addItem(){
        mItemList.add(0, new Item(idText.getText().toString(), addAmountField.getText().toString()));
        listOfLists.get(dropdown.getSelectedItemPosition()).add(0, new Item(idText.getText().toString(), addAmountField.getText().toString()));
        System.out.println("ListSize: " + listOfLists.get(dropdown.getSelectedItemPosition()).size());

        addAmountField.setVisibility(View.GONE);
        btnAdd.setVisibility(View.GONE);
        mAdapter.notifyDataSetChanged();
        saveData2();
    }

This is the code for switching the lists when I choose another option from the Spinner . 当我从Spinner选择另一个选项时,这是用于切换列表的代码。 What I do here is that I clear the mItemList and then I fill it with one of the ArrayList<Item> inside listOfLists . 我在这里做的是我清除mItemList ,然后我用的一个填充ArrayList<Item>listOfLists

 dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                mItemList.clear();

                for (int i = 0; i < listOfLists.get(position).size(); i++) {
                    mItemList.add(listOfLists.get(position).get(i));
                }


                mAdapter.notifyDataSetChanged();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

        });

How can I resolve this so that the top option of the Spinner will work just like the other options where it adds one item at a time and still has the items after I switch to another option? 我如何解决这个问题,以便Spinner的顶部选项可以像其他选项一样工作,它一次添加一个项目,并在切换到另一个选项后仍然有项目?

I managed to solve it so for anyone who faces similar problems this was the problem: 我设法解决了这个问题,所以对于遇到类似问题的人来说,这就是问题:

Unlike the other lists in listOfLists where I set mItemList to the chosen list like this 与listOfLists中的其他列表不同,我将mItemList设置为所选列表,如下所示

 mItemList.clear();

 for (int i = 0; i < listOfLists.get(position).size(); i++) {
      mItemList.add(listOfLists.get(position).get(i));
 }

mItemList is always set to the first list when I run the app like this 当我像这样运行应用程序时,mItemList始终设置为第一个列表

mItemList = listOfLists.get(dropDown.getSelectedItemPosition());

So I just changed it so it sets mItemList the same way it is done with the other lists and now it is working. 因此,我只是对其进行了更改,以便它以与其他列表相同的方式设置mItemList,现在它可以工作了。

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

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