Difference between revisions of "Dynamic List Activity"
From John Freier
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | This is some example code to use ArrayList and attach it to the List view using the ArrayAdapter. | ||
− | public class TopActivity extends ListActivity | + | 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; | ||
private ArrayList<String> list; | private ArrayList<String> list; | ||
− | + | ||
public void onCreate(Bundle savedInstanceState) | public void onCreate(Bundle savedInstanceState) | ||
{ | { | ||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||
− | + | ||
this.list = new ArrayList<String>(); | this.list = new ArrayList<String>(); | ||
adapter = new ArrayAdapter<String>(this, R.layout.top, this.list); | adapter = new ArrayAdapter<String>(this, R.layout.top, this.list); | ||
setListAdapter(adapter); | setListAdapter(adapter); | ||
− | + | ||
//not for sure if I need the below. | //not for sure if I need the below. | ||
//ListView lv = getListView(); | //ListView lv = getListView(); | ||
//lv.setTextFilterEnabled(true); | //lv.setTextFilterEnabled(true); | ||
− | + | ||
addItem(); | addItem(); | ||
− | + | ||
} | } | ||
− | + | ||
public void addItem() | public void addItem() | ||
{ | { | ||
Line 27: | Line 32: | ||
} | } | ||
} | } | ||
+ | |||
+ | |||
+ | Your Layout should look like this... | ||
+ | <?xml version="1.0" encoding="utf-8"?> | ||
+ | <TextView xmlns:android="http://schemas.android.com/apk/res/android" | ||
+ | android:layout_width="fill_parent" | ||
+ | android:layout_height="fill_parent" | ||
+ | android:padding="10dp" android:textStyle="bold" android:textSize="20sp" android:textColor="#ffffff"> | ||
+ | </TextView> | ||
+ | |||
+ | '''Error''' - If you get the error, something along the lines of R.id.list is not in the content. Remove the setContentView method, you don't need it |
Latest revision as of 09:21, 6 July 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(); } }
Your Layout should look like this...
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textStyle="bold" android:textSize="20sp" android:textColor="#ffffff"> </TextView>
Error - If you get the error, something along the lines of R.id.list is not in the content. Remove the setContentView method, you don't need it