Android 单击“建议”停止自动完成文本视图扩展文本

Android 单击“建议”停止自动完成文本视图扩展文本,android,android-asynctask,autocompletetextview,Android,Android Asynctask,Autocompletetextview,我有一个AutoCompleteTextView,每次键入新字母时都会对其进行过滤。过滤过程包含一个回溯过程(runQuery方法),因为我从外部数据库接收数据,这需要一点时间。这意味着在键入或删除新字母后,列表(array_name、array_id)将用新数据刷新,这就是我在下拉菜单中显示的内容 默认情况下会发生什么?过滤器运行,runQuery查询我需要的数据,列表正确填写,当我点击其中一个建议时,它工作正常 但是,当我单击某个建议时,AutoCompleteTextView会使我的Fil

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

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

但是,当我单击某个建议时,
AutoCompleteTextView
会使我的
FilterProvider
中的代码再次运行,因为它将文本扩展到建议中,并将其视为文本更改。这意味着runQuery将再次运行,并在执行单击之前立即删除列表(array\u name、array\u id)。只有在第二次运行查询完成之前单击建议时,才会执行此操作。见:

是否有可能以某种方式禁用
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);
actv_name=(AutoCompleteTextView)findviewbyd(R.id.actv_name);
actv_名称设置阈值(3);
字符串[]from={“name”,“bid”};
int[]to={android.R.id.text1};
a=新的SimpleCursorAdapter(这是android.R.layout.simple_下拉列表_项目_1line,null,from,to,0);
a、 设置管柱转换柱(1);
FilterQueryProvider=新的FilterQueryProvider(){
@凌驾
公共游标运行查询(最终CharSequence约束){
Log.d(“标记”,“运行查询约束:”+约束);
if(约束==null){
返回null;
}
数组_name.clear();
数组_id.clear();
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=null;
httppost=新的httppost(搜索目标);
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“newtext”,constraint.toString());
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
Log.e(“错误”,“http连接中的错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
//Log.i(“sb”,sb+);
Bresult=sb.toString();
Log.i(“Bresult”,Bresult+);
}捕获(例外e){
Log.e(“错误”,“错误转换结果”+e.toString());
}
c=新矩阵(列名称);
试一试{
if(Bresult!=null){
jArray=新JSONArray(Bresult);

对于(int i=0;我在这里看不到任何AsyncTask…你在说什么AsyncTask?是的,它只是一个需要时间的后台线程(runQuery)。我用它修复了原始问题。感谢你指出它。有什么建议吗?请看我的回答:我添加了tv.setOnItemClickListener(l);我看不到aye正在观察的行为等一下……为什么要调用actv_name.setText(“”?)呢?我之所以调用它,是因为每当用户单击某个建议时,它就会转到另一个活动,如果用户按“后退”按钮,我希望AutoCompleteTextView再次为空。
            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("");
                }
            });