Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listview not updating with notifydatasetchanged() call

This is my code

listview =(ListView) findViewById(R.id.lv1);


    ArrayList<SClass> Monday = new ArrayList<SClass>();

    SClass s1=new SClass();
    s1.sName="samp";
    s1.salary=1000;
    Monday.add(s1);
    temp=Monday;
    adapter = new CustomAdap(this, temp);
    listview.setAdapter(adapter);

The above code works fine.But when i change my code to this

    listview =(ListView) findViewById(R.id.lv1);


    adapter = new CustomAdap(this, temp);

    SClass s1=new SClass();
    s1.sName="samp";
    s1.salary=1000;
    Monday.add(s1);
    temp=Monday;

    listview.setAdapter(adapter);
    adapter.notifyDataSetChanged();

Listview does not show anything.what is the problem?

like image 545
tr_quest Avatar asked Mar 17 '12 05:03

tr_quest


People also ask

How can you update a ListView dynamically?

This example demonstrates how do I dynamically update a ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What happens when we call notifyDataSetChanged?

From the docs notifyDataSetChanged() : Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

How do I refresh a ListView adapter?

To refresh the ListView in Android, call notifyDataSetChanged() method on the Adapter that has been set with the ListView. Also note that the method notifyDataSetChanged() has to be called on UI thread.

How do I refresh ArrayAdapter?

You can modify existing adapter data and call notifyDataSetChanged(). In your case you should call listView. setAdapter(adapter) in onClick method.


4 Answers

It looks like you're changing the collection that you initialized adapter with. I would change your code in this way:

// initial setup
listview =(ListView) findViewById(R.id.lv1);
ArrayList<SClass> Monday = new ArrayList<SClass>();
adapter = new CustomAdap(this, Monday);
listview.setAdapter(adapter);

// change your model Monday here, since it is what the adapter is observing
SClass s1=new SubjectClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);

// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();

Note that if your CustomAdap was a subclass of ArrayAdapter, you could also have done

// change your array adapter here
SClass s1=new SubjectClass();
s1.sName="samp";
s1.salary=1000;
adapter.add(s1);

// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();

EDIT: I understand more what you want to do now thanks to your comment. You'll probably want to have the adapter replace its contents with that your different ArrayLists then. I would make your CustomAdap be a subclass of ArrayAdapter.

Then you can utilize it this way:

// replace the array adapters contents with the ArrayList corresponding to the day
adapter.clear();
adapter.addAll(MONDAY);

// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
like image 123
louielouie Avatar answered Nov 04 '22 00:11

louielouie


Why it works in first code ?

--- Because you are setting the values to temp List and passing it the adapter and it shows it into listview.

Why not work in second code ?

--- Because you are setting temp to adapter far before you set value into temp
second,your adapter class might not getting the updated value when you set new value to temp ..that because temp is not public or not at class level or not static.. Put the temp declaration at root level and try.

And please show your full code as much as required and Logcat if you getting any warnings than also.

like image 25
MKJParekh Avatar answered Nov 03 '22 23:11

MKJParekh


Check for a link to your referenced view in the proper xml file. Or at least check for the existence of said xml file.

like image 20
lastshadowrider Avatar answered Nov 03 '22 22:11

lastshadowrider


What adapter are you using? It is clearly a case where your adapter is not getting updated after u set the data in your temp variable.

like image 36
Shubhayu Avatar answered Nov 03 '22 23:11

Shubhayu