Add item to spinner after it has already been filled - android

I create a spinner somewhere in my code and fill it in with values
val dataAdapter = ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, some_list)
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
mySpinner?.adapter = dataAdapter
My Question is, How do I add more items to this spinner after I have applied the adapter in a completely different section of code in Kotlin? for example, after a server request
I understand you can access the adapter via
mySpinner?.adapter
But how do I add to it?
EDIT.
I Understand there is a solution in the following code
some_list.add("Apple");
dataAdapter.notifyDataSetChanged();
However this requires having access to 3 data items at a class or global level instead of just 1 (the spinner). I understand this may be the only way, it is just sub-optimal, thank you for your responses

you can create a new adapter with a the newest list and then assign it to the spinner , or you can create a custom adapter with your item list and then just add the new item to the list and call notifyDataSetChanged()

Related

Single & Multiple Items Select in listview using listadapter in listactivity

I am using customized listview with listadaper in listactivity.I want to add the option for single & multiple items selecting in listview.I searched about this but all are using check box with adapter class.I dont have any adapter class in my app.I am using listactivity with no adapter class.So how can i get the view of checkbox for single /multiple select?
My adapter:
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), entriesList,R.layout.new_todo_singleentry, new String[]
{"``id","time","date","message","status"},new int[] { R.id.todo_id,R.id.todo_time,R.id.todo_date,R.id.todo_entry});
I'm not aware of an option to do this without a custom adapter class.
I would advice you to read this and use the example in chapter 14 to create the functionality that you are looking for.

How to hide items of listview in android

I'm trying to develop an app in which i need to create a list which contain many items.
There is also some button represent categories , and i want to populate my list according to the category.
For example: if there are 10 items in the list out of which item 1,5,7,8 are belongs to 1st category and rest of them belongs to 2nd category,now if user press 1st category button then listview show items belonging to 1st category only.
How can I do it?
I think ExpandableListView will work for you. Refer this tutorial: http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
If you wish to be able to filter (hide/show) items in a ListView you can use an adapter, for example by creating your own implementation of BaseAdapter. This adapter has methods for sorting, clearing and adding elements to the dataset used by your ListView, as stated in this list tutorial. Just remember to call notifyDataSetChanged() after you've altered the dataset to refresh the ListView.
Setup example:
ListView list = (ListView)findViewById(R.id.list);
adapter = new MyAdapter(...); // Probably send along data
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
Filter example:
int[] newData = { 1, 5, 7, 8 };
adapter.clear();
for(int i : newData)
adapter.add(i);
adapter.notifyDataSetChanged();
You need to hide item in list (remove from list) before you set adapter. When your list is ready set notifyDataSetChanged() to adapter

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

Why is the ArrayAdapter doesn't have the set method like arraylist do?

Well it seem i am having quite some trouble with my arrayadapter of strings. It seem there is no real way to only modify an item in the list. I had to delete and insert back the string to see changes appears. So my question is Why is the ArrayAdapter doesn't have the set method like arraylist do ? Why it do not have any easy way to change an element in the list ? In order to change an element is is an option to create a custom adapter based on BaseAdapter ?
lets see this sample code:
ArrayList<String> items = new ArrayList<String>();
MyAdapter myAdapter = new MyAdapter(items, context);
you can modify an item using:
// items.set(index,object);
items.set(0,"Hello New Value");
myadapter.notifyDataSetChanged();// to tell the adapter there's new values has been changed

Which is the correct way to add item into ArrayAdapter

Consider the following code.
List<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, resource, textViewResourceId, list);
// Method 1 : Add an item.
adapter.add("ITEM1");
// Method 2 : Add an item
list.add("ITEM2");
I was wondering, which is the correct way to add item into ArrayAdapter? As seems to me, both methods just work fine.
Method 1 updates the associated AdapterView, if you have already attached the ArrayAdapter to the AdapterView. Method 2 does not, requiring you to call notifyDataSetChanged() on the ArrayAdapter.
Typically, you populate the ArrayList before creating the ArrayAdapter, then use Method 1 to add new entries dynamically later on (e.g., based on user data entry).
This is what I do, especially for my Search Results page - where it grows as the user scroll down (list changes).
I'd keep a local ArrayList of Strings that is global to the class,
Initialize the adapter (also locally and globally),
Have a designated method alter the ArrayList of strings,
Then call the adapter.notifyDataSetChanged();
This will not only update your list and also update the adapter to work in sync.
Hope this helps,
best,
-serkan

Categories

Resources