clear ExpandableListView - android

I am using an ExpandableListView along with a ViewPager so in onPageSelected() I need to clear the list.
I know one way to do it for a ListView is setAdapter(null) but I get The method setAdapter(ListAdapter) is ambiguous for the type ExpandableListView error if I try it on ExpandableListView.

Try by type casting null with BaseExpandableListAdapter like below.
listview.setAdapter((BaseExpandableListAdapter)null);

Have you tried inputting an empty adapter? Something like the following:
ExpandableListAdapter adapter = new ExpandableListAdapter(); //or what ever adapter you use/created
listView.setAdapter(adapter);
This is not a null adapter and you will not get a null pointer. The adapter will contain no elements to display/show/populate.
You can check if the adapter is empty by calling isEmpty().

Related

Should adapter be local variable or instance variable?

When setting adapter for listView, should I just do listView.setAdapter(new MyAdapater()); or should I keep the adapter as instance variable and set it to null when onDestory() ?
The answer depends on the use case.
If you are going to do data manipulation such as rearranging the order of elements or changing the data dynamically in some way, then its "better" to have an instance variable of your adapter. It will safe you from casting your adapter from ListView getAdapter() method, whenever accessing your adapter.
If you are creating a simple list view consisted of for ex. 10 Strings and you dont plan on doing anything with the data set, then you don't need to keep a reference to your adapter.
It is better to maintain adapter as instance variable because each and every time you have to create new adapter instead of that just change the data and you can call notifyDatasetChanged() so that adapter will be refreshed.

Deleting Custom List view item android without remove method

I have created a custom list view whose parent class is Base Adapter. Now i need to delete its menu item .i searched for it and most of the tutorial says to use "remove" method and will be done.But actually in all tutorials they extend their class from Array-adapter.Now problem in my case is i didn't find the remove method in Base Adapter class to use it . so how i can remove my list view item in this case ?.
You won't find a remove in a BaseAdapter, because ArrayAdapter is already an Array so..
What you can do is remove the item from the ArrayList<> and then set it again for the adapter, and after that call notifyDataSetChanged(),
have a setData(ArrayList) in your adapter so you don't have to instantiate a new one. i.e.
setData(ArrayList list) {
this.list = list;
notifyDataSetChanged();
}
hope this helps

can't refresh data with ArrayAdapter

My data are defined in a global variable ArrayList cells and my custom ArrayAdapter holds a reference to that variable:
class CellAdapter extends ArrayAdapter<Cell> {
CellAdapter() {
super(context, 0, 0, cells);
}
}
Then I have a method in which i refresh the cells by calling clear() and then using the add() method. After doing this, then I call:
adapter.notifyDataSetChanged();
The adapter should still hold a reference to the global variable with which I have initialized it, right? Anyway, it's not working. What am I doing wrong?
adapter.clear() is calling cells.clear() since the super class holds a reference to your ArrayList<Cell>. He works, but you are filling up the adapter with an empty dataset.
An ArrayAdapter does not make a copy of your cells array list. Whenever you make modifications to the objects inside the cells array, you can just call notifyDataSetChanged on it.
If you modify the list itself (especially removing items from the list) you must notify the adapter, otherwise it will start throwing exceptions.
adapter.clear();
is clearing your cells object as well.
Just don't call clear and addAll
If you have updated your cells list then just call:
adapter.notifyDataSetChanged();
It will refresh

Adapter notifyDataSetChanged nor working

I used notifyDataSetChanged for adapter but my List didn't updated. Here is part of my code.
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,userNames);
list = (ListView) findViewById(R.id.listViewUsers);
list.setAdapter(adapter);
SO every time I'm adding item to userNames , I'm calling adapter.notifyDataSetChanged(); method, but there are no any updates. I tried many things with cleaning , clearing userNames but it's not working.
Can anyone help me?
Thanks
EDIT:
I'm calling these two functions where I'm adding new user.
userNames.add(user.getUsername());
adapter.notifyDataSetChanged();
Also I have User class, but I fill in ListView with users' usernames.
Try add username directly to adapter instead of adding to userNames. Maybe when you create a new instance of ArrayAdapter, it will create a new private List inside adapter and you add username to old instance of userNames :
adapter.add(user.getUsername());
adapter.notifyDataSetChanged();
For an ArrayAdapter, notifyDataSetChanged() only works if you use the add, insert, remove, and clear functions on the Adapter.
When an ArrayAdapter is constructed, it holds the reference for the List that was passed in. If you were to pass in a List that was a member of an Activity, and change that Activity member later, the ArrayAdapter is still holding a reference to the original List. The Adapter does not know that you changed the List in the Activity.
Try this:
userNames.add(user.getUsername());
adapter.clear();
adapter.addAll(userNames);
adapter.notifyDataSetChanged();

Calling notifyDataSetChanged on a ListAdapter

I have a ListAdapter as shown below:
setListAdapter(new ArrayAdapter<String>(DeleteMenu.this,
android.R.layout.simple_list_item_1,
classes));
I am trying to call notifyDataSetChanged() on it within an onListItemClick() function. I've tried a few different ways and looked at similar questions here on StackOverFlow but I still can't figure it out.
Can someone please help?
Edit: I should be more clear, I'm not getting an error, I simply don't know what to call the function notifyDataSetChanged() method on. Do I have to assign my ListAdapter to a variable and call it like var.notifyDataSetChanged()?
You should call notifyDataSetChanged on Adapter.
Adapter adapter = new Adapter();
ListView list = (ListView) findViewById(R.id.listview);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
You can call the method notifyAdapterChanged() by getting a reference to the Adapter. You can either keep a local reference (like user1411084s answer), or retrieve it by calling:
getListAdapter()
Important to notice is that the interface ListAdapter doesn't provide the method notifyDataSetChanged(), so you should cast it to the type of the adapter you assigned earlier.
The result will look like this (able to call anywhere in your ListActivity/ListFragment:
((ArrayAdapter) getListAdapter()).notifyDataSetChanged();
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
See more info in this answer:
https://stackoverflow.com/a/4198569/3994630

Categories

Resources