Cursor的使用

Android1.5读取联系人数据时,需要用到Cursor,昨天开始时怎么也读不到数据,代码如下

Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null); this.startManagingCursor(c); Log.i("Name", c.getString(c.getColumnIndex(People.NAME))); 

此时编译器会报异常android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3

 

后来才知道需要调用Cursor的一个方法moveToFirst(),将Index移动到第一位上才可以。

 

修改代码如下,则运行正常,输出联系人的姓名

Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null); this.startManagingCursor(c); c.moveToFirst(); Log.i("Name", c.getString(c.getColumnIndex(People.NAME))); 

 

 

 

你可能感兴趣的:(c,android,null,编译器)