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

前端 未结 2 1754
猫巷女王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:17

    you can search in your adapter... i didn't see your adapter you can look my adapter https://github.com/kenmeidearu/SearchableSpinner/blob/master/searchablespinnerlibrary/src/main/java/com/kenmeidearu/searchablespinnerlibrary/ListAdapterSpinner.java

    i give you example

    0 讨论(0)
  • 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

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <android.support.v7.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
    </LinearLayout>
    

    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<String> stringArrayAdapter = new ArrayAdapter<String>(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.

    0 讨论(0)
提交回复
热议问题