Android List View Refresh

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.

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.

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.

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

How to refresh listview when click an item on listview

Your choices are:

  • Use the functions of the ArrayAdapter to modify the underlying List (add, insert, remove, clear, etc.)
  • Re-create the ArrayAdapter with the new List data. (Uses a lot of resources and garbage collection.)
  • Create your own class derived from BaseAdapter and ListAdapter that allows changing of the underlying List data structure.
  • Use the notifyDataSetChanged every time the list is update. To call it on the UI-Thread use the runOnUiThread method of the Activity. Then notifyDataSetChanged will work.

Example

final ArrayAdapter adapter = ((ArrayAdapter)getListAdapter());
runOnUiThread(new Runnable() {

public void run() {
adapter.notifyDataSetChanged();
}
});


Related Topics



Leave a reply



Submit