如何使用一个游标从数据库中检索2列数据....现在我一次使用2个游标
ArrayList<String> sender_array = new ArrayList<String>();
ArrayList<String> body_array = new ArrayList<String>();
messagedb=ListView.this.openOrCreateDatabase("message",0, null);
messagedb.execSQL("CREATE TABLE IF NOT EXISTS tab2(sender INT(13),body varchar)");
Cursor cur1=mydb.rawQuery("select * from tab2", null);
while(cur.moveToNext())
{
String sender1=cur.getString(cur.getColumnIndex("sender"));
sender_array.add(sender1);
}
cur1.close();
Cursor cur2=mydb.rawQuery("select * from tab2", null);
while(cur.moveToNext())
{
String body1=cur.getString(cur.getColumnIndex("body"));
body_array.add(body1);
}
cur2.close();
messagedb.close();
如何使用一个光标弹出列表视图?
答案 0 :(得分:2)
Cursor cur=mydb.rawQuery("select sender, body from tab2", null);
while(cur.moveToNext())
{
String sender = cur.getString(0);
String body = cur.getString(1);
}
cur.close();
或者这更安全,如果您决定稍后更改列顺序
Cursor cur=mydb.rawQuery("select sender, body from tab2", null);
while(cur.moveToNext())
{
String sender = cur.getString(cur.getColumnIndex("sender"));
String body = cur.getString(cur.getColumnIndex("body"));
}
cur.close();
答案 1 :(得分:0)
使用一个光标,如下所示:
Cursor cur=mydb.rawQuery("select sender, body from tab2", null);
更新:
您不需要ArrayList,可以将Cursor直接加载到SimpleCursorAdapter并使用此适配器将数据加载到ListView
SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)
然后将适配器设置为ListView:
ListView.setAdapter(your_SimpleCursorAdapter)