Android SQL数据库应用实践 “问题点”“疑难点”“解析”

应用 Android SQL 数据库时,遇到的问题:

场景1:Android SQL查询后,获取到Cursor并查询数据;遇到以下问题:"android.database.CursorIndexOutOfBoundsException: Index -1 requested"

D/Demo    (22249): [ContackPickerActivity] onItemClick::cursor.getCount()=70; position=1
D/Demo    (22249): [ContackPickerActivity] outUri.toString()=content://com.android.contacts/contacts/2
D/Demo    (22249): [ContactPickerTestActivity] onActivityResult::requestCode=1000
D/Demo    (22249): [ContactPickerTestActivity] onActivityResult::PICK_CONTACK running...
D/Demo    (22249): [ContactPickerTestActivity] onActivityResult::contackData=content://com.android.contacts/contacts/2
D/Demo    (22249): [ContactPickerTestActivity] ERROR is detected...android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

源代码如下:

        switch (requestCode) {
            case PICK_CONTACK:
                if (resultCode == Activity.RESULT_OK) {
                    LogUtil.d(TAG, "onActivityResult::PICK_CONTACK running...");
                    Uri contactData = data.getData();
                    LogUtil.d(TAG, "onActivityResult::contackData="
                            + contactData.toString());
                    Cursor cursor = getContentResolver().query(contactData,
                            null, null, null, null);
                    if (cursor != null) {
                        String name;
                        try {
                            name = cursor
                                    .getString(cursor
                                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
                            cursor.close();
                            TextView tv = (TextView) findViewById(R.id.selected_contact_textview);
                            tv.setText(name);
                        } catch (Exception e) {
                            e.printStackTrace();
                            LogUtil.d(TAG,
                                    "ERROR is detected..." + e.toString());
                        }
                    }
                }
                break;
            default:
                break;
        }

问题出在以下点:

name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));

在使用query()查询时,返回结果描述如下:

     * @return A Cursor object, which is positioned before the first entry, or null

Cursor指针指向的是查询结果的前一位置,在这之后调用的getColumnIndex()出现超出“边界”的异常。

解决办法:调用moveToFirst()即可

if (cursor != null && cursor.moveToFirst()) {
    String name;
    try {
        name = cursor
                .getString(cursor
                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
        cursor.close();
        TextView tv = (TextView) findViewById(R.id.selected_contact_textview);
        tv.setText(name);
    } catch (Exception e) {
        e.printStackTrace();
        LogUtil.d(TAG,
                "ERROR is detected..." + e.toString());
    }
}
posted @ 2017-02-10 22:25  jamesK4W  阅读(214)  评论(0编辑  收藏  举报