在Android 4中使用Asynctask和webservice自动完成TextView

Aki*_*lan 5 android android-asynctask android-4.0-ice-cream-sandwich

我正在尝试使用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());
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

我的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();
    }

}
Run Code Online (Sandbox Code Playgroud)

我的问题是,

  • 我可以记录ArrayList来自AsyncTask(我记录并检查)的结果.但这些值未显示在自动完成下拉列表中.
  • 我很惊讶,我向Web服务器发出自动完成请求,输入的字符数超过三个.一旦键入超过3个字符,请求即将进行,没有显示任何建议.但是一旦发生请求,显示少于3个字符的建议显示.
  • 我的代码没有抛出异常.

我试过的是,

  • 我试着在设置适配器onPostExecuteAsyncTask.显示相同的输出.
  • 我试过用android 2.3模拟器.它工作正常.

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

Inf*_*nus 8

我有同样的情况.我不知道它会有所帮助,但我建议你在添加适配器后添加这些代码行.

    if (!textView.isPopupShowing()) {
        textView.showDropDown();
    }
Run Code Online (Sandbox Code Playgroud)

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