Notifydatasetchanged Example

notifyDataSetChanged example

For an ArrayAdapter, notifyDataSetChanged only works if you use the add(), insert(), remove(), and clear() 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 you changed the List in the Activity.

Your choices are:

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

how to use notifyDataSetChanged()

Not exactly, but you're probably not actually updating the adapter's list. Make a method in your adapter that looks like this

public void updateItems(ArrayList<HashMap<String, String>> newList) {
list.clear();
list.addAll(newList);
this.notifyDataSetChanged();
}

Then call this from wherever you have the adapter set up

mAdapter.updateItems(newList)

notifyDataSetChanged() not working on custom RecyclerView adapter

Do not reset the reference to the data object. Instead try something like this:

MainActivity.data.clear();
MainActivity.data.addAll(dbh.ReadNotilist(this));
MainActivity.adapter.notifyDataSetChanged();

Set notifyDataSetChanged() on Recyclerview adapter

you are setting the new list to the RecyclerView Adapter , set the list in the Adapter:

make a method setItems(list) in adapter and call it before notifyDataSetChanged() and in adapter do

this.persons = new ArrayList<>(persons);

in setItems

add this method in adapter:

public void setItems(List<ServiceModel> persons) {
this.persons = persons;
}

and call it before notifyDataSetChanged() like this:

adapter.setItems(list);
adapter.notifyDataSetChanged();

notifydatasetchanged not working after app close?

I guess you are trying to use RecyclerView and populate it with your arrayAdapter.You are missing the statement to instruct the recycler view to use the adapter.

Try adding this and check if it works

recyclerView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();

If you are using any other views such as ListView you have to set your adapter to that.

listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();

If you are not using any views, you can't get your data populated. Please check this article for more on ArrayAdapter:
ArrayAdapter with ListView, ArrayAdapter with RecyclerView

Android notifyDataSetChanged not refreshing the items after removing

Try this one

itemsPending.remove(sp.keyAt(i));
adapterPending.remove(adapterPending.getItem(sp.keyAt(i)));
adapterPending.notifyDataSetChanged();

EDIT:

So basically the switching of list data is working fine. What's messing the code is his file writing. I resolve it this way.

First is a create a function that would update my Pending Files to submitted.

public void submitPendingProfile(String filename){
try {
BufferedReader file = new BufferedReader(new FileReader(path + "/" + filename+".txt"));
String line;
StringBuffer inputBuffer = new StringBuffer();

while ((line = file.readLine()) != null) {
inputBuffer.append(line);
inputBuffer.append('\n');
}
String inputStr = inputBuffer.toString();

file.close();


inputStr = inputStr.replace("--PENDING SUBMIT--", "--SUBMITTED--");

FileOutputStream fileOut = new FileOutputStream(path + "/" + filename+".txt");
fileOut.write(inputStr.getBytes());
fileOut.close();

} catch (Exception e) {
System.out.println("Problem reading file.");
}
}

Then i refactor the loop for simplier process. Like removing the line SubmittedProfile(); that keeps reading all text files if conditions is true. That is a lot of process. Here's how instead.

for(int i = lstPendingPro.getAdapter().getCount() - 1 ; i >= 0; i--) {
if (sp.get(i)) {
//So when file is submitted, i update the files status using the above function.
submitPendingProfile(itemsPending.get(i));

//To avoid rereading of files, just add the item before removing it to the pending list
itemsSubmit.add(itemsPending.get(i));
adapterSubmit.notifyDataSetChanged();

itemsPending.remove(sp.keyAt(i));
adapterPending.notifyDataSetChanged();
Toast.makeText(ProList.this, "Your profiles have been submitted successfully.", Toast.LENGTH_LONG).show();
}
}


Related Topics



Leave a reply



Submit