I'm using the navigation drawer example in Android. What's the easiest way to change the string items in the adapter dynamically? Do I just create a new adapter and set it with my new values?
The problem is I'm altering the string items based on user state logged in and logged out. How do I access it from a non static context and just say update list adapter? It doesn't seem to be re-drawing itself and running my adapter code which is dynamic based on user state, so I guess it runs/inits once and I have to create and load a new adapter if I want to change it later?
Thanks.
You haven't given us much to go on (a little code would be nice), but you can update the data in the ListView by calling notifyDatasetChanged() on the Adapter. If you are not using a custom Adapter e.g. an ArrayAdapter then you can just create a new Adapter instance with different String values and set it to the ListView.
Related
I'd like to achieve the following behaviour:
When an spinner is touched it makes an http post that returns some results.
Until the results are received the spinner doesn't display showing its current results.
Once the results are received it finally displays showing the results.
I successfully manage to make the petition and getting the results problem is that it looks like when an spinner is touched it automatically displays. There seems to be no way of telling it: "I have now touched you, stop there, and when response is received then show the results that you have received."
Is there any way to do this?
Since every time user touch spinner you made a request, what you could do is , as user touch spinner , clear the values in adapter.
//Here is a list of options to use in spinner.
List<String> options;
ArrayAdapter<String> adapter = new ArrayAdapter<>(context , R.layout.simple_spinner_items , options);
as user touch the spinner clear adapter items or if you using custom Adapter implement clear functionality or if the custom adapter inherits ArrayAdapter then no need to implement clear functionality ,its already there.
adapter.clear();
once you received the data which is List of String (I'm assuming)
adapter.addAll(options);
I am trying to reinitialize the already set adapter for a spinner in android. I don't wanna create a new adapter object because the material spinner library i am using is having an issue when replacing already set adapter with a new object .
So I have a list that changes over time and once after I set the adapter to the spinner initially , afterwards when the list gets changed (updated , cleared etc) I want to reinitialize the already set adapter (using something like adapter.setList() whatever ) ...
I am new to android . Whats the best way to do this with minimal code ?
Try to call mAdapter.notifyDataSetChanged() after changing your adapter data (i.e. the list etc.). Without calling it, nothing will happen. Hope it helps.
So I have a custom spinner header, where the user can change the title of names based on selection. What I want to do is if they click on the spinner and change the default, then it only shows the names in the listview from the title of names. Currently I have tried this by reinstantiating my fragment from my tabhost, and if conditions are met it would just remove from list everything that doesnt have what user selected. But this is inefficient and it didn't work as the listview wasn't updated and the condition wasn't even getting called on the new instantiation... Is there any filter way I can do for this? Any ideas or help would be great!
You only need to refresh the ListView not the entire fragment. There are several ways to do this, including providing a different data source to the ListView's adapter. The exact details for how to do this depend on whether you are using an ArrayAdapter or a CursorAdapter.
I know a list view needs an adapter, but WHY what is the purpose of it, and can it show lists without an adapter ?
An adapter manages the data model and adapts it to the individual rows in the list view.
Filtering and sorting of the data is handled by the adapter.
The notifyDataSetChanged() method on the adapter is called if the data has changed or if new data is available.
The notifyDataSetInvalidated() method is called if the data is not available anymore.
Also see What's the role of adapters in Android?
From the docs:
An Adapter object acts as a bridge between an AdapterView and the
underlying data for that view. The Adapter provides access to the data
items. The Adapter is also responsible for making a View for each item
in the data set.
The ListView needs to know which items to show, and needs to get Views for these items. The Android team chose to implement this using Adapters.
Note that for showing simple lists of Strings, there is the ArrayAdapter class.
I have a ListView.
I'm using anArrayAdapter and I'd like to:
when the user clicks any item on the list, its LinearLayout (is just a content for x information) fades out and is substituted by other LinearLayout (with y information) which fades in.
However, I do not know how to apply this on my ArrayAdapter. I've searched for a while but I'm not understanding how can I access a single item from the Adapter and make it's children fade out or fade in.
Any help would be very appreciated. Thank you very much for your time.
In general - adapters do not hold items the role of adapter is to get data model, and to produce item views when i.e. list view needs them.
The main concept of replacing item is to replace data inside the adapter and call notifyDataSetChanged(); - thats all.
I don't like ArrayAdapter as it's aimed for very simple scenarios. Usually I just create my own Adapter class extending BaseAdapter (just 4 methods to implement).
The transitions of items on ListView are described here: Adding animation to a List View in Android