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.
Related
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
I know I can update the array which was initally passed to the arrayAdapter's constructor, and call notifyDataSetChanged.
I wonder if it's possible to plug-in new arraylist into adapter?
simple:
mAdapter.clear();
mAdapter.addAll(collection);
also, do add individual elements you can call mAdapter.add(element); instead of having to keep a reference to the Array that was passed to the constructor.
I'm sending an bean Object from the first to the second View. Other times I use ArrayAdapter when I had an Array with data to show but this time I only have an bean Object that I want to show. What type of Adapter I must to use ?
You do not need to use any adapter, just pass the object itself. You can use the passed object in the activity directly, for setting the contents of the UI controls, for instance. You need adapters when you have a list of items. More information on this is provided on the class documentation of Adapter here: http://developer.android.com/reference/android/widget/Adapter.html
I have an ArrayAdapter that's using an ArrayList to display data in a ListView.
During the course of the activity, I sometimes need to edit the ArrayList by adding and deleting items.
Is there a difference if I call the add/delete functions on the actual ArrayAdapter vs. the underlying ArrayList? Which is better to use?
Use the adapter methods. This will automatically notify your adapter (and thus the bound list) that your data has changed.
Some times it is necessary (or at least more convenient) to modify the ArrayList (e.g., is a field of some other class, or it is being modified by other thread that does not know about the adapter).
In those cases, you will need to call adapter.notifyDataSetChanged()
I'm using an AutoCompleteTextView with an ArrayAdapter which works like supposed.
The problem is, that I have to change the Array with the Autocomplete-Values. Calling notifyDataSetChanged() doesn't help. No changes are shown.
Do you know something to get around this problem?
Do not modify the ArrayList and call notifyDataSetChanged() as it will have no effect on ArrayAdapters (implementation seems broken).
Use clear(), add(), insert(), and remove() directly on your ArrayAdapter instead of those methods on your ArrayList.
You need to add more details to the question but based on a guesstimate of you problem, I would say that there is some problem in the implementation.
notifyDataSetChanged() informs the view to reload the data. If the data set up methods in the ArrayAdapter reference an unchanged data entity, notifyDataSetChanged() will have not effect.
A custom adapter implementation that extends ArrayAdapter will generally have an internal data structure that is the source of data for the adapter and which will contain the AutoComplete values you require.