Android: Wrong item checked when filtering listview

前端 未结 3 801
挽巷
挽巷 2021-01-06 13:25

I\'m suffering from the same issue as this question: Wrong item checked when filtering ListView in android

As suggested in the above question, I have an Hashset hold

3条回答
  •  离开以前
    2021-01-06 13:33

    This is my solution:

    • To selecte an item in list view after filtering, at the beginning, I got wrong item because I used this:

      ItemData item = listItems.get(position);
      

    The correct way should be like this:

        ItemData item = (ItemData) parent.getItemAtPosition(position);
    
    • To delete an item after filtered, I have tried this:

      for(int i=0;i

    But that didn't worked because my custom adapter. In my custom adapter, to use filter, I have two listItems:

        ArrayList listItemsToShow = new ArrayList< ItemData >();
        ArrayList< ItemData > listItemsBackup = new ArrayList< ItemData >();
    

    So to delete an item, I added new method in my custom adapter:

        public void deleteItem(String itemID) {
        for (int i = 0; i < listItemsToShow.size(); i++) {
            if (listItemsToShow.get(i).getId().equals(itemID)) {
                listItemsToShow.remove(i);
                break;
            }
        }
    
        for (int i = 0; i < listItemsBackup.size(); i++) {
            if (listItemsBackup.get(i).getId().equals(itemID)) {
                listItemsBackup.remove(i);
                break;
            }
        }
    

    Finally to delete an item in list view:

        subjectBaseAdapter.deleteItem(subject.getId());
        subjectBaseAdapter.notifyDataSetChanged();
    

    Hope this help

提交回复
热议问题