-1

I am stuck due to this.... as every ones problem (I think) is that.

I set a listview onCreate method of activity & setAdapter according to it hence I can't refresh a LISTVIEW as Activity created one time Only.

So please guide me if anyone finds the correct ANSWER.. please

Philipp Jahoda
  • 47,594
  • 21
  • 164
  • 175
Abhijeet
  • 362
  • 2
  • 12

1 Answers1

0

You can call notifyDataSetChanged() on the Adapter of your ListView, or set a new Adapter for your ListView somewhere else in code.

Please also have a look at this post for specific information:

Android: how to refresh ListView contents?

Taken from the post above:

// this could be inside your oncreate method:
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(defaultAdapter);  // the defaultAdapter is the ListAdapter you first added to the listview, its a member-variable

// this will refresh the adapter and therefore refresh the listView
public void refreshListViewContent() {

    List<Item> newItems = getNewListViewItems(); // get the new listview items from somewhere

    defaultAdapter.clear();  // clear your adapter
    defaultAdapter.addAll(newItems); // add the new items
    defaultAdapter.notifyDataSetChanged();  // do the actual refresh
}

The method refreshListViewContent() can be called from anywhere in your code, for example when you click a Button or something like that:

Button button = (Button) findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {

       public void onClick(View v) {

           refreshListViewContent();
       }
 });
Community
  • 1
  • 1
Philipp Jahoda
  • 47,594
  • 21
  • 164
  • 175
  • where did i call notifyDataSetChanged() at the time of setting adapter but its already set on create i am setting listview on my first layout – Abhijeet Aug 16 '13 at 15:37