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 listview in a custom adapter?
Try this
Inside your onResume()
add this
if (listvew!= null) {
listvew.invalidateViews();
}
adapter.notifyDataSetChanged();
How to refresh listview after click imagebutton in listview item from adapter?
Simple call notifyDataSetChanged();
when you click in button for refresh your ListView
buttonHeart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
...
notifyDataSetChanged();
}
});
Refresh ListView from ArrayAdapter
You are instantiating a new adapter each time. What you have to do is put the line where you instantiate the adapter before the click listener, and in the click listener modify that adapter and call notifyDataSetChanged() on it. You of course have to add some setters to your adapter in order to modify the data.
Has to look similar to this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.config_hidden);
listView=(ListView) findViewById(R.id.hiddenList);
//instantiate the adapter (just one time)
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());
//assign the adapter to the listview
listView.setAdapter(adapter);
xmlFileManager=new XmlFileManager(this);
addNumber=(Button) findViewById(R.id.addNum);
addNumber.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
LayoutInflater factory = LayoutInflater.from(HiddenCall.this);
final View alertDialogView = factory.inflate(R.layout.add_number, null);
AlertDialog.Builder adb = new AlertDialog.Builder(HiddenCall.this);
adb.setView(alertDialogView);
adb.setTitle(R.string.dialog_title);
adb.setIcon(R.drawable.phone);
final AlertDialog alertDialog = adb.create();
adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
numberToAdd=(EditText) alertDialogView.findViewById(R.id.numberToAdd);
String number = numberToAdd.getText().toString();
if(number.length()>0){
xmlFileManager.addNumberToXml(number , HIDDEN_NUMBER_TYPE);
//adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());
//adapter.setNotifyOnChange(true);
//set the changed data
adapter.setData(xmlFileManager.getHiddenNumbers());
//notify that the model changed
adapter.notifyDataSetChanged();
}
} });
adb.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
} });
adb.show();
}
});
//adapter.notifyDataSetChanged();
//adapter.setNotifyOnChange(true);
Related Topics
Error Inflating Class Androidx.Constraintlayout.Constraintlayout After Migration to Androidx
How to Open the Options Menu Programmatically
Android Set Button Background Programmatically
How to Disable All Views Inside the Layout
How to Draw Outside the Bounds of an Android Canvas
How to Pass a File Path Which Is in Assets Folder to File(String Path)
Aidl Is Missing Android Studio
What Are the Android Sdk Build-Tools, Platform-Tools and Tools? and Which Version Should Be Used
Unsigned APK Can Not Be Installed
Android E/Parcel﹕ Class Not Found When Unmarshalling (Only on Samsung Tab3)
How to Automatically Uninstall Android App from Device Before Installing a New Version
Android Studio Build Fails with "Task '' Not Found in Root Project 'Myproject'."
Crashlytics Android Sdk - Custom Uncaughtexceptionhandler
Android: How to Maximize Preferencefragment Width (Or Get Rid of Margin)