我正在尝试使用AsyncTask与服务器进行自动填充。我在这里分享了我的代码。

这是我的AutocompleteTextwatcher的代码:

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.mainautocomplete);
textView.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable editable) {

    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int arg1, int arg2, int arg3) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
        String text=charSequence.toString();
        if (text.length() > 3) {
            MyAsyncTask myTask = new MyAsyncTask();
            try {
                ArrayList<String> adapterList = new ArrayList<String>();
                adapterList=myTask.execute(url).get();/*My Web service url*/
                if(!adapterList.isEmpty()){
                    Log.v("ADPTER LIST",adapterList.toString());
                    adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, adapterList);
                    textView.setAdapter(adapter);
                }else{
                    Log.v("ADPTER LIST","No VALUES");
                }
            } catch (InterruptedException e) {
                Log.v("catch",e.getMessage());
            } catch (ExecutionException e) {
                Log.v("catch2",e.getMessage());
            }
        }
    }
});

我的AsyncTask代码是
private class MyAsyncTask extends AsyncTask<String, Void, ArrayList<String>>{
    @Override
    protected void onPreExecute(){
        pd = ProgressDialog.show(HomeLayoutActivity.this,"", "Please Wait!");
    }

    @Override
    protected ArrayList<String> doInBackground(String... params) {
        try {
            String myQuery=params[0];
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = null;
            InputStream is = null;
            String result = null;
            StringBuilder sb = null;

            response = httpClient.execute(new HttpGet(myQuery));
            is = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = "0";

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();
            result = sb.toString();
            JSONArray jArray = new JSONArray(result);
            JSONObject jData = null;
            tags.clear();

            for (int i = 0; i < jArray.length(); i++) {
                jData = jArray.getJSONObject(i);
                tags.add(jData.getString("tagname"));
            }
            Log.v("JSON",""+tags.toString());

        } catch(Exception e){
            Log.v("MUGBUG4",e.getMessage());
        }
        return tags;
    }

    @Override
    protected void onProgressUpdate(Void...strings ){

    }

    @Override
    protected void onPostExecute(ArrayList<String> result){
        Log.v("OPE",result.toString());
        pd.dismiss();
    }

}

我的问题是
  • 我可以从ArrayList记录AsyncTask的结果(我已登录并检查了)。但是这些值未显示在自动完成下拉列表中。
  • 我在这里感到很惊讶,我向Web服务器发出了自动完成请求,要求输入三个以上的字符。键入超过3个字符后,请求将继续,没有任何建议显示。但是,建议一次出现后,建议输入的字符数少于3个。
  • 我的代码没有抛出异常。

  • 我试过的是
  • 我试图在onPostExecuteAsyncTask中设置适配器。显示相同的输出。
  • 我尝试使用android 2.3模拟器。工作正常。

  • android 4和Asynctask doInBackground()有什么问题吗?

    最佳答案

    我有同样的情况。我不知道这有什么帮助,但是我建议您在添加适配器后添加以下代码行。

        if (!textView.isPopupShowing()) {
            textView.showDropDown();
        }
    

    它帮助了我。确实,我不知道此行为的确切原因,但是在AutoCompleteTextView的早期实现中,他们使用ListView来显示下拉列表。在4.0(可能更早)中,他们开始使用ListPopupWindow。可能这就是原因。
    干杯

    10-07 19:31
    查看更多