从 SQLite 数据库制作列表视图
我正在尝试从我的 SQLite 数据库填充 listview...这就是我从数据库获取数据的方式:
Cursor c = database.rawQuery("SELECT * FROM " + TableName, null);
int Column1 = c.getColumnIndex("uri");
int Column2 = c.getColumnIndex("file");
int Column3 = c.getColumnIndex("id");
c.moveToFirst();
if (c != null) {
do {
String uri = c.getString(Column1);
String file = c.getString(Column2);
int id = c.getInt(Column3);
} while (c.moveToNext());
}
我通常会像这样向 listview 添加一个数组:
ListView my_listview2 = (ListView) findViewById(R.id.listView1);
String my_array[] = {"Android", "iPhone"};
my_listview2.setAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.my_custom_row, my_array));
如何从我的 sql 查询中创建一个用于 setadapter 的数组?
I'm trying to populate listview from my SQLite database... this is how I get my data from database:
Cursor c = database.rawQuery("SELECT * FROM " + TableName, null);
int Column1 = c.getColumnIndex("uri");
int Column2 = c.getColumnIndex("file");
int Column3 = c.getColumnIndex("id");
c.moveToFirst();
if (c != null) {
do {
String uri = c.getString(Column1);
String file = c.getString(Column2);
int id = c.getInt(Column3);
} while (c.moveToNext());
}
I would normally add an array to listview like that:
ListView my_listview2 = (ListView) findViewById(R.id.listView1);
String my_array[] = {"Android", "iPhone"};
my_listview2.setAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.my_custom_row, my_array));
How can I make an array to setadapter from my sql query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行此操作的最佳方法是使用 CursorAdapter 或 SimpleCursorAdapter。这将为您提供最佳性能,一旦您弄清楚了,您就会发现这是使用 SQLite 数据库时最简单的方法。
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
下面是我经常使用的一个简单的 CustomCursorAdapter。只需将 CustomCursorAdapter 类添加为内部类即可。
像这样创建 CustomCursorAdapter 的实例...
您需要像您已经做的那样创建光标。
The best way to do this is to use a CursorAdapter or a SimpleCursorAdapter. This will give you the best performance and once you figure it out you'll find it's the simplest approach when using a SQLite db.
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
Below is a simple CustomCursorAdapter that I use frequently. Just add the CustomCursorAdapter class as an inner class.
Create an instance of the CustomCursorAdapter like so...
You'll need to create your cursor just like you're already doing.
我发现使用记事本教程对于了解这一点非常有用。
它向您展示了如何通过非常简单的步骤使用 sqlite 数据库实现列表视图。
http://developer.android.com/resources/tutorials/notepad/index.html
I found working with the notepad tutorial very useful for learning about this.
It shows you how to implement the listview using the sqlite database in very easy steps.
http://developer.android.com/resources/tutorials/notepad/index.html