user3759635
user3759635

Reputation: 1

How can i hide a listview item in android?

Hi friends i have a listview and the contents are fetched from a webservice call. In that webservice call, there are fields like

 "OGType": "ORG" and  "OGType": "GROUP"

If click a button, the listview must shows the item having "OGType": "ORG", and hide the item having "OGType": "GROUP". Hope you understand what i meant. Please anyone help me for that. Thanks in Advance.

Upvotes: 0

Views: 247

Answers (5)

Shivam Verma
Shivam Verma

Reputation: 8023

The non hackish way will be to remove the items from your Collection which you use to generate the listview and then call notifyDataSetChanged();

Upvotes: 0

Stanislav
Stanislav

Reputation: 405

Try to set new data (only with ORG) to adapter and then call

adapter.notifyDataSetChanged();

Upvotes: 1

MiguelAngel_LV
MiguelAngel_LV

Reputation: 1258

You can use a visible list and filters lists. You should use "visible" for complete the BaseAdpter as always, then, you can change the pointer of visible to other list (all, filter...)

Don't worry by the memory, are pointers, you only have each element only once.

public class MyAdapter extends BaseAdapter {

   private ArrayList<MyItem> visible;
   private ArrayList<MyItem> all;
   private ArrayList<MyItem> filter;

   public MyAdapter(ArrayList<MyItem> items) {
       all = items;
       visible = all; //Set all as visible
       filter = new ArrayList<Item>();
       for (Item i : items)
           if (i.getType().equals("ORG"))
               filter.add(i);

   }

   //Complete adapter using "visible"

   public void showOnlyOrg() {
      visible = filter;
      notifydatasetchanged();
   }
}

Upvotes: 0

Varundroid
Varundroid

Reputation: 9234

You can create a custom adapter and pass data to it in the form of Array or ArrayList (ArrayList is better when dealing with Custom Adapters). Whenever you need to add or remove the data from ListView, just add or remove the item to or from you ArrayList and call notifyDataSetChanged() on your custom adapter and it will update the ListView automatically.

In your case, whenever you click a button, edit you ArrayList and call your custom adapter's method called notifyDataSetChanged() and that's it. You'll see every time you call this method ListView will refresh itself if you have made any changes to the data. Hope it helps.

NOTE - CUSTOM ADAPTER IS NOT COMPULSORY. ANY ADAPTER CAN BE USED e.g SimpleAdapter, ArrayAdapter etc.

Upvotes: 0

G&#246;del77
G&#246;del77

Reputation: 839

You can do it in your getView Method in your Adapter Class. That's the header public View getView(int pos, View convertView, ViewGroup, parent)

There you can properly hide the element(s) you want, you know, using the method setVisibility()

For more help you can take a look here

Upvotes: 0

Related Questions