添加单词用户词典和检索他们的字典背 [英] Add word to user dictionary and retrieve them back from dictionary

查看:178
本文介绍了添加单词用户词典和检索他们的字典背的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个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";
        }
    }
}

这篇关于添加单词用户词典和检索他们的字典背的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆