2014-03-31 11 views
1

AutoCompleteTextView不顯示任何建議,但數據填充適配器如何AutoCompleteTextView顯示的建議名單,並從API填充數據

這裏是我的CODE

public class AUTOSuggestion extends AsyncTask<String, String, String> { 
    String TITLE, id,level;  
    String response = " "; 
    ProgressDialog dialogProgress = new ProgressDialog(Home.this); 

    String tex; 

    public AUTOSuggestion(String text) { 
     // TODO Auto-generated constructor stub 
     tex=text; 
    } 

    protected void onPreExecute() { 
     dialogProgress.setCancelable(true); 
     dialogProgress.setMessage("Please wait.."); 
     dialogProgress.setIndeterminate(false); 
     dialogProgress.show(); 

    } 

    @Override 
    protected String doInBackground(String... params) { 
     try { 
      ArrayList<NameValuePair> param = new ArrayList<NameValuePair>(); 
      param.add(new BasicNameValuePair("search", tex)); 
     /* param.add(new BasicNameValuePair("catId", id)); 
      param.add(new BasicNameValuePair("level", level));*/ 
      response = CustomHttpClient.executeHttpPost(AUTOSUGGESTION_URL, param); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return response; 
    } 

    protected void onPostExecute(String response) 
    { 
     if (response != "" || response != null) { 
      try { 
       ArrayList<String>sug_list=new ArrayList<String>(); 
       JSONObject Obj = new JSONObject(response); 
       //String status = Obj.getString("TAG_STATUS"); 
       JSONArray jarr=Obj.getJSONArray("0"); 
       for(int i=0;i < jarr.length(); i++){ 
       JSONObject p = (JSONObject) jarr.get(i); 
       String words = p.getString("title"); 
       sug_list.add(words); 
       } 

       item = sug_list.toArray(new String[sug_list.size()]); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
       Toast.makeText(getBaseContext(), e + "", Toast.LENGTH_LONG).show(); 
       finish(); 
      } 

     } 
     try { 
      dialogProgress.dismiss(); 
     } catch (Exception e) { 
      e.fillInStackTrace(); 
     } 
     autocomplete = (AutoCompleteTextView) findViewById(R.id.autocomplete); 
     //Toast.makeText(getBaseContext(), item.toString(), Toast.LENGTH_LONG).show(); 
     adapter = new ArrayAdapter<String>(Home.this,android.R.layout.simple_dropdown_item_1line, item); 

     // Create adapter 
     //adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, item); 
     autocomplete.setThreshold(1); 
     autocomplete.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 
    } 

雖然塊調試它顯示的話

GUYS請幫助我:'(

enter image description here

正如所建議的在這裏這個其他人是改變我做

修改後的代碼

public class AUTOSuggestion extends AsyncTask<String, String, String> { 
    String TITLE, id,level;  
    String response = " "; 
    ProgressDialog dialogProgress = new ProgressDialog(Home.this); 

    String tex; 

    public AUTOSuggestion(String text) { 
     // TODO Auto-generated constructor stub 
     tex=text; 
    } 

    protected void onPreExecute() { 
     dialogProgress.setCancelable(true); 
     dialogProgress.setMessage("Please wait.."); 
     dialogProgress.setIndeterminate(false); 
     dialogProgress.show(); 

    } 

    @Override 
    protected String doInBackground(String... params) { 
     try { 
      ArrayList<NameValuePair> param = new ArrayList<NameValuePair>(); 
      param.add(new BasicNameValuePair("search", tex)); 
      response = CustomHttpClient.executeHttpPost(AUTOSUGGESTION_URL, param); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return response; 
    } 

    protected void onPostExecute(String response) 
    { 
     if (response != "" || response != null) { 
      try { 

       JSONObject Obj = new JSONObject(response); 
       //String status = Obj.getString("TAG_STATUS"); 
       JSONArray jarr=Obj.getJSONArray("0"); 
       for(int i=0;i < jarr.length(); i++){ 
       JSONObject p = (JSONObject) jarr.get(i); 
       String words = p.getString("title"); 
       sug_list.add(words); 
       } 

       item = sug_list.toArray(new String[sug_list.size()]); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
       Toast.makeText(getBaseContext(), e + "", Toast.LENGTH_LONG).show(); 
       finish(); 
      } 

     } 
     try { 
      dialogProgress.dismiss(); 
     } catch (Exception e) { 
      e.fillInStackTrace(); 
     } 

     adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,sug_list);   
     autocomplete.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 
     Toast.makeText(getBaseContext(),sug_list+"",Toast.LENGTH_LONG).show(); 
    } 
} 

這裏是item.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#eaeaea" 
android:padding="10dp" 
android:textColor="#000" 
android:textSize="16sp" > 

</TextView> 

enter image description hereenter image description here

+2

打印產品陣列將它添加到適配器,是否有輸出,讓我們前知道。 – SilentKiller

+0

對不起,我的錯。嘗試'android.R.layout.simple_list_item_1'而不是'android.R.layout.simple_dropdown_item_1line' – SilentKiller

+0

代碼在我身邊正常工作。 – SilentKiller

回答

2

花費6小時後我才知道,我必須添加一行

autocomplete.showDropDown(); 

asynconpost

-3

檢查此page

您可能需要update的用戶界面。

runOnUiThread(new Runnable(){ 
     public void run(){ 
      aAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,suggest); 
      autoComplete.setAdapter(aAdapter); 
      aAdapter.notifyDataSetChanged(); 
     } 
    }); 
+0

請檢查代碼,他已經在做。 – SilentKiller

相關問題