Sometimes, It is needed to update a list of elements, for example for an agenda, a list of things to buy, etc.
For create a list we have to add to our xml layout the following:
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
and we can add entries by using code, for example the action fillData(), will take our own objects, in my case words and add them to the list by using an adapter.
private void fillData() {
//db.open(); open the database
words = db.fetchAllWords();
ArrayAdapter<Word> adapter= new ArrayAdapter<Word>(this, R.layout.entry, R.id.text1, words);
ListView lv = getListView();
lv.setAdapter(adapter);
//db.close();
}
your own objects, in my case Word needs to have overriten the toString method, the string that will be returned is the string used by the adapter, so the string which will be used for fullfill the list.
@Override
public String toString(){
return a+" -> "+b;
}
No comments:
Post a Comment