Android, remove adapter from listview - android

I have a ListView that I'm using for some different adapters, is this right to use following code for removing my ListView adapter and clear the ListView ?
list.setAdapter(new ArrayAdapter<Void>(mContext, 0, new Void[]{}));
What is your offer? and what is the best way?
Code has been updated

Simply Set Listview.setAdapter(null)

clear the data in the resource (i.e) what ever your using in the arrayAdapter
madapter.notifyDataSetChanged();

Related

Add item to spinner after it has already been filled

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

Android Custom ListView/Adapter

I am wanting to change my listView from the normal ArrayAdapter (simple_list_item_1) to something more like this:
Name..... Score(center right).
Date (under name)
So There's 3 Views...
name
date
score
I've looked up how to make custom adapters and layouts but they are all very confusing. I just want a simple fix that I can add to an existing project.
Here's my code for my list right now:
//update listView
listAdapter = new ArrayAdapter<String>(GradesActivity.this, android.R.layout.simple_list_item_1, names);
mListView.setAdapter(listAdapter);
Help is appreciated!
I just had the SAME problem last week. My solution was a tutorial on the internet: http://androidtuts4u.blogspot.com.br/2013/02/android-list-view-using-custom-adapter.html
Just copying and pasting the code onto a temporary project (it will take less than 15 minutes) will help you understand how simple it is and also make it very clear so you can implement it into your original project. I hope it helps!
Peace!
ArrayAdapter can't auto binding three view
You can try SimpleAdapter or extend BaseAdapter implements it youself
This is Google's training
https://developer.android.com/training/material/lists-cards.html
https://developer.android.com/training/wearables/ui/lists.html
And here is a sample of SimpleAdapter http://www.java2s.com/Code/Android/UI/UsingSimpleAdaptertofilldatatoListView.htm
There are many fixes you need to do
1. Create a class that extends BaseAdapter.
Override all the methods create a constructor of that class that will initialize listitems
You are just passing names
listView listAdapter = new ArrayAdapter(GradesActivity.this, android.R.layout.simple_list_item_1, names);
Instead of just names create a class that contains three elements you need I.e name date and score and make there get and set methods
And the object of the class will be saved in a list.
4. In getview method you have to set these variables

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

ListView refresh taking it to the top of the list

Scenario: I am using a listview with custom Adapter. I am fetching data every 5 seconds and then displaying it in the listview.
Problem: After every refresh the list scrolls at the top. I would want the list to stay as is from the scroll standpoint and just the underlying data to be refreshed.
Current code: This is called every time I fetch a new data.
mListAdapter = new CustomListAdapter(this, mListData);
mList.setAdapter(mListAdapter);
Instead of your code please try to put every time you fetch data this one:
adapter.notifyDataSetChanged();
Instead of calling this, you should set the new data to the adapter and then call notifyDataSetChanged.
Hope this helps!
As you get new mListData then just invalidate the adapter not re-initialized the adapter you can call like this
mListAdapter.notifyDataSetChanged();
You can use
mList.setSelection(mListData.size() - CONSTANT.CONTENT_SIZE);
This way the listview will not refresh to the top.
Or you can also use
adapter.notifydatasetchanged
by updating the binded list.
This Worked For Me:
adapter.setData(list);
adapter.notifyDataSetChanged();

How to refresh dynamic listview?

I have a dynamic listview and I want to refresh the list when any new item is added.
How can I do this.Pls help me.
Thanks,
Monali
Call notifyDataSetChanged() on your adapter
Here is a demo for how u can do that http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
USE setListAdapter(yourAdapter);
Make your adapter to ListAdapter find more details here
then just use
yourAdapter.notifyDataSetChanged(); and you are done !
1) create a method
2) set adapterView in that method.
3) at click event of any image button call that method.
or to put Adapter.notifyDataSetChanged();
I think this would be easy for you.
Also you can do this like...
I have custom row for list. It has textview and I can change it's value ...
((TextView)(myWatchList.getChildAt(INDEX - myWatchList.getFirstVisiblePosition()).findViewById(R.id.symbolCountTV))).setText(NEWSTRING);

Categories

Resources