使用 SimpleCursorAdapter 在 listView 中显示多列

display more than one column in listView with SimpleCursorAdapter

我有一个用 SimpleCursorAdapter 填充的 ListView。列表视图显示我的数据库的一列的内容。该专栏只是我在数据库中创建的课程(如英语、数学)。我还有每节课的主题(比如阅读写作……)。而那也是同table的一列。在我的 listView 中它只显示 "English",但我想显示 "English - Reading",所以我可以有所作为。

我该怎么做?

顺便说一句,我的课程专栏是 'branche_cours',我想显示的另一栏是 'designation'。

这是我的 SimpleCursorAdapter

     lvCours =(ListView)findViewById(R.id.ListCours);
    final Cursor cursor = dbhelper.getAllCours();
    String[] from = { "branche_cours" }; int[] to = { android.R.id.text1 };
    adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to, 0);
    lvCours.setAdapter(adapter);
    adapter.notifyDataSetChanged();

1. 为您的行项目创建布局 list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <TextView
        android:id="@+id/text_branche_cours"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text="English"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text=" - " />

    <TextView
        android:id="@+id/text_designation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text="Reading" />
</LinearLayout>

2. 要向 TextView R.id.text_branche_cours 显示 branche_cours 和向 TextView R.id.text_designation 显示 designation,请在您的 Activity:

lvCours = (ListView)findViewById(R.id.ListCours);
final Cursor cursor = dbhelper.getAllCours();

String[] from = { "branche_cours", "designation" }; 
int[] to = { R.id.text_branche_cours, R.id.text_designation };

adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to, 0);
lvCours.setAdapter(adapter);
adapter.notifyDataSetChanged();

这是一个很好的Tutorial

希望对你有所帮助~