【问题标题】:Single EditText that updates few ListViews via Adapter/TextWatcher通过 Adapter/TextWatcher 更新几个 ListView 的单个 EditText
【发布时间】:2016-08-12 12:26:46
【问题描述】:

我有一个视图,其中我将单个 EditText 和屏幕分隔在两个部分(2 个 ListViews)上。我想将数据输入到 EditText 并使用这些数据以不同的方式更新两个 ListView。例如,对于第一个 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);

}

【问题讨论】:

    标签: android android-arrayadapter listadapter textwatcher


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 2011-11-18
      • 2015-08-08
      相关资源
      最近更新 更多