嗨,我有一个SQLite数据库,我使用一个简单的游标来读取数据库,然后将其显示给TextView。但是,我想在ListView中显示数据。
所以我有字符串,但我不知道如何在ListView中显示它。有一个简单的方法或我将不得不创建各种适配器?
我还想知道如何将列表视图的一部分设置为标题,将其他部分设置为子标题。我想要它,以便每个行都是不同的列表项,如果可能的话。
答案 0 :(得分:1)
这是一个简单的例子。假设您的主要活动中有两个名为“Items”和“ItemsId”的数组列表。 现在,在您的数据库处理程序类中,使用下面的代码获取表并将值存储到Items和ItmesId数组列表中。
MainActivity.Items.clear();
MainActivity.ItemsId.clear();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
// Adding values to list
MainActivity.Items.add(cursor.getString(0));
MainActivity.ItemsId.add(cursor.getString(0));
} while (cursor.moveToNext());
}
现在使用主要活动中的项目数组列表填充列表视图。
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, Items);
listview.setAdapter(adapter);
答案 1 :(得分:0)
这是一个简单的例子:
yourListView.setAdapter(new SimpleCursorAdapter(
/* The context where the ListView associated with this SimpleListItemFactory is running */,
/* resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to" */,
/* The database cursor. Can be null if the cursor is not available yet. */,
/* A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet. */,
/* The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet. */,
/* Flags used to determine the behavior of the adapter, as per CursorAdapter(Context, Cursor, int). */));