2017-07-06 27 views
0

私は答えを見つけることができない問題に直面しています。私はTextWatcherを使ってeditTextをデータベースの結果のリストを見つけることができます。結果をPopupMenuに表示します。ポップアップメニューはキーボードのクリックを避けることを避けます

enter image description here

final List<SearchResult> searchResults = new ArrayList<>(); 

    final PopupMenu menu = new PopupMenu(getActivity(), binding.bottomSheetWriteNews.editTextNewsSubCategory); 
    menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
     @Override 
     public boolean onMenuItemClick(MenuItem item) { 
      binding 
        .bottomSheetWriteNews 
        .editTextNewsSubCategory 
        .setText(item.getTitle()); 

      return false; 
     } 
    }); 


    final Realm realm = Realm.getDefaultInstance(); 

    binding.bottomSheetWriteNews.editTextNewsSubCategory.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

      searchResults.clear(); 
      menu.getMenu().clear(); 

      RealmResults<SubCategory> subCategoriesResult = realm.where(SubCategory.class).contains("name", s.toString(), Case.INSENSITIVE).findAll(); 
      for(SubCategory subCategory : subCategoriesResult) { 
       searchResults.add(new SearchResult(subCategory.getId(), subCategory.getName(), false)); 
       menu.getMenu().add(subCategory.getName()); 
      } 
      menu.show(); 
     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
             int after) { 
     } 

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

私の問題は、私はのPopupMenuは私が望んでいた文字を入力する代わりに却下されているキーボードをクリックして、すべての回である:それはこのようになります。それはユーザーにとって迷惑で使い物にならない。私は、キーボードのクリックがPopupMenuを却下しないようにしたい。公式の文書ではTouching outside of the popup will dismiss itと書かれていますが、私はGoogle Keepを使用していますが、私の問題なしで同じ機能(結果をフィルタリングするためにテキスト入力用のPopupMenuを表示)があります。

ありがとうございました!

答えて

0

代わりのポップアップメニューを使用します(

スピナー、(リストビューまたはリサイクルビュー)

を使用すると、レイアウトが積み重ね取得しないようにnotifyDataSetChanged().

を行うことができますオフとオン)

希望すると助かります。

0

私は、同僚との別の解決策を見つけた後、自分自身の答えに応じます。 ArrayAdapterを取ることができるautoCompleteTextViewという名前の特定のEditTextがあります。そして、それはあなたのための検索を行います。ここで私はそれを実装する方法です:

final Realm realm = Realm.getDefaultInstance(); 
    RealmResults<SubCategory> subCategoriesResult = realm.where(SubCategory.class).findAll(); 
    List<String> categoryName = new ArrayList<>(); 
    for(SubCategory subCategory : subCategoriesResult) { 
     categoryName.add(subCategory.getName()); 
    } 
    String[] stringArray = categoryName.toArray(new String[0]); 


    ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), 
      android.R.layout.simple_dropdown_item_1line, stringArray); 
    binding.bottomSheetWriteNews.autoCompleteTextViewNewsSubCategory.setAdapter(adapter); 

そして、もっと何もありません! 2文字を入力すると、結果のリストがPopupMenuとして表示されます。

いずれにしても、PopupMenuが表示されているときにキーボードをクリックしてから消滅することを回避する解決策がある場合は、私は興味があります。ありがとう!

関連する問題