2016-08-12 1 views
-1

私は1つのEditTextと2つの部分(2 ListViews)で区切られた画面を持つビューを持っています。 EditTextにデータを入力し、このデータを使用して両方のListViewを異なる方法で更新したいと思います。たとえば、最初のListViewには、自分の入力から始まる単語を表示します。 2番目のリストビューには、自分の入力を含む単語を表示します。Adapter/TextWatcherを使用して少数のListViewを更新する単一のEditText

必要な機能を実装する2つの異なるカスタムListAdapterを作成しました。それから私はそれらを私のEditTextオブジェクトに加え、バグが始まります。私がアダプターのうちの1つだけを使用すると、すべてが大丈夫です。

私はどのようにして1つのEditTextに2つのアダプタを使用して、この2つのリストビューを異なる方法で更新することができますか。

ここに私のコードです。

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

} 

答えて

0

アンのEditTextは、従って最後の、「watcher2」は、それが最後に設定したので、使用されてしまうものであろう1登録TextChangedListenerを可能にします。両方のonTextChanged()ブロックにあるコードを1つのTextWatcherにマージし、そのコードだけを設定する方法を見つける必要があります。また、あなたはあなたがそれらのコードを投稿するdidntのため、pontentially私は確かにあなたを教えて傾けるあなたのカスタム・アダプターで、いくつかのロジックを変更する必要があります意味が、可能性のアダプタを設定する方法を変更することを検討することをお勧めします https://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener(android.text.TextWatcher)

。リストビューのアダプタは、理想的にはTextWatcherの外側のどこかに設定する必要があります。次に、onTextChanged()コードでadpater.notifyDataSetChanged();を実行して、リストビューをフィルタリングした後にリストビューを更新し、アダプタ内ではなくonTextChanged()内で行う必要があります。

関連する問題