5

ArrayAdapter を使用して AutocompleteTextView のリストを動的に更新しようとしています。このビューを更新するために、TextWatcher を使用して、AutocompleteTextView で発生する可能性のある変更を監視します。

問題は、リストがまったく更新されず、その理由が理解できないことです。私はインターネットでそのようなものを探していて、いくつかの異なるアプローチを見つけましたが、最も単純なアプローチであるはずのこのアプローチが機能しない理由をまだ理解できません。どんな説明でも大歓迎です。

ターゲット AVD: Google API レベル 10、Android 2.3.3

簡略化されたコードは次のとおりです。

public class AutocompleteActivity extends Activity implements TextWatcher {
    ArrayAdapter<String> adapter = null;
    AutoCompleteTextView acTextView = null;
    ArrayList<String> addresses = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.autocompleteview);

        acTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete_address);
        adapter = new ArrayAdapter<String>(this, R.layout.listitem, addresses);
        adapter.setNotifyOnChange(true);
        acTextView.addTextChangedListener(this);
        acTextView.setAdapter(adapter);
    }

    @Override
    public void onTextChanged(CharSequence text, int start, int before, int after) {
        try {
            adapter.add("test");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
4

1 に答える 1

3

ソースコードでは、TextWatcher が AutoCompleteTextView に提供されています。これが本当の問題です。

AutoCompleteTextView のソース コードを見ると、AutoCompleteTextView には「MyWatcher」という名前の独自の TextWatcher があることがわかります。このため、AutoCompleteTextView は入力アクションに応答できません。

于 2011-10-13T07:33:31.990 に答える