光标:获取字段值(安卓) [英] Cursor : get the field value (Android)

查看:202
本文介绍了光标:获取字段值(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有游标的问题在我的Andr​​oid应用程序。 我有一个SQLiteDatabase返回我的光标,当我尝试用获取的值:

I have problems with Cursor in my Android application. I have a SQLiteDatabase that return me Cursor when I try to fetch the values with :

public Cursor fetchOption(long rowId) throws SQLException {

    Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
        KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null,
        null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;

}

但我不知道如何获取光标的字段的值。 如果我这样做:

But I don't know how to obtain the value of the field in the Cursor. If I do that :

String a = mOptionDb.fetchOption(0).getColumnName(0).toString();
String b = mOptionDb.fetchOption(0).getColumnName(1).toString();
String c = mOptionDb.fetchOption(0).getColumnName(2).toString();

予得到的列(_id,标题,正文),但不是值的名称。 我该如何解决?

I obtain the name of the columns (_id, title, body) but not the value. How can I resolve that?

感谢你提前为您解答。 迈克尔

Thank you in advance for your answer. Michaël

推荐答案

我想你可以忘记检查空。

I think you can forget about checking for null.

相反,检查是否有数据,然后使用光标访问的列:

Instead check if there is data and then access the columns using the cursor:

Cursor cursor = fetchOption(0);

if (cursor.moveToFirst()) // data?
   System.out.println(cursor.getString(cursor.getColumnIndex("title")); 

cursor.close(); // that's important too, otherwise you're gonna leak cursors

这也可能是有意义的阅读机器人教程。记事本教程似乎符合这个要求:<一href="http://developer.android.com/guide/tutorials/notepad/index.html">http://developer.android.com/guide/tutorials/notepad/index.html

这篇关于光标:获取字段值(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆