自动填充下拉列表不会显示

时间:2013-01-20 20:12:06

标签: android web-services autocomplete

最终我试图从webservice结果中获得一个自动完成列表,到目前为止我有这个代码,我记录了从服务返回的结果是正确的,但结果不会填入下拉列表,所以我可以选择。我认为它与我如何放置适配器有关。

 AlertDialog.Builder adb = new AlertDialog.Builder(SettingsMain.this);
                LayoutInflater inflater = SettingsMain.this.getLayoutInflater(); 
                final View searchlayout = inflater.inflate(R.layout.search_friend, null);

                friend_name = (AutoCompleteTextView) searchlayout.findViewById(R.id.friend_name);
                friend_name.setThreshold(3);
                dpy = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,adapterList);
                friend_name.setAdapter(dpy);
                friend_name.addTextChangedListener(new TextWatcher() {
                    public void afterTextChanged(Editable editable) {

                    }

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

                    }

                    public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                        String text = charSequence.toString();
                        if (text.length() > 3) {
                            new MyAsyncTask().execute(url+text);


                        }
                    }
                });



                adb.setView(searchlayout)

               .setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int whichButton) {  


                        String name = friend_name.getText().toString();
                        Log.d( "ECHO" , "text : " + name);
                        return;                  
                       }  
                  })
               .setNegativeButton("Done", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {


                        dialog.cancel(); 
                    }
                }); 

                    adb.show();     

这是我的asynctask类

        class MyAsyncTask extends AsyncTask<String, Void, JSONObject>{
    ArrayList<String> names;
    JSONObject jArray;

    @Override
    protected void onPreExecute(){

    }

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


            response = httpClient.execute(new HttpPost(url));
            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();
            jArray = new JSONObject(result);


        } catch(Exception e){
            Log.v("DOIB",e.getMessage());
        }
        return jArray;
    }

    @Override
    protected void onPostExecute(JSONObject result_data){
        try{

            for (int i = 0; i < result_data.length(); i++) {

                adapterList.add(result_data.getJSONObject("friend"+i).optString("user"));
                Log.d("NMES", result_data.getJSONObject("friend"+i).optString("user").toString());
            }
            dpy.notifyDataSetChanged();
        }catch(Exception e){ Log.d("SYNCERR", e.toString());}
    }

}

任何提示或帮助将不胜感激。谢谢:))

已更新

1 个答案:

答案 0 :(得分:1)

你无法按照你想要的方式从asynctask返回数据。你必须这样做:

ArrayList<String> adapterList = new ArrayList<String>();
new MyAsyncTask() {
    @Override
    protected void onPostExecute( ArrayList<String> list ) {
        adapterList = list;
    }
}.execute( ... );