设置文本时多重自动完成TextView

时间:2015-08-11 03:59:48

标签: android

我遇到问题MultilAutoCompleteTextView在我设置文字时自动完成。这是我的代码

listContact = (List<Contact_ID>) readObj(this, "kute");
        ListContactAdapter adapter = new ListContactAdapter(MainAddSms.this, R.layout.layout_getallcontact_item, listContact);
        auto = (MultiAutoCompleteTextView) findViewById(R.id.textViewAuto);
        auto.setThreshold(1);
        auto.setAdapter(adapter);
        auto.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

        auto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Contact_IDs apk = new Contact_IDs(null, null);
                apk.setPhoneNumbers(listContact.get(position).getPhoneNumber());
                apk.setNames(listContact.get(position).getName());
                listContactPhone.add(apk);
            }
        });
  auto.setText("09758525244,09758514711"); 

09758525244 = minhml

09758514711 =我!

当我输入字母建议时,它们仍能正常工作,但是当我将文本设置为它时,它就无法正常工作。谁能帮我? 读书的好处

1 个答案:

答案 0 :(得分:0)

我理解你的问题。以下是解决方案:

multiAutoTextView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // If wanna filter only the texts are present in TextView, update adapter here
                adapter.clear();
                String text = String.valueOf(s);
                String[] values = text.split(",");
                arrayListMulti = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.items_array)));
                for (String value : values) {
                    for (String original : arrayListMulti) {
                        if (original.contains(value.trim())) {
                           adapter.add(original);
                        }
                    }
                }
                adapter.notifyDataSetChanged();

                // delay execution to avoid "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid"
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        multiAutoTextView.showDropDown();
                    }
                }, 500);                     
            }
        });

multiAutoTextView.setText("09758525244");