ListView hide some items
Solution 1
I would implement this in the following way:
Create a list/array inside of your adapter which will hold the indexes of the items that are going to be hidden.
For example:
List<Integer> hiddenItems;
Override your adapters
getCount()
method to return the number of items in your list MINUS the number of items in the collection mentioned in step 1.For example:
@Override public int getCount() { return items.size() - hiddenItems.size(); }
Override
getItem()
to return the items offset by the indexes contained in the collection from step 1.For example:
@Override public Object getItem(int position) { for (Integer hiddenIndex : hiddenItems) { if(hiddenIndex <= position) position++; } // Now your regular getItem code... }
Solution 2
UPDATE:
Yes you can add/remove
even after you have provided the data to the adapter. Store the data in some ArrayList<type>
and pass it to the custom adapter class.
ArrayList<String> dataList;
// store data in dataList
list.setAdapter(CustomAdapter(YourClass.this, dataList));
The easiest work around is to remove
those items from the adapter of the ListView and call notifyDataSetChanged()
. This will remove the cells/row
of those items from the list.
adapter.remove(object);
adapter.notifyDataSetChanged(); // this will update your ListView
If you plan to use those items in the future in the list again, store them in separate place For example store them in a file or database, which ever suits your needs the best.
Solution 3
Seems to me your best bet would be to either extend the ListAdapter you're using and add methods for hiding certain rows and adjust getCount()
etc. Or you could write a wrapper adapter (you could implement the WrapperListAdapter
interface) for use with any ListAdapters here and in the future whenever you need to hide rows.
Lingviston
Updated on June 04, 2022Comments
-
Lingviston almost 2 years
I want to hide some items in ListView depending on some criteria. I found 2 solutions but none of them work. I've tryed to return empry
View
fromgetItem()
method but divider is still visible. So if I hide all of the items for example it leads to a large stack of dividers. The second one was to setView.GONE
ingetItem()
method. But ListView still reservates place for invisible items and that leads to the empty views in my list. Has anyone found a workaround for this question?EDIT: Also I need to notice that I can't remove any data from the dataset. Adapter/ListView must only HIDE specified items.
-
Lingviston over 10 yearsSorry, I've already implement my own Adapter which removes items from provided ArrayList also. So I can't use remove method for this propose. Actually I need to edit my answer.
-
Umer Farooq over 10 years@Lingviston Updated the answer
-
Marian Klühspies about 9 yearsBut what if the last item is hidden? In your case, it would raise the position b one, but there is no element left
-
CoolMind over 8 yearsDon't forget to sort hiddenItems if they are not not sorted. It took me many hours to fix that bug. Also as @MarianKlühspies noticed, I added some code: return position < list.size() ? list.get(position) : null;