简体   繁体   English

AutoCompleteTextView侦听器

[英]AutoCompleteTextView listeners

I'm creating input with drown menu, which fetches data from server with input from user and adds the data to the list. 我正在使用淹没菜单创建输入,该菜单使用来自用户的输入从服务器获取数据并将数据添加到列表中。

I got this code: 我得到了这段代码:

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

            }
        });

Now the problem I got is: I need to know which items in dropdown list is clicked. 现在我遇到的问题是:我需要知道点击下拉列表中的哪些项目。

Usually it is done by using onItemClick , but I already have TextWatch listener added to from_autocompl and android allows by default only one listener and now I want to know how to do it. 通常它是通过使用onItemClick完成的,但我已经将TextWatch监听器添加到from_autocompl并且android默认情况下只允许一个监听器,现在我想知道如何做到这一点。 How to by pass this? 如何通过这个?

You no need to use textChange Listener Use this, AutoComplete Box have the default property of textwatcher 您无需使用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();
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM