How to use SearchView.OnQueryTextListener() in searchable spinner?

前端 未结 2 1753
猫巷女王i
猫巷女王i 2021-01-12 07:50

I\'m creating a searchable spinner using third party library. I have added library classes(SearchableListDialog, SearchableSpinner) in my app. Everything is working fine but

2条回答
  •  天涯浪人
    2021-01-12 08:25

    Instead of using a Spinner with SearchView, I would suggest you to achieve this with ListView and SearchView, I tried and it works very well.

    Put a button on your activity. Now clicking on this button will open a custom dialog.

    Your custom_dialog.xml

    
    
    
        
    
        
    
    
    

    Then set your button onClick event and do the following.

     @Override
                public void onClick(View view) {
                    Dialog dialog = new Dialog(SearchText.this);
                    LayoutInflater inflater = LayoutInflater.from(SearchText.this);
                    View view1 = inflater.inflate(R.layout.custom_search_layout, null);
                    ListView listView = view1.findViewById(R.id.listView);
                    SearchView searchView = view1.findViewById(R.id.searchView);
    
                    final ArrayAdapter stringArrayAdapter = new ArrayAdapter(SearchText.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.my_currency));
                    listView.setAdapter(stringArrayAdapter);
    
                    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                        @Override
                        public boolean onQueryTextSubmit(String newText) {
                            return false;
                        }
    
                        @Override
                        public boolean onQueryTextChange(String newText) {
                            stringArrayAdapter.getFilter().filter(newText);
                            return false;
                        }
                    });
                    dialog.setContentView(view1);
                    dialog.show();
                }
    

    Now do your search, and add OnItemClickListener on your listview, and do whatever you want after selecting your choice.

提交回复
热议问题