How to refresh Android listview?
Call notifyDataSetChanged()
on your Adapter
object once you've modified the data in that adapter.
Some additional specifics on how/when to call notifyDataSetChanged()
can be viewed in this Google I/O video.
Android: how to refresh ListView contents?
To those still having problems, I solved it this way:
List<Item> newItems = databaseHandler.getItems();
ListArrayAdapter.clear();
ListArrayAdapter.addAll(newItems);
ListArrayAdapter.notifyDataSetChanged();
databaseHandler.close();
I first cleared the data from the adapter, then added the new collection of items, and only then set notifyDataSetChanged();
This was not clear for me at first, so I wanted to point this out. Take note that without calling notifyDataSetChanged()
the view won't be updated.
Correct way to refresh a ListView adapter in Android
I think the correct way to do it is a use
startActivityForResult(...)
method to launch your Second activity (where your new items is added).
And in your second activity use
setResults(RESULT_OK or RESULT_CANCELLED)
(diffrent result when new items is added or user just back to prev screen).
Then in your ListAdapter class add method
public void updateData(List data) {
this.data = new ArrayList<>(data);
notifyDataSetChanged();
}
And call it in your onActivityResults(...)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == YOUR_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
adapter.updateData(...your data from result Intent, or from anywhere else...);
}
}
Also i strongly recommend you to use RecyclerView instead of ListView
UPD: to answer your question - i think that biggest mistake in your code, is to create new instance of ListAdapter object in onResume() method, when it's not necessary
How to refresh ListView dynamically?
adapter.notifyDataSetChanged();
You can call the above method to refresh list view any time. In your case call it after you delete a record from database.
How to Refresh Android List Adapter, so it shows new-added items
The solutions proposed thus far work, but are dependent on the Android version of your device. For example, to use the AddAll method you have to put android:minSdkVersion="10" in your android device.
To solve this questions for all devices I have created my on own method in my adapter and use inside the add and remove method inherits from ArrayAdapter that update you data without problems.
My Code: Using my own data class RaceResult, you use your own data model.
ResultGpRowAdapter.java
public class ResultGpRowAdapter extends ArrayAdapter<RaceResult> {
Context context;
int resource;
List<RaceResult> data=null;
public ResultGpRowAdapter(Context context, int resource, List<RaceResult> objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
this.data = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
........
}
//my own method to populate data
public void myAddAll(List<RaceResult> items) {
for (RaceResult item:items){
super.add(item);
}
}
ResultsGp.java
public class ResultsGp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...........
...........
ListView list = (ListView)findViewById(R.id.resultsGpList);
ResultGpRowAdapter adapter = new ResultGpRowAdapter(this, R.layout.activity_result_gp_row, new ArrayList<RaceResult>()); //Empty data
list.setAdapter(adapter);
....
....
....
//LOAD a ArrayList<RaceResult> with data
ArrayList<RaceResult> data = new ArrayList<RaceResult>();
data.add(new RaceResult(....));
data.add(new RaceResult(....));
.......
adapter.myAddAll(data); //Your list will be udpdated!!!
How refresh ListView?
I would suggest using RecyclerView instead of ListView. Also, you can create a method in your adapter which would take the list of data and set it and use notifydatasetchanged there.
How to refresh Android listview evry 10 seconds
Put your Volley request code inside a method, let's name id fetchData();
Then you can call your service again after 10 seconds by the below code:
final Handler mHandler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10000);
mHandler.post(new Runnable() {
@Override
public void run() {
// Write your code here to call the method.
fetchData();
}
});
} catch (Exception e) {
// TODO: handle exception here
e.printStackTrace();
}
}
}
}).start();
Try this and lo let me know if any problem.
Related Topics
Failed to Resolve: Com.Android.Support:Cardview-V7:26.0.0 Android
How to Start My App When the Phone Starts on Android
How to Detect Incoming Calls, in an Android Device
Android Saving File to External Storage
What APIs Are Used to Draw Over Other Apps (Like Facebook'S Chat Heads)
How to Get the External Sd Card Path For Android 4.0+
Set Color of Textview Span in Android
How to Prevent Android Taking a Screenshot When My App Goes to the Background
How to Create a Release Signed Apk File Using Gradle
Making Textview Scrollable on Android
Is There an Addheaderview Equivalent For Recyclerview
Service VS Intentservice in the Android Platform
Android Min Sdk Version Vs. Target Sdk Version
Onsaveinstancestate () and Onrestoreinstancestate ()