Android: invalid item checked while filtering list

I am suffering from the same problem as this question: Invalid item checked while filtering ListView in android

As suggested in the previous question, I have a Hashset containing all the selected identifiers, but I cannot figure out how to use it when the cursor overflows the marked elements.

My problem is only cosmetic - for example:

  • Facebook is in fifth position on the unfiltered list.
  • The user performed a search for “face”, only “Facebook” will appear in the 1st position in the filtered list.
  • The user checks Facebook as selected and returns to the unfiltered list.
  • The item being checked is the first item on the list, not Facebook (position 5).

Note: Other than that, everything else works fine. For example, “delete” will delete the desired items because I use selectedIds for this (even if the marked items are wrong).

One click on a list item:

OnItemClickListener mOnItemClickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { //gets the Bookmark ID of selected position Cursor cursor = (Cursor)parent.getItemAtPosition(position); String bookmarkID = cursor.getString(0); boolean currentlyChecked = checkedStates.get(position); checkedStates.set(position, !currentlyChecked); if (!selectedIds.contains(bookmarkID)) { selectedIds.add(bookmarkID); selectedLines.add(position); } else { selectedIds.remove(bookmarkID); selectedLines.remove(position); } } }; 

Inside the cursor: - this is where the problem is.

This refills the checked items - the problem is that it does it by position (pos), and what was the correct position of the element in the filtered list is not its position in the unfiltered list, which leads to an erroneously marked element.

 CheckedTextView markedItem = (CheckedTextView) row.findViewById(R.id.btitle); markedItem.setChecked(checkedStates.get(pos)); 

Would thank for any help!

+3
android android-listview
source share
3 answers

Solved a problem. As suggested in the question that I added to myself when you fill in the checkboxes, another list / Hashset should determine whether to mark the item as checked or not.

Using my code:

 //first you will need to get the current item ID String bookmarkID = cursor.getString(0); //Then, check if the current ID is in the selectedIds hash/list //If true - mark current item view as checked, else - uncheck. CheckedTextView markedItem = (CheckedTextView) row.findViewById(R.id.btitle); if (selectedIds.contains(new String(bookmarkID))) { markedItem.setChecked(true); } else { markedItem.setChecked(false); } 

Hope this helps anyone! :)

0
source share

Could it just be that the ListView does not know that the data set has changed? Call notifyDatSetChanged () on your adapter to report it.

 my_adapter.notifyDataSetChanged(); 

This will redraw the list with the updated data.

+1
source share

This is my decision:

  • To select an item in the list view after filtering, at the beginning I got the 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 remove an item after filtering, I tried this:

     for(int i=0;i<listItems.size();i++){ if(listItems.get(i).getItemID() == item.getItemID()){ listItems.remove(i); myAdapter.notifyDataSetChanged(); } } 

But this did not work, because my custom adapter. In my user adapter, to use a filter, I have two listItems:

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

So, to remove the item, I added a new method to 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 remove an item as a list:

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

Hope for this help

0
source share

All Articles