如何动态地添加建议autocompletetextview以preserving人物状态状态、人物、建议、动态

2023-09-11 10:24:31 作者:歪腻I

让我解释一下我的问题。

Let me explain my question.

Prbolem说明:

我现在面临一些问题,AutoCompleteTextView在那里我必须证明后,每个按键preSS建议。 事情是这样的,建议名单是动态的,就像谷歌的建议功能。 这意味着为用户保持键入加上所有匹配的旧的建议,应该会显示新建议,应添加。

I am facing some problem with AutoCompleteTextView where i have to show suggestions after each keypress. Thing is that,list of suggestion is dynamic like google's suggestion feature. It means the new suggestions should be added as user keeps typing in plus all matching old suggestions should be displayed.

例如

我写的德,然后它应该显示类似测试1和放previous建议; TEST2和新的建议,我会得到来自网络API.Suppose网络API给了我茶字和放大器; 紧张。

I write "te" and then it should display previous suggestions like "test1" & "test2" and the new suggestions that i will get from Web API.Suppose web api gives me word "tea"& "tension ".

现在的AutoCompleteTextView将有德作为字符串的所有四点建议表示它下面。

Now the AutoCompleteTextView will have "te" as string with all four suggestions showing below it.

这是我在寻找什么。

看起来很简单,但它是呈现出奇怪的行为。

looks simple but it is showing a strange behaviour.

我使用默认的一个ArrayAdapter类的实例,其中我宣布在全球范围

I am using default ArrayAdapter class instance of which i am declaring globally.

arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,suggestions);
word.setAdapter(arrayAdapter);

建议是ArrayList的。

suggestions is ArrayList.

一旦获得新的结果从的WebAPI我只需拨打

Upon getting new result from WebApi i simply call

arrayAdapter.notifyDataSetChanged();

刷新附有此数据观察和观点(在我们的例子AutoCompleteListView)。

to refresh the data observer and views attached with this ( in our case AutoCompleteListView).

但关闭的建议。

当我不使用 notifyDataSetChanged(); 它显示,无论人物我已经输入的所有建议

When i don't use notifyDataSetChanged(); it is showing all suggestions regardless of characters i have typed.

我试图用自定义过滤器的许多建议,但他们都不是有帮助的,因为我无法用notifyDataSetChanged()。

I tried it with custom filter as many suggested but none of them is helpful as i couldn't use notifyDataSetChanged().

我张贴的图像,以免混淆。

I am posting an image to avoid confusions.

我有一个困惑就是为什么 notifyDataSetChanged();如果它不是working.I没有使用列表相同arrayAdapter instance.I其他任何参考真的怀疑其基准的问题。

I have a confusion that why notifyDataSetChanged(); its not working.I haven't use any other reference of list with same arrayAdapter instance.I really doubt if its a reference problem.

任何一种技巧或窍门将AP preciated。 让我知道如果我失去了一些东西。 谢谢你。

Any kind of tips or tricks will be appreciated. Let me know if i am missing something. Thanks.

推荐答案

中这样做的最简单的方法之一(把code中的onCreate):

one of the easiest way of doing that (put the code in onCreate):

编辑:addied维基百科自由的OpenSearch(如果 https://en.wikipedia.org 不起作用尝试 http://en.wikipedia.org )

addied wikipedia free opensearch (if https://en.wikipedia.org doesn't work try http://en.wikipedia.org)

    AutoCompleteTextView actv = new AutoCompleteTextView(this);
    actv.setThreshold(1);
    String[] from = { "name", "description" };
    int[] to = { android.R.id.text1, android.R.id.text2 };
    SimpleCursorAdapter a = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, null, from, to, 0);
    a.setStringConversionColumn(1);
    FilterQueryProvider provider = new FilterQueryProvider() {
        @Override
        public Cursor runQuery(CharSequence constraint) {
            // run in the background thread
            Log.d(TAG, "runQuery constraint: " + constraint);
            if (constraint == null) {
                return null;
            }
            String[] columnNames = { BaseColumns._ID, "name", "description" };
            MatrixCursor c = new MatrixCursor(columnNames);
            try {
                String urlString = "https://en.wikipedia.org/w/api.php?" +
                        "action=opensearch&search=" + constraint +
                        "&limit=8&namespace=0&format=json";
                URL url = new URL(urlString);
                InputStream stream = url.openStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
                String jsonStr = reader.readLine();
                // output ["query", ["n0", "n1", ..], ["d0", "d1", ..]]
                JSONArray json = new JSONArray(jsonStr);
                JSONArray names = json.getJSONArray(1);
                JSONArray descriptions = json.getJSONArray(2);
                for (int i = 0; i < names.length(); i++) {
                    c.newRow().add(i).add(names.getString(i)).add(descriptions.getString(i));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return c;
        }
    };
    a.setFilterQueryProvider(provider);
    actv.setAdapter(a);
    setContentView(actv, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));