单个EditText,通过Adapter / TextWatcher

时间:2016-08-12 12:26:46

标签: android android-arrayadapter listadapter textwatcher

我有一个视图,我在两个部分(2个ListViews)上分离了单个EditText和屏幕。我想将数据输入到EditText并使用此数据以不同方式更新两个ListView。例如,对于1st ListView,请输入以我的输入开头的单词。到第二个ListView去包含我输入的单词。

我制作了两个不同的自定义ListAdapter来实现所需的功能。然后我将它们添加到我的EditText对象并开始错误。如果我只使用其中一个适配器,那么一切都还可以。

那我怎么能用两个适配器单个EditText来以不同的方式进行这个更新2 ListViews。

这是我的代码。

ListView lv;
ListView lv2;
ArrayAdapter<String> adapterForSearch;
ArrayAdapter<String> adapterForTurkishSearch;


@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.dictionary_screen);
    mContext = this;

    List<Map<String,?>> dictionaryTopics = new LinkedList<>();


    dictionaryTopics.add(createItem("Я ТЫ МЫ ВЫ", formWordCouplesSet(words.getMeU())));
    dictionaryTopics.add(createItem("Animals", formWordCouplesSet(words.getAnimals())));


    // create our list and custom adapter
    SeparatedListAdapter adapter = new SeparatedListAdapter(this);

    adapter.addSection("Turkish Dictionary", new SimpleAdapter(this, dictionaryTopics, R.layout.list_complex,
            new String[]{ITEM_TITLE, ITEM_CAPTION}, new int[]{R.id.list_complex_title, R.id.list_complex_caption}));

    list = (ListView) findViewById(R.id.dictionary_topics);
    list.setAdapter(adapter);
    list.setClickable(true);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            for(int index=0; index<((ViewGroup)view).getChildCount(); ++index) {
                View nextChild = ((ViewGroup)view).getChildAt(index);
                if (index >0) {
                    if (checkVisibility(nextChild))
                    {
                        nextChild.setVisibility(View.GONE); dimCounter--; checkDimCounter();
                    }
                    else {
                        nextChild.setVisibility(View.VISIBLE); dimCounter++; checkDimCounter();
                    }
                }
            }
        }
    });

    //Search Implementation

    inputSearch = (EditText) findViewById(R.id.inputSearch);

    lv = (ListView) findViewById(R.id.list_view);
    lv2 = (ListView) findViewById(R.id.list_view2);

    adapterForSearch = new MyAdapter<>(this, R.layout.list_item2, R.id.product_name, wordCombos);
    adapterForTurkishSearch = new MyTurkishAdapter<>(this, R.layout.list_item2, R.id.product_name, wordCombos);



    TextWatcher watcher1;

    watcher1 = new TextWatcher() {


        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text

            if (cs.length() == 0)
            {
                    lv.setAdapter(null);
            }
            else {

                    lv.setAdapter(adapterForSearch);
                    adapterForSearch.getFilter().filter(cs);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
        }
        @Override
        public void afterTextChanged(Editable arg0) {
        }
    };


    TextWatcher watcher2;

    watcher2 = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text

            if (cs.length() == 0)
            {

                    lv2.setAdapter(null);

            }
            else {

                    lv2.setAdapter(adapterForTurkishSearch);
                    adapterForTurkishSearch.getFilter().filter(cs);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {
        }
    };

    inputSearch.addTextChangedListener(watcher1);
    inputSearch.addTextChangedListener(watcher2);

}

1 个答案:

答案 0 :(得分:0)

EditText只允许一个注册的TextChangedListener,因此最后一个,&#34; watcher2&#34;将是最终被使用的那个,因为它是最后一组。您应该找到一种方法将onTextChanged()块中的代码合并到一个TextWatcher中,并且只设置一个。 https://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener(android.text.TextWatcher)

您也可以考虑更改设置适配器的方式,这可能意味着您必须更改自定义适配器中的某些逻辑,但由于您没有为它们发布代码,我无法确切地告诉您。理想情况下,列表视图的适配器应设置在TextWatcher之外的某个位置。然后在你的onTextChanged()代码中你应该做adpater.notifyDataSetChanged();在你运行代码来过滤你的列表视图后更新listview,这应该在onTextChanged()里面而不是在适配器里完成。