AutoCompleteTextView侦听器

时间:2014-02-14 11:38:35

标签: android

我使用淹没菜单创建输入,该菜单使用来自用户的输入从服务器获取数据并将数据添加到列表中。

我收到了这段代码:

from_autocompl = (AutoCompleteTextView)rootView.findViewById(R.id.from_autocompl);
        from_autocompl.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                    new JSONParse().execute();
                    if (ImDoneWithJSON == 1)
                    {
                        ArrayAdapter<List_From_JSON> adapter = new ArrayAdapter<List_From_JSON>(
                                this, android.R.layout.simple_list_item_1, data);
                        from_autocompl.setAdapter(adapter);
                        ImDoneWithJSON = 0;
                    }

            }
        });

现在我遇到的问题是:  我需要知道点击下拉列表中的哪些项目。

通常使用onItemClick完成,但我已经将TextWatch侦听器添加到from_autocompl并且android默认只允许一个侦听器,现在我想知道如何执行。如何通过这个?

1 个答案:

答案 0 :(得分:2)

您不需要使用textChange Listener使用此功能,AutoComplete Box具有textwatcher的默认属性

            public class MainActivity extends Activity {

// private AutoCompleteTextView autoComplete;
 private MultiAutoCompleteTextView multiAutoComplete;
 private ArrayAdapter<String> adapter;

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

    // get the defined string-array 
    String[] colors = getResources().getStringArray(R.array.colorList);

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,colors);

    //autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
    multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoComplete);

    // set adapter for the auto complete fields
//  autoComplete.setAdapter(adapter);
    multiAutoComplete.setAdapter(adapter);

    // specify the minimum type of characters before drop-down list is shown
    //autoComplete.setThreshold(1);
    multiAutoComplete.setThreshold(2);
    // comma to separate the different colors
    multiAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

    // when the user clicks an item of the drop-down list
    multiAutoComplete.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
             Toast.makeText(getBaseContext(), "MultiAutoComplete: " +
                        "you add color "+arg0.getItemAtPosition(arg2),
                        Toast.LENGTH_LONG).show();
        }
    });
}