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
I have a CursorAdapter
that can't be modified with backing array, because checking whether item should be shown or not was after getting result from database. I've implemented solution in bindView(View v, Context context, Cursor c)
in similar method as was described in other posts. I think that the best way is overriding bindView()
method rather then getView(int position, View convertView, ViewGroup parent)
because you should carry about null-ckecking for convertView in getView().
The second thing: I've tried to hide View v
in bindView(View v, Context context, Cursor c)
and it doesn't worked. After investigation I have figured out that I have to hide each element in view (including layouts that contain your texts, images and etc.)
In some case you have an easy solution :
I have to hide a View in a list view because the items to populate the view are invalid, So I don't want to see the view :
In my list adapter :
public class PlanListAdapter extends BaseAdapter{
//some code here : constructor ......
// my code that create the view from the item list (one view by item ...)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.my_layout, null);
if(my_data_are_not_valid) {
//just create an empty view
convertView = new Space(context);
}
else {
//populate the view with data here
populate(convertView);
}
return convertView;
}
//some code here to populate the view ...
}
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));
}
A hack would be to set the height of the list item you want hidden to 0.
But, as seretur says, the correct approach is to remove the item and use notifyDataSetChanged()
. Why is this solution not appropriate in your case?
For me a simple solution was to create my own adapter with a custom row layout, containing a wrapper of all content (e.g. LinearLayout), and set this wrapper visibility to View.GONE in the adapter's getView() method, when I did not want that item shown. No need to modify the data set or maintain two lists. When creating the ListView, I also set it to not automatically show dividers:
android:divider="@null"
android:dividerHeight="0dp"
and draw my own divider (which I also set to GONE when I don't want that item). Otherwise you'd see multiple dividers or a thick gray line where multiple items were hidden.
If you create your own adapter
you can do it in the public View getView(int position, View convertView, ViewGroup parent)
method. This can be useful in case you are planning to show the invisible items at some point. For example:
if(item.getYourRequirement == undesiredVlue)
convertView.setVisibility(View.GONE);
else
convertView.setVisibility(View.VISIBLE);
I hope this helps