简体   繁体   English

从ArrayList中删除元素

[英]removing elements from an ArrayList

I have a Singleton class called User , this user have an ArrayList. 我有一个名为User的Singleton类,此用户有一个ArrayList。 now i have a ListView that display User's elements , selecting them and next press a button it will delete selected ones; 现在我有一个ListView,显示User的元素,选择它们,然后按下一个按钮,它将删除选择的元素; in this case : 在这种情况下 :

    view.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    view.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

        ArrayList<Chat> elementsToErase= new ArrayList<Chat>();
        @Override
        public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {



            if(b) {
                elementsToErase.add((Chat)view.getItemAtPosition(i));


            }
                else{
                    elementsToErase.remove((Chat)view.getItemAtPosition(i));

            }




        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            MenuInflater inflater = actionMode.getMenuInflater();
            inflater.inflate(R.menu.menu_chat_edit, menu);
            view.setSelected(true);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            switch (menuItem.getItemId()) {
            case R.id.delete_chat:
               Arraylist<Chat> userElement =          User.getIstance().getChats();
                for (int i = 0; i < elementsToRemove.size(); ++i) {
                userElement.removeChat(elementsToRemove.get(i));



                }

                adapter.notifyDataSetChanged();
                actionMode.finish();
                return true;

            default:
                return false;
            }

        }

before delete, if i create a new chat and try to add some text in it works. 删除之前,如果我创建一个新的聊天并尝试在其中添加一些文本,则可以。 After delete, if i create a new chat and try to add some text it won't add the text. 删除后,如果我创建一个新的聊天并尝试添加一些文本,它将不会添加文本。

this is how i remove elements from ArrayList : 这就是我从ArrayList中删除元素的方式:

   for(int i = 0 ; i < chats.size();++i){
          if(chats.get(i).equals(chat)) {
              return  chats.remove(i);
       }
        }
    return new Chat("null",0);
}

It's possible that i "break" the reference to the User ArrayList? 我可能会“破坏”对用户ArrayList的引用?

If you use this: 如果使用此:

Arraylist<Chat> userElement = User.getIstance().getChats();

Both userElement and User.getIstance().getChats() will use the same underlining array , modification done to either one of them will reflect in the other one. userElementUser.getIstance().getChats()都将使用相同的下划线array ,对其中一个进行的修改将反映在另一个array

If you want to have two different Objects , create the second ArrayList like this: 如果要具有两个不同的Objects ,则创建第二个ArrayList如下所示:

Arraylist<Chat> userElement = new ArrayList <Char>(User.getIstance().getChats());

Also, this loop: 另外,此循环:

   for(int i = 0 ; i < chats.size();++i){
      if(chats.get(i).equals(chat)) {
          return  chats.remove(i);
       }
    }

will throw a ConcurrentModificationException , if you want to remove Object s from a List while you iterate it, you have to use an Iterator . 将抛出ConcurrentModificationException ,如果要在迭代时从List移除Object ,则必须使用Iterator

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

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