2012-05-25 32 views
5

我的XML:AutoCompleteTextView沒有表現出任何下拉項

<AutoCompleteTextView 
     android:id="@+id/searchAutoCompleteTextView_feed" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:completionThreshold="2" 
     android:hint="@string/search" /> 

我的Java代碼:

AutoCompleteTextView eT = (AutoCompleteTextView)findViewById(R.id.searchAutoCompleteTextView_feed); 
eT.addTextChangedListener(this); 
String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"}; 
ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, sa); 
eT.setAdapter(aAdapter); 

這不是工作atall ....我的意思是它只是工作就像一個EditTextView。我在哪裏錯了?

完整代碼:

public class FeedListViewActivity extends ListActivity implements TextWatcher{ 


    private AutoCompleteTextView eT; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.feed); 

     eT = (AutoCompleteTextView) findViewById(R.id.searchAutoCompleteTextView_feed); 
     eT.addTextChangedListener(this); 

        Thread thread = new Thread(null, loadMoreListItems); 
        thread.start(); 
    } 

    private Runnable returnRes = new Runnable() { 
     public void run() { 

      //code for other purposes 
     } 
    }; 

    private Runnable loadMoreListItems = new Runnable() { 
     public void run() { 
      getProductNames(); 

      // Done! now continue on the UI thread 
      runOnUiThread(returnRes); 
     } 
    }; 

    protected void getProductNames() { 

      String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"}; 

      ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(getApplicationContext(), 
        android.R.layout.simple_dropdown_item_1line, sa); 
      eT.setAdapter(aAdapter); 

    } 

    public void afterTextChanged(Editable s) { 
     // TODO Auto-generated method stub 

    } 

    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
     // TODO Auto-generated method stub 

    } 

    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     // TODO Auto-generated method stub 

    } 
} 
+0

根本不工作?你輸入了多少個字符來檢查? –

+4

您使用Threshold作爲2,因此它會在您輸入2個字符後觸發 – Venky

+0

是的,我總是打字超過2 ...它沒有顯示任何下拉項目 – Housefly

回答

11

我剛纔看到您的其他問題。一段時間以來,我一直在爲自動完成而苦苦掙扎,並且我幾乎恢復了您下載所有關鍵字的新實現,直到我終於將其實現。我所做的是;

//In the onCreate 
//The suggestArray is just a static array with a few keywords 
this.suggestAdapter = new ArrayAdapter<String>(this, this.suggestionsView, suggestArray); 
//The setNotifyOnChange informs all views attached to the adapter to update themselves 
//if the adapter is changed 
this.suggestAdapter.setNotifyOnChange(true); 

在我textwatcher的onTextChanged方法,我得到的建議使用的AsyncTask

//suggestsThread is an AsyncTask object 
suggestsThread.cancel(true); 
suggestsThread = new WertAgentThread(); 
suggestsThread.execute(s.toString()); 

在的AsyncTask的onPostExecute然後我更新autocompletetextview

//suggestions is the result of the http request with the suggestions 
this.suggestAdapter = new ArrayAdapter<String>(this, R.layout.suggestions, suggestions); 
this.suggestions.setAdapter(this.suggestAdapter); 
//notifydatasetchanged forces the dropdown to be shown. 
this.suggestAdapter.notifyDataSetChanged(); 

setNotifyOnChangenotifyDataSetChanged瞭解更多信息

+8

來檢查看來你還沒有完全理解notifyDataSetChanged()方法的概念。您正在onPostExecute方法中創建一個ArrayAdapter的新實例,並將其設置爲適配器。在你的例子中,notifyDataSetChanged()方法調用是無用的。 –

0
AutoCompleteTextView eT = (AutoCompleteTextView)findViewById(R.id.searchAutoCompleteTextView_feed); 
// eT.addTextChangedListener(this); 
    String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"}; 
    ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, sa); 
    eT.setAdapter(aAdapter); 

其工作et.addtext行看到這個人之前只是評論...

+0

是啊我試過,甚至....它沒有爲我工作 – Housefly

+0

其工作在我的android 2.3.1版本完美.. – jigspatel

+0

確實版本事宜autocompletetextview ???我知道...我是一些較低的API ...需要通過更改 – Housefly

1

這是我的項目中的一個片段。我認爲,從服務獲得數據後,您所要做的就是:

  1. 清除以前的數據。
  2. 清除以前的適配器值。
  3. 然後使用add()或addAll()方法將值添加到數據列表。
  4. 通過在適配器上調用notifyDataSetChanged()來通知更改的數據。

    @Override 
    public void onGetPatient(List<PatientSearchModel> patientSearchModelList) { 
    
    //here we got the raw data traverse it to get the filtered names data for the suggestions 
    
    stringArrayListPatients.clear(); 
    stringArrayAdapterPatient.clear(); 
    for (PatientSearchModel patientSearchModel:patientSearchModelList){ 
    
        if (patientSearchModel.getFullName()!=null){ 
    
         stringArrayListPatients.add(patientSearchModel.getFullName()); 
    
        } 
    
    } 
    
    //update the array adapter for patient search 
    stringArrayAdapterPatient.addAll(stringArrayListPatients); 
    stringArrayAdapterPatient.notifyDataSetChanged(); 
    

    }

但在此之前這一切確保您已連接適配器自動完成TextView的,如果不這樣做如下:

ArrayAdapter<String> stringArrayAdapterPatient= new ArrayAdapter<String>(getActivity(),android.support.v7.appcompat.R.layout.select_dialog_item_material,stringArrayListPatients); 

completeTextViewPatient.setAdapter(stringArrayAdapterPatient);