使用光标填充列表视图
Populate list view with cursor
我是 Android 开发的新手,请原谅我的天真。
我已经在 activity 上创建了一个列表视图:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView" />
我已经从我的 sqllite 中提取了所有日期 table:
public Cursor getAll()
{
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database.rawQuery("select * from todos",null);
return cursor;
}
(至少我想我有,我不确定如何测试那里是否有数据,数据库是否存在,table 是否存在等,因为我不知道如何查看数据库)。
如何使用 table 中的行填充列表视图?
简短的回答是您可以使用游标适配器。
看到这个post:Cursor adapter and sqlite example
这也可能有帮助:https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter
请注意,除了包含 ListView 的 xml 布局文件外,您还应该有一个 xml 布局文件来定义 ListView 中的每一行,这就是您要提供给您的CursorAdapter.
我是 Android 开发的新手,请原谅我的天真。
我已经在 activity 上创建了一个列表视图:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView" />
我已经从我的 sqllite 中提取了所有日期 table:
public Cursor getAll()
{
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database.rawQuery("select * from todos",null);
return cursor;
}
(至少我想我有,我不确定如何测试那里是否有数据,数据库是否存在,table 是否存在等,因为我不知道如何查看数据库)。
如何使用 table 中的行填充列表视图?
简短的回答是您可以使用游标适配器。
看到这个post:Cursor adapter and sqlite example
这也可能有帮助:https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter
请注意,除了包含 ListView 的 xml 布局文件外,您还应该有一个 xml 布局文件来定义 ListView 中的每一行,这就是您要提供给您的CursorAdapter.