如何实现onTextChangedListenerItem来搜索RecyclerView中的项目

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何实现onTextChangedListenerItem来搜索RecyclerView中的项目相关的知识,希望对你有一定的参考价值。

我正在使用来显示片段上的项目。我已经使用在我的应用程序中实现了searh功能。当我要在中键入文本时,视图中的所有项目都消失了,我无法获得过滤结果。这是my的代码

private void addTextListener() {
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                 s=s.toString().toLowerCase();
                 final ArrayList<CurrentEntry> filter=new ArrayList<>();
                 for(int i=0;i<filter.size();i++)
                 {
                     final  String text=current.get(i).toString().toLowerCase();
                     if(text.contains(s))
                     {
                         filter.add(current.get(i));
                     }

                 }
                 recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                 adapt=new NewAdapter(filter,getActivity());
                 recyclerView.setAdapter(adapt);
                 adapt.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable s) {
                //filter(s.toString());
            }
        });
    }  

这是我使用

private void getData() {
        String json_req = "json_req";
        String url =getContext().getString(R.string.CURRENT_STATUS);


        JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                       try
                       {

                           JSONObject object=new JSONObject(response.toString());
                           JSONArray data=object.getJSONArray("results");
                           for(int i=0;i<data.length();i++)
                           {
                               JSONObject json = data.getJSONObject(i);
                             final   CurrentEntry c=new CurrentEntry();
                               userUrl=json.getString("name");
                               JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, userUrl, null, new Response.Listener<JSONObject>() {
                                   @Override
                                   public void onResponse(JSONObject response) {
                                       try {
                                           String foodie_name=response.getString("username");
                                           c.setName(name);
                                       } catch (JSONException e) {
                                           e.printStackTrace();
                                       }
                                       adapt.notifyDataSetChanged();
                                   }
                               }, new Response.ErrorListener() {
                                   @Override
                                   public void onErrorResponse(VolleyError error) {

                                   }
                               }
                               )

                               {
                                   @Override
                                   public Map<String, String> getHeaders() throws AuthFailureError {
                                       Map<String, String> headers = new HashMap<>();
                                       String credentials = ""+":"+"";
                                       String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                                       headers.put("Content-type", "application/json");
                                       headers.put("Authorization", auth);
                                       return headers;
                                   }
                               };
                               AppController.getInstance().addToRequestQueue(request,second_req);
                               c.setPeople(json.getString("my_people"));
                               String temp=json.getString("my_timetime");
                               temp=temp.substring(11);
                               temp=temp.substring(0,temp.length()-4);
                               c.setEstimate(temp);
                               current.add(c);

                            }

                           recyclerView.setAdapter(adapt);
                           adapt.notifyDataSetChanged();
                       }
                       catch (JSONException e)
                       {

                       }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }

                })
        {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                String credentials = ""+":"+"";
                String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                headers.put("Content-type", "application/json");
                headers.put("Authorization", auth);
                return headers;
            }
        };

        AppController.getInstance().addToRequestQueue(req, json_req);
    }

如何解决这个问题,以便在中获得过滤结果。

答案

删除此代码

adapt=new NewAdapter(filter,getActivity());
recyclerView.setAdapter(adapt);
adapt.notifyDataSetChanged();

每次更改文本时都不需要创建NewAdapter。相反,尝试更改适配器列表的值并调用notifyDataSetChanged(),如下所示。

adapt.filter=filter;
adapt.notifyDataSetChanged();

并在你的onCreate()中添加这些行

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapt=new NewAdapter(filter,getActivity());
recyclerView.setAdapter(adapt);
另一答案

在您的代码中,您一次又一次地调用以下代码;

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapt=new NewAdapter(data,getActivity());
recyclerView.setAdapter(adapt);
adapt.notifyDataSetChanged();

只需在片段的onCreateView中编写此代码一次:

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapt=new NewAdapter(data,getActivity());
recyclerView.setAdapter(adapt);

现在,在您的适配器类中,您需要拥有一个数据设置器,可用于设置从您的API中获取的更新数据,以及您视图中的搜索过滤器。并发布你只需要调用adapt.notifyDataSetChanged():

adapt.setNewData(data);
adapt.notifyDataSetChanged();

以上是关于如何实现onTextChangedListenerItem来搜索RecyclerView中的项目的主要内容,如果未能解决你的问题,请参考以下文章

请问SQL如何实现批量查询

如何快速自己实现Map

API 面试四连杀:接口如何设计?安全如何保证?签名如何实现?防重如何实现?

delphi 中如何实现对象之间的数据共享

C#如何实现进程单例运行

Android如何实现弹幕效果