13

私のXML:

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

これはまったく機能していません....つまり、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

    }
}
4

6 に答える 6

21

この質問を見る前に、あなたの他の質問を見ました。私はしばらくオートコンプリートに苦労していましたが、最終的に機能するまで、すべてのキーワードをダウンロードするという新しい実装にほぼ戻りました。私がしたことは次のとおりです。

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

詳細については、 setNotifyOnChangeおよびnotifyDataSetChangedを参照してください。

于 2012-05-30T11:06:56.167 に答える
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 行にコメントするだけです...

于 2012-05-25T11:29:28.773 に答える