16

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?

tr_quest
  • 735
  • 2
  • 10
  • 24
  • This would be clearer if you supplied the source for CustomAdap and Monday, as well as where Monday is declared. – louielouie Mar 17 '12 at 05:51
  • i have added code for customadap and monday is an Arraylist of SClass – tr_quest Mar 17 '12 at 05:58
  • @TariqIbrahim can you see http://stackoverflow.com/questions/28148618/listview-not-refreshing-after-click-on-button –  Jan 27 '15 at 13:01

4 Answers4

19

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();
louielouie
  • 14,881
  • 3
  • 26
  • 31
  • My aim to use temp is because,i have different arraylists,monday tuesday etc.i'd like to copy the display different contents on different days,which is why i use one arraylist temp.and temp can be assigned the values from any of the arraylists monday,tuesday etc. – tr_quest Mar 17 '12 at 06:19
  • there is no method called addAll – tr_quest Mar 17 '12 at 06:50
  • 2
    Your CustomAdap will have to subclass ArrayAdapter, which includes `clear` and `addAll`. Something like `public class CustomAdap extends ArrayAdapter`. Please see http://developer.android.com/reference/android/widget/ArrayAdapter.html for reference. – louielouie Mar 17 '12 at 06:53
  • i could use a for loop.but that adds to the length of my code .any similar method to AddAll will help :) – tr_quest Mar 17 '12 at 06:57
  • my custom adapter extends ArrayAdapter – tr_quest Mar 17 '12 at 06:57
  • there are SOOO many people saying how to do this using runnables, threads, etc.. but this is the easiest/best way. Thanks! – reidisaki Aug 04 '14 at 06:25
  • Thank you so much ,You save my day. – Payal Sorathiya Feb 23 '17 at 05:49
5

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.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
3

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

lastshadowrider
  • 103
  • 1
  • 9
3

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.

Shubhayu
  • 13,402
  • 5
  • 33
  • 30