2013-01-20 48 views
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());} 
    } 

} 

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

修订

+0

还得到一个performFiltering错误 – kabuto178

回答

1

你不能从返回的AsyncTask数据你想要的方式。你要做这样的:

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

会尝试现在 – kabuto178

+0

应该adapterList是全球性的? – kabuto178

+0

yes或在活动 –

相关问题