简体   繁体   English

未在 Searchview 中显示正确的 Listview 项目

[英]Not showing the correct Listview Item in Searchview

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listView"
            android:divider="#ad5"
            android:dividerHeight="2dp"
            android:layout_below="@+id/searchView"/>
        <SearchView
            android:id="@+id/searchView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:queryHint="Search Here"
            android:iconifiedByDefault="false"
            android:layout_alignParentTop="true"/>
    
    </RelativeLayout>
        
      

It is the XML file of my search view.It contain Search View and List view它是我的搜索视图的 XML 文件。它包含搜索视图和列表视图

public class MainActivity extends AppCompatActivity {
            SearchView searchView;
            ListView listView;
            ArrayList list;
            ArrayAdapter adapter;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                searchView = findViewById(R.id.searchView);
                listView = findViewById(R.id.listView);
                list = new ArrayList<>();
                list.add("Apple");
                list.add("Banana");
                list.add("Pineapple");
                list.add("Orange");
                list.add("Mango");
                list.add("Grapes");
                list.add("Lemon");
                list.add("Melon");
                list.add("Watermelon");
                list.add("Papaya");
                adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
                listView.setAdapter(adapter);
                searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
            adapter.getFilter().filter(query.trim(), new Filter.FilterListener() {
                @Override
                public void onFilterComplete(int count) {
                    if(count==0){
                        Toast.makeText(MainActivity.this, "No Match found",Toast.LENGTH_LONG).show();
                    }
                }
            });
        return false;
    }
                    @Override
                    public boolean onQueryTextChange(String newText) {
                        adapter.getFilter().filter(newText);
                      //Toast.makeText(getApplicationContext(),newText,Toast.LENGTH_SHORT).show();
                        return false;
        
                                }
                });
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Toast.makeText(MainActivity.this,list.get(position)+"",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
        
   

This is the class for my search view but the problem is if I select orange directly in listview it will toast orange,and so on.But while I searching for orange if I give 'o' it will show orange in list view but when I click orange it will toast apple.why this happen how to get the problem solved..Thanks in advance这是我的搜索视图的 class 但问题是如果我直接在列表视图中使用 select 橙色,它将烤橙色,依此类推。但是当我搜索橙色时,如果我给出“o”,它将在列表视图中显示橙色但是当我单击橙色它会烤苹果。为什么会发生这种情况如何解决问题..提前谢谢

All you need to do it filter the result and validate the new size of item count.您需要做的就是过滤结果并验证项目计数的新大小。 You can use filter variant to listen for List size if its 0 then you can so Error message.您可以使用过滤器变体来侦听列表大小,如果它为 0,那么您可以这样做错误消息。

public boolean onQueryTextSubmit(String query) {
        adapter.getFilter().filter(query.trim(), new Filter.FilterListener() {
            @Override
            public void onFilterComplete(int count) {
                if(count==0){
                    Toast.makeText(MainActivity.this, "No Match found",Toast.LENGTH_LONG).show();
                }
            }
        });
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM