【发布时间】:2017-09-15 15:30:23
【问题描述】:
我尝试使用 ContentResolver 从表中获取数据:UserDictionary.Words。
无论我做什么,游标中都没有检索到任何行,所以我一直为 cursor.getCount() 获取 0。
我将所需的权限放在清单文件中:
使用权限 android:name="android.permission.READ_CONTACTS"
使用权限 android:name="android.permission.WRITE_CONTACTS"
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ContentResolver resolver = getContentResolver();
String[] projection = new String[]{BaseColumns._ID, UserDictionary.Words.WORD};
Cursor cursor =
resolver.query(UserDictionary.Words.CONTENT_URI,
projection,
null,
null,
null);
if (cursor.moveToFirst()) {
do {
long id = cursor.getLong(0);
String word = cursor.getString(1);
// do something meaningful
} while (cursor.moveToNext());
}
}
我应该怎么做才能解决这个问题?
【问题讨论】:
-
您是否在运行时请求所有正确的权限?
-
你能用安卓应用ContentProviderHelper打开
UserDictionary.Words吗?在我的 android-4.4 上它可以查询UserDictionary.Words