Reputation: 5671
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
from getItem()
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 set View.GONE
in getItem()
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.
Upvotes: 3
Views: 11233
Reputation: 7486
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.
Upvotes: 6
Reputation: 5223
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...
}
Upvotes: 10
Reputation: 1100
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.
Upvotes: 1