2

最终,我试图从 web 服务结果中实现自动完成列表,到目前为止,我有这段代码,我记录了从服务返回的结果,这些结果是正确的,但结果不会被填充到下拉列表中,因此我可以从中进行选择。我认为这可能与我放置适配器的方式有关。

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

}

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

更新

4

1 回答 1

1

您无法以您尝试的方式从 asynctask 返回数据。你必须这样做:

ArrayList<String> adapterList = new ArrayList<String>();
new MyAsyncTask() {
    @Override
    protected void onPostExecute( ArrayList<String> list ) {
        adapterList = list;
    }
}.execute( ... );
于 2013-01-20T20:19:50.363 回答