在单击建议时停止AutoCompleteTextView扩展文本

时间:2015-02-07 14:05:16

标签: android android-asynctask autocompletetextview

我有AutoCompleteTextView我每次输入新信件时都会过滤。过滤过程包含一个后台进程(runQuery方法),因为我从外部数据库接收数据需要一点时间。这意味着在输入或删除新字母后,列表(array_name,array_id)将使用新数据刷新,这是我在下拉菜单中显示的内容。

默认情况会怎样?过滤器运行,runQuery查询我需要的数据,列表正确填充,当我点击其中一个建议时,它工作正常。

但是,当我点击建议时,AutoCompleteTextView会使我的代码在FilterProvider中再次运行,因为它会将文本扩展到建议中,并且会将其视为文本更改。这意味着runQuery将再次运行并在执行单击之前立即删除列表(array_name,array_id)。这仅在我在第二个runQuery完成之前单击建议时执行。参见:

是否有可能以某种方式禁用AutoCompleteTextView的此功能,以便在我点击建议时它不会扩展文本,或者您是否有任何其他建议可以解决此问题?

       actv_name = (AutoCompleteTextView) findViewById(R.id.actv_name);
                actv_name.setThreshold(3);
                String[] from = { "name","bid" };
                int[] to = { android.R.id.text1 };
                a = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to, 0);
                a.setStringConversionColumn(1);

                FilterQueryProvider provider = new FilterQueryProvider() {

                   @Override
                    public Cursor runQuery(final CharSequence constraint) {

                    Log.d("TAG", "runQuery constraint: " + constraint);
                    if (constraint == null) {
                        return null;
                    }

                    array_name.clear();
                    array_id.clear();

                    try {

                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = null;
                        httppost = new HttpPost(search_goal);

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                        nameValuePairs.add(new BasicNameValuePair("newtext", constraint.toString()));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        is = entity.getContent();
                        }catch(Exception e){
                                Log.e("error", "Error in http connection "+e.toString());
                    }
                        //convert response to string
                        try{
                                BufferedReader reader = new BufferedReader( new InputStreamReader(is,"iso-8859-1"),8);
                                StringBuilder sb = new StringBuilder();
                                String line = null;
                                while ((line = reader.readLine()) != null) {
                                        sb.append(line + "\n");
                                }
                                is.close();
                                //Log.i("sb", sb + "");
                                Bresult=sb.toString();
                                Log.i("Bresult", Bresult + "");
                        }catch(Exception e){
                                Log.e("error", "Error converting result "+e.toString());
                        }


                        c = new MatrixCursor(columnNames);

                        try {
                            if (Bresult != null) {
                                jArray = new JSONArray(Bresult);
                                for(int i=0;i<jArray.length();i++){
                                    JSONArray innerJsonArray = jArray.getJSONArray(i);
                                    for(int j=0;j<innerJsonArray.length();j++){
                                        JSONObject jsonObject = innerJsonArray.getJSONObject(j);
                                        array_name.add(jsonObject.getString("NAME"));
                                        array_id.add(jsonObject.getString("ID"));
                                        c.newRow().add(j).add(jsonObject.getString("NAME")).add(jsonObject.getString("ID"));
                                    }
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    for (int j=0; j<array_id.size(); j++) {
                        Log.d("ACTV LOG", array_id.get(j) + ", " + array_name.get(j));

                    }
                    Log.i("ASYNC C size", c.getCount() + "");

                    return c;
                }
            };

            a.setFilterQueryProvider(provider);
            actv_name.setAdapter(a);

OnClickListener:

            actv_name.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick (AdapterView<?> parent, View view, int position, long id) {

                    Intent intent = new Intent(NewActivity.this, ProfileActivity.class);
                    intent.putExtra("id", array_id.get(position));
                    startActivity(intent);
                    actv_name.setText("");
                }
            });

0 个答案:

没有答案