I’m developing a simple application in android. I have a list of categories (for now), which I would like to display as a list, and then (but this is far away) on click, start another activity with some functions… well, the situation:
my single_list_item.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Name Label -->
<TextView android:id="@+id/category_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:textColor="#43bd00"/>
<!-- Description Label -->
</LinearLayout>
In the main activity here is where I call the adapter and the layout:
List<categorie> values = datasource.getAllCategorie();
// Use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<categorie> adapter = new ArrayAdapter<categorie>(this,
R.layout.single_list_item, values);
setListAdapter(adapter);
In the Data source here is where I declare the getAllCategorie:
public List<categorie> getAllCategorie() {
List<categorie> categorie = new ArrayList<categorie>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_CATEGORIE,
allCategorieColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
categorie categoria = cursorToCategorie(cursor);
categorie.add(categoria);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return categorie;
}
and finally the categorie.class :
public class categorie {
private long id;
private String nome;
private long preferita;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public long getPreferita() {
return preferita;
}
public void setPreferita(long preferita) {
this.preferita = preferita;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return nome;
}
}
Currently, if I run the app, it freezes while starting the listview, it makes it completely empty. I would like to specify where to put each category element, such as the name, or the id or if it is “liked” (get and set Preferita in the categorie.class).
When I started, the adapter part was:
ArrayAdapter<categorie> adapter = new ArrayAdapter<categorie>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
and all was magically OK… why is using a default layout OK? again, where do I specify what goes where?
thanks in advance.
When you want to implement a simple list, there is no problem using a stock
ArrayAdapter. Android looks at the objects you supply to the adapter, callstoString()on those objects and populates aTextViewwithid=@android:id/text1with thatString. It worked “magically” when you usedandroid.R.layout.simple_list_item_1because that layout includes aTextViewwithid=@android:id/text1If you want more control over the layout of each row in your
ListView, you can use the SimpleAdapter which lets you map values to certainViewsin your row layout. Alternatively, you can write your own adapter class by extendingArrayAdapter. I recommend writing your own Adapter, as it is both fun and it gives some insight in how lists are populated. Here’s a guide on how to get started with that.