添加单词用户词典和检索他们的字典背他们的、字典、单词、词典

2023-09-05 09:52:37 作者:27.來卜及の愛

我有几个edittexts在用户输入公司名称,客户名称,用途....各种各样的东西我的应用程序。现在,我想补充的话字典编程,因此他们不必重新输入整个单词每次,而不是字典应该建议的话,一旦他们开始输入。

I have few edittexts in my application where user enters company name, client name, purpose....kinds of things. Now I want to add those words to dictionary programmatically and thus they don't have to re-enter the whole word everytime, instead dictionary should suggest the word once they start typing.

我在网上搜索有关上同样和我有类似

I searched on the web regarding the same and I got something like

UserDictionary.Words.addWord(getActivity(), et_client_name.getText().toString(), 1, "", locale);

和我们需要给出两个权限的应用程序:

And we need to give two permissions to the app:

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

但我的问题是:有一次,我添加使用上述说法的话;如何检索它从字典中回来,并建议用户只要用户开始键入

But my problem is : Once I add words using the above statement; how to retrieve it back from dictionary and suggest user as soon as user starts typing in.

任何帮助或引用任何好的教程是AP preciated!

Any help or reference to any good tutorials is appreciated!

推荐答案

我设法找到了我的问题由我自己的解决方案....回答这个问题,希望它可以帮助别人作为同一种参考问题:

I managed to find solution of my question by my own....answering this question in the hope that it might help someone as a reference for the same kind of issue:

public class HomeActivity extends Fragment implements TextWatcher {

    String itemClientName[] = {};
    ArrayAdapter<String> clientNameAdapter;
    ArrayList<String> clientNameList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AutoCompleteTextView et_client_name = (AutoCompleteTextView) findViewById(R.id.et_client_name);
        getFromDictionary();
        suggestClientName();
        et_client_name.addTextChangedListener(this);
    }

    public void getFromDictionary() {
        System.out.println("Inside getFromDictionary");
        ContentResolver resolver = getActivity().getContentResolver();
        String[] projection = new String[]{BaseColumns._ID, UserDictionary.Words.WORD};
        cursor = resolver.query(UserDictionary.Words.CONTENT_URI, projection, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                long id = Integer.parseInt(cursor.getString(0));
                word = cursor.getString(1);
                // Prepare list for autoCompletion
                System.out.println("Inside prepareMyList" + word);
                if (!clientNameList.contains(word))
                    clientNameList.add(word);
                System.out.println("clientNameList::" + clientNameList);
                System.out.println("Word from dictionary is::" + word);
                // do something meaningful
            } while (cursor.moveToNext());
        }
    }

    public void suggestClientName() {
        String newAdd1 = et_client_name.getText().toString();
        if (!clientNameList.contains(newAdd1)) {
            clientNameList.add(newAdd1);
            // update the autocomplete words
            clientNameAdapter = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_dropdown_item_1line, clientNameList);
            et_client_name.setAdapter(clientNameAdapter);
        }
        // display the words in myList for your reference
        String s = "";
        for (int i = 0; i < clientNameList.size(); i++) {
            s += clientNameList.get(i) + "\n";
        }
    }
}
 
精彩推荐
图片推荐