AutoCompleteTextView, ArrayAdapter and notifyDataSetChanged() - android

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.

Related

Not sure how to deal with ListView IllegalStateException: “The content of the adapter has changed but ListView did not receive a notification”

Sorry for a brick of a code but I read that I need to run something on UI thread, how do I do that? private ArrayList JsonFIveDays(String weatherSearchResults) is where I set my listview adapter if that helps.
You have to notify the adapter whenever you do changes in the weatherArrayList.
For Example,
weatherAdapter.notifyDataSetChanged()
But in your case to use like that you have to declare the weatherAdapter as a property of the class.
I hope it will help you.
You need to notify adapter that you changed data with notifyDataSetChanged(). You can do that in setData method after changing data.

Kotlin clear an adapter

What is the best way to clear an adapter in Kotlin?
I was trying to clear a recyclerview and update the values.
I am using LoaderManager and I am kind of stuck in this state.
If you want update recyclerview after adding/removing data in your adapter use this code
If you use Arraylist to store the data objects then just clear your list and call notifyDataSetChanged.
dataList.clear()
recyclerView?.adapter?.notifyDataSetChanged()

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.

What is dataset of BaseAdapter

When we use ArrayAdapter we pass something like a list to the super class. But base adapter constructor doesn't have any parameters. How does this class find the data set? I have seen in some examples they just define an array and override the functions without specifying the list as the Dataset. So how does the class understand this is the dataset? What if we define more than one list in the derived class?
Edit:
I think I should clarify my question. When we use ArrayAdapter the dataset is specified and the program knows what to iterate and calls getView for each of them. But in BaseAdapter we only define a list and override 4 functions and it works! My question is why does it work?! we didn't specify the dataset we just specify the getView body and it returns a view. I don't understand how the program finds the dataset.
As the documentation explains it well :
Common base class of common implementation for an Adapter
Means you have to do the implementation.
http://developer.android.com/reference/android/widget/BaseAdapter.html
How does this class find the data set?
It doesn't. Your subclass of BaseAdapter manages the data set.
What if we define more than one list in the derived class?
So long as you implement the abstract Adapter methods properly (getCount(), getView(), getItem(), getItemId(), ...), how you manage your own data is up to you.
Well after more considerations on the codes I think this is how base adapter works:
It loops the getView() function with 'position' parameter to be from 0 to what is returned by getCount().
we must override getCount() and send the correct index of the last item of dataSet. Each time getView() is called we can work with the views and any list we want according to the current position.
And I think the main difference between ArrayAdapter and BaseAdapter is that ArrayAdapter finds the last index of the list when we pass it to the super class but in base Adapter we should define the last index. Implementation of getView() is the same and we can use any list we want in getView. The trick was only about position parameter.

Editing ArrayLists in Android ListView

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()

Categories

Resources