Content Provider to get UserDicationary returns nothing

非 Y 不嫁゛ 提交于 2019-12-11 12:15:21

问题


EDIT: I tried using this to add some words

UserDictionary.Words.addWord(getActivity().getApplicationContext(),"DROIDODD",11,null,null);

Now the cursor contains this word , but how do i access my other words in the personal dictionay , Where does android store the new words we enter through keyboards ? Is in in this datastore ,then why doesnt the dictionary content provider access those?

I am trying to get and display the words in the android user dictionary using a content resolver ,Problem is the returned cursor does not contain any values , the cursor count is 0 , I want get all the Words in the user dictionary and simply append it to a textVIew inside my fragment

The Fragment.java code is shown below :

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;
    }
}

My output is only The words in your personal dictionary are

My logcat is

 V/myLog﹕ Inside Oncreate
 V/myLog﹕ 0

Where am i going wrong , I am sure my phone dictionary has quite a few words, i tried it on multiple device , still the same . Please help!


回答1:


I have similar problem on my Samsung devices, it seems that Samsung (for some reason) has overridden the user dictionary... I installed google keyboard from the play store which has an option for adding words to the dictionary, when opening the user dictionary, I find it empty and pressing add words yield no response at all, except for the system sound :).

If you are like me, trying to experiment with content providers and found that user dictionary is an easy target, it is just as simple to switch to user contacts.

First change permissions so that you have access to Contacts instead

<uses-permission android:name="android.permission.READ_CONTACTS"/>

Then change your content resolver as follows:

Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int wordColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

This got me going through the rest of experimentation, or exercise..




回答2:


First we need to check weather our personal dictionary is empty or not.

I used this to get the results:

In your phone Find and tap Settings > Language & input > Personal dictionary > Add or the + (plus) icon. And add a word. Now try running the program or app.

The dictionary was empty, when i added some values, it returned results.

For different versions of android, see this page.

I used the above link for sony.

Similarly you can find how to add dictionary for other mobiles.



来源:https://stackoverflow.com/questions/31360301/content-provider-to-get-userdicationary-returns-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!