编辑:我试着用它来添加一些词
UserDictionary.Words.addWord(getActivity().getApplicationContext(),"DROIDODD",11,null,null);
现在光标包含这个词,但是我如何访问个人词典中的其他词, android 在哪里存储我们通过键盘输入的新词? 在这个数据存储中,那么为什么字典内容提供者不访问那些?
我正在尝试使用内容解析器获取并显示 android 用户词典中的单词,问题是返回的游标不包含任何值,游标计数为 0, 我想获取用户词典中的所有单词,并将其简单地附加到我的 fragment 中的 textVIew
Fragment.java代码如下:
package com.sweetapps.managephonedictionary;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.UserDictionary;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
private String details="The words in your personal dictionary are \n\n";
public MainActivityFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("myLog","Inside Oncreate");
//content provider code
ContentResolver contentResolver= getActivity().getApplicationContext().getContentResolver();
//get all details
Cursor cursor=contentResolver.query(UserDictionary.Words.CONTENT_URI,null,null,null,null );
Log.v("myLog",cursor.getCount()+"");
while (cursor.moveToNext())
{
details=details+cursor.getString(cursor.getColumnIndex(UserDictionary.Words.WORD));
Log.v("myLog",cursor.getString(cursor.getColumnIndex(UserDictionary.Words.WORD)));
}
cursor.close();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_main, container, false);
TextView textView= (TextView) view.findViewById(R.id.wordsTextView);
textView.setText(details);
return view;
}
}
我的输出只有你个人字典里的单词是
我的日志是
V/myLog﹕ Inside Oncreate
V/myLog﹕ 0
我哪里出错了,我确定我的手机词典有很多单词,我在多个设备上试过了,还是一样。 请帮忙!
最佳答案
我在我的三星设备上遇到了类似的问题,三星(出于某种原因)似乎已经覆盖了用户字典...我从 Play 商店安装了谷歌键盘,打开时可以选择向字典添加单词用户词典,我发现它是空的,按添加词根本没有任何反应,除了系统声音:)。
如果您像我一样尝试使用内容提供者进行试验并发现用户字典是一个简单的目标,那么切换到用户联系人也同样简单。
首先更改权限,以便您可以访问联系人
<uses-permission android:name="android.permission.READ_CONTACTS"/>
然后按如下方式更改您的内容解析器:
Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int wordColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
这让我完成了其余的实验或练习..
关于java - 获取 UserDicationary 的内容提供者不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31360301/