我正在android中开发一个简单的应用程序。我有一个类别列表(现在),我想将其显示为列表,然后(但这很远)单击,开始另一个具有某些功能的活动......好吧,情况:
我的 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>
在主要活动中,我在这里调用适配器和布局:
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);
在数据源中,我在这里声明 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;
}
最后是 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;
}
}
目前,如果我运行该应用程序,它会在启动列表视图时冻结,使其完全为空。我想指定每个类别元素的放置位置,例如名称、id 或者是否“喜欢”(在 categorie.class 中获取并设置 Preferita)。
当我开始时,适配器部分是:
ArrayAdapter<categorie> adapter = new ArrayAdapter<categorie>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
一切都神奇地好……为什么使用默认布局好?再次,我在哪里指定什么去哪里?
提前致谢。