2012-05-25 16 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" /> 

MY 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); 

これは私がそのだけでEditTextViewのように働いて意味.... atall機能していません。どこが間違っていますか?

完全なコード:

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

スレッショルドを2に設定したので、2文字を入力した後に起動します – Venky

+0

ええと、2を常に入力しています...ドロップダウンリストが表示されません – Housefly

答えて

11

コメント私はこの1つだけを見る前に、あなたの他の質問を見ました。私はしばらくの間オートコンプリートで苦労していたし、最終的には動作させるまで、すべてのキーワードをダウンロードするというあなたの新しい実装にほとんど戻った。私がしたことは、私textwatcherのonTextChanged方法で

//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); 

、私はその後、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を参照してくださいに更新AsyncTaskのonPostExecuteでasynctask

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

を使用することを提案している得ます

+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

完璧に私のアンドロイド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); 
関連する問題