2013-01-20 65 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或在活動 –

相關問題