我的应用无法从People.CONTENT_URI获取任何内容。 插入成功,结果为:“URI = content:// contacts / people / 15” 光标打印为:
"cursor=>>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@d1caa72
<<<<<
position=-1
我在查询后尝试使用游标moveToFirst,结果日志显示:
cursor=>>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@d1caa72
<<<<<
position=0
代码段:
import android.provider.Contacts.People;
final ContentResolver mResolver = mContext.getContentResolver();
mResolver.delete(People.CONTENT_URI, null, null);
final ContentValues valuse = new ContentValues();
valuse.put(People._ID, "1");
valuse.put(People.NAME, "name");
Uri uri = mResolver.insert(People.CONTENT_URI, valuse);
Log.d("MyApp","preparePeople: URI=" +uri);
private final String[] mPROJECTION = new String[] {
People._ID, People.NAME
};
final Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, mPROJECTION, null, null, null);
Log.i("MyApp","cursor=" + DatabaseUtils.dumpCursorToString(c) + " position=" + c.getPosition());
请告诉我我的代码段有什么问题。光标输出未显示插入的任何值。
答案 0 :(得分:0)
您正在从光标中获取数据,但您没有告诉光标从哪个位置获取,默认光标指向-1位置。
所以,你必须使用,
c.moveToFirst();
获取光标后。