Difference between revisions of "Dynamic List Activity"

From John Freier
Jump to: navigation, search
Line 1: Line 1:
  public class TopActivity  extends ListActivity  
+
This is some example code to use ArrayList and attach it to the List view using the ArrayAdapter.
 +
 
 +
One thing I didn't know, is you could use ArrayList.
 +
 
 +
When you change the data in the ArrayList use adapter.notifyDataSetChanged() to update the list.
 +
 
 +
  public class TopActivity  extends ListActivity
 
  {
 
  {
 
       private ArrayAdapter<String> adapter;
 
       private ArrayAdapter<String> adapter;

Revision as of 10:41, 2 February 2011

This is some example code to use ArrayList and attach it to the List view using the ArrayAdapter.

One thing I didn't know, is you could use ArrayList.

When you change the data in the ArrayList use adapter.notifyDataSetChanged() to update the list.

public class TopActivity  extends ListActivity
{
     private ArrayAdapter<String> adapter;
     private ArrayList<String> list;

     public void onCreate(Bundle savedInstanceState) 
     {
          super.onCreate(savedInstanceState);

          this.list = new ArrayList<String>();
          adapter = new ArrayAdapter<String>(this, R.layout.top, this.list);
          setListAdapter(adapter);

          //not for sure if I need the below.
          //ListView lv = getListView();
          //lv.setTextFilterEnabled(true);

          addItem();

     }

     public void addItem()
     {
          this.list.add("Two");
          adapter.notifyDataSetChanged();
     }
}