SearchView工具栏界面中未弹出项目Android Studio Java

如何解决SearchView工具栏界面中未弹出项目Android Studio Java

大家好,我想在工具栏上实现一个搜索界面,以便在GridView中搜索项目。但是它显示在我的工具栏上,但是当我单击搜索图标并开始键入内容时,没有任何提示。我对当前的问题不知所措。我使用自定义适配器,因此我相信它还有很多工作要做。

这是我的自定义ArrayAdapter类(WordAdapter)。将内容集中在getView()函数之后,因为我认为这是类中最相关的部分。我还添加了一些全局变量,例如ArrayList list和listFull,因为我认为我需要整个项目列表的一些副本,但老实说,我不确定该怎么做。


    //variable responsible for making checkbox visible or not
    private boolean displayCheckBox;
    private ArrayList<WordFolder> original_list;
    private ArrayList<WordFolder> new_list;

    //constructor - it takes the context and the list of words
    WordAdapter(Context context,ArrayList<WordFolder> word){
        super(context,word);

        //creating a copy of the ArrayList containing all the folders name
        original_list = new ArrayList<>(word);
        new_list = new ArrayList<>(word);

    }

    //sets the visibility of the checkBox
    public void setCheckBoxVisibility(boolean visible){
        this.displayCheckBox = visible;
    }


    @Override
    public View getView(int position,View convertView,ViewGroup parent){
        View listItemView = convertView;
        if(listItemView == null){
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view,parent,false);
        }

        //getting the checkBox view id
        CheckBox checkBox = (CheckBox) listItemView.findViewById(R.id.check_box);
        checkBox.setVisibility(displayCheckBox ? View.VISIBLE : View.GONE);


        //Getting the current word
        final WordFolder currentWord = getItem(position);

        //making the 3 text view to match our word_folder.xml
        TextView date_created = (TextView) listItemView.findViewById(R.id.date_created);

        TextView title = (TextView) listItemView.findViewById(R.id.title);

        TextView desc = (TextView) listItemView.findViewById(R.id.desc);

        //using the setText to get the text and set it in the textView
        date_created.setText(currentWord.getDateCreated());

        title.setText(currentWord.getTitle());

        desc.setText(currentWord.getTitleDesc());

        //call automatically when checkbox is changed
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            @Override
            //compound button = the view of the button
            //b = the new state of the checkbox
            public void onCheckedChanged(CompoundButton compoundButton,boolean b) {
                //set the value of the checkbox to the CurrentWord
                currentWord.setChecked(b);
            }
        });

        return listItemView;

    }
    
    @NonNull
    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {

                FilterResults filterResults = new FilterResults();

                if (charSequence == null || charSequence.length() == 0) {
                    filterResults.count = original_list.size();
                    filterResults.values = original_list;
                } else {
                    String searchStr = charSequence.toString().toLowerCase();
                    ArrayList<WordFolder> results = new ArrayList<>();
                    for (WordFolder item : original_list) {
                        if (item.getTitle().contains(searchStr)) {
                            results.add(item);
                        }
                        filterResults.count = results.size();
                        filterResults.values = results;
                    }
                }

                //new list which contains only filtered items
                ArrayList<WordFolder> filteredList = new ArrayList<>();
                if(charSequence == null || charSequence.length() == 0){
                    filteredList.addAll(original_list);
                }
                else{
                    String filterPattern = charSequence.toString().toLowerCase().trim();

                    for(WordFolder item : original_list){
                        if(item.getTitle().toLowerCase().contains(filterPattern)){
                            filteredList.add(item);
                        }
                    }
                }

                FilterResults results = new FilterResults();
                results.values = filteredList;
                return results;
            }

            @Override
            protected void publishResults(CharSequence charSequence,FilterResults filterResults) {
                new_list.clear();
                new_list.addAll((ArrayList) filterResults.values);
                notifyDataSetChanged();

            }
        };
        return filter;
    }
}

这是MainActivity中代码的一部分,我创建了Search界面以供参考。 Btw itemadapter是我的自定义适配器变量的名称

    public boolean onCreateOptionsMenu(Menu menu) {
        // initialize menu inflater
        MenuInflater inflater = getMenuInflater();
        if(whichToolbar == 0){
            //inflate menu
            inflater.inflate(R.menu.menu_search,menu);
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);
            //initialize menu item
            MenuItem searchItem = menu.findItem(R.id.search);));
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String s) {
                    itemAdapter.getFilter().filter(s);
                    return true;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    itemAdapter.getFilter().filter(newText);
                    return true;
                }
            });
            searchView.setOnCloseListener(new SearchView.OnCloseListener() {
                @Override
                public boolean onClose() {
                    itemAdapter.getFilter().filter("");
                    return false;
                }
            });
             
        }
        else{
            inflater.inflate(R.menu.delete_menu,menu);
        }
        return true;
    }

我已更新此问题,因为尚未回答问题。我一直在关注和观看Youtube视频(不仅是1个vid),还一直在关注android开发人员指南,并确保代码“正常运行”,但我的搜索栏目前什么也不做。

解决方法

在适配器中使用它:

private ArrayList<WordFolder> actualdata = new ArrayList<WordFolder>();
    private ArrayList<WordFolder> orignallist= new ArrayList<>();

WordAdapter(Context context,ArrayList<WordFolder> word){
        super(context,word);

         this.actualdata=new ArrayList<WordFolder>();
        this.orignallist=new ArrayList<WordFolder>();
        actualdata.addAll(word);//use this to set up your view
        orignallist.addAll(word);


    }


public void filterData(String query){
        query=query.toLowerCase();
 
        actualdata.clear();
        if(query.isEmpty()){
            actualdata.addAll(orignallist);
        }
        else {
            ArrayList<WordFolder> newlist = new ArrayList<>();
            for(WordFolder gd: orignallist) {

                if ((gd.getTitle().contains(query))  ) {
                    newlist.add(gd);
                }
            }

            if(newlist.size()> 0){

                actualdata.addAll(newlist);
            }

        }

        notifyDataSetChanged();
    }

然后将您的主类itemAdapter.getfilter()更改为itemAdapter.filterData("your query")

现在应该可以了

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-