【问题标题】:display more than one column in listView with SimpleCursorAdapter使用 SimpleCursorAdapter 在 listView 中显示多列
【发布时间】:2017-05-05 19:22:03
【问题描述】:

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

我该怎么做?

顺便说一句,我的课程栏目是“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();

【问题讨论】:

  • 我还没有找到解决方案,伙计们..

标签: android listview simplecursoradapter


【解决方案1】:

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. 要将branche_cours 显示到TextView R.id.text_branche_coursdesignation 到TextView R.id.text_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

希望对你有帮助~

【讨论】:

  • 乐于助人:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
相关资源
最近更新 更多