嗨,我有一个SQLite数据库,我用一个简单的游标读取数据库,然后将其显示给TextView。但是,我想在ListView中显示数据。在ListView中显示SQLite
所以我有字符串,但我不知道如何在ListView中显示它。有没有简单的方法,还是我必须创建一个适配器?
我还想知道如何设置列表视图的部分到标题和其他人的子标题。如果可能的话,我希望每行都是不同的列表项。
嗨,我有一个SQLite数据库,我用一个简单的游标读取数据库,然后将其显示给TextView。但是,我想在ListView中显示数据。在ListView中显示SQLite
所以我有字符串,但我不知道如何在ListView中显示它。有没有简单的方法,还是我必须创建一个适配器?
我还想知道如何设置列表视图的部分到标题和其他人的子标题。如果可能的话,我希望每行都是不同的列表项。
下面是一个简单的例子:
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). */));
下面是一个简单example.Lets说你有两个数组列表命名为您的主要活动“项目”和“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());
}
现在使用主活动中的Items数组列表填充您的列表视图。
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, Items);
listview.setAdapter(adapter);
听起来像你想[SimpleCursorAdapter](http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html)。如果你发布你的表格模式和你想显示的列,我会更好地理解你的问题。 – Sam 2012-07-16 20:41:49
你试过了吗? – 2012-07-16 20:43:00
我只想显示我的数据库中的两列。 – user1261404 2012-07-16 20:49:36