Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide an item in a listview in Android

I know, this question was asked before, but I haven't seen a working answer for it.

Is there any way to hide some items in a ListView without changing source data?

I tried to set visibility of the item view to gone, it won't be displayed anymore, but the place reserved for this item is still there.

I also set:

android:dividerHeight="0dp" android:divider="#FFFFFF" 

Without success.

like image 642
Tima Avatar asked Feb 18 '11 12:02

Tima


2 Answers

You can either write your own ListAdapter or subclass one of the existing ones.

In your ListAdapter, you would simply filter out the items you do not want displayed by returning modified values for getCount(), getItem() and getItemId() as appropriate.

like image 97
Carsten Avatar answered Sep 22 '22 02:09

Carsten


if you want to hide the item like this:

convertView.setLayoutParams(new AbsListView.LayoutParams(-1,1)); convertView.setVisibility(View.GONE); 

can't be AbsListView.LayoutParams(-1,0);

if convertview are reused you should add this below to set it height back:

if(convertView.getVisibility() == View.GONE) {             convertView.setVisibility(View.VISIBLE);             convertView.setLayoutParams(new AbsListView.LayoutParams(-1,-2));         } 
like image 26
taotao Avatar answered Sep 25 '22 02:09

taotao