使用回收器视图中的搜索视图搜索项目时,应用程序被退出



我正在使用RecyclerView中的SearchView搜索项目。当我在SearchView中键入某些内容时,未显示过滤器结果,并且该应用程序将退出。它没有提供任何日志。我很困惑在哪里使用notifyDataSetChanged方法。
这是适配器类的代码。

public class NewAdapter extends RecyclerView.Adapter<NewAdapter.ViewHolder> implements Filterable{
    private ArrayList<CurrentEntry> listItems;
   private ArrayList<CurrentEntry> filterList;
   Context context;
    public NewAdapter(ArrayList<CurrentEntry> listItemss, Context context) {
      //  this.listItems = listItems;
       this.listItems=listItemss;
       this.filterList=listItems;
        this.context = context;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_card,parent,false);
        return new ViewHolder(v);
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
          /* CurrentEntry current=listItems.get(position);
           holder.number.setText(current.getNo());
           holder.Name.setText(current.getName());
           holder.People.setText(current.getPeople());
           holder.Estimate.setText(current.getEstimate());
           holder.Foodie.setText(current.getFoodie());*/
          holder.number.setText(filterList.get(position).getNo());
          holder.Name.setText(filterList.get(position).getName());
          holder.People.setText(filterList.get(position).getPeople());
          holder.Estimate.setText(filterList.get(position).getEstimate());
          holder.Foodie.setText(filterList.get(position).getFoodie());
    }
    @Override
    public int getItemCount() {
        return listItems.size();
    }
    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                String charString=constraint.toString();
                if(charString.isEmpty())
                {
                    filterList=listItems;
                }
                else
                {
                    ArrayList<CurrentEntry> myList=new ArrayList<>();
                    for(CurrentEntry temp:listItems)
                    {
                        if(temp.getName().toLowerCase().contains(charString));
                        filterList.add(temp);
                    }
                    filterList=myList;
                }
                    FilterResults filterResults=new FilterResults();
                filterResults.values=filterList;
                return filterResults;
            }
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                             filterList=(ArrayList<CurrentEntry>) results.values;
                          notifyDataSetChanged();
            }
        };
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView number;
        TextView Name ;
        TextView People;
        TextView Estimate;
        TextView Foodie;
        public ViewHolder(View itemView) {
            super(itemView);
            number=(TextView)itemView.findViewById(R.id.no);
            Name=(TextView)itemView.findViewById(R.id.name);
            People=(TextView)itemView.findViewById(R.id.people);
            Estimate=(TextView)itemView.findViewById(R.id.estimate);
            Foodie=(TextView)itemView.findViewById(R.id.foodie);
        }
    }  

这是Json请求的代码

 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();
                            newUrl=json.getString("foodie");
                            JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, newUrl, null, new Response.Listener<JSONObject>()
                            {
                                @Override
                                public void onResponse(JSONObject response)
                                {
                                    try
                                    {
                                        String foodie_name=response.getString("username");
                                        c.setFoodie(foodie_name);
                                    } catch (JSONException e)
                                    {
                                        e.printStackTrace();
                                    }
                                    adapter.notifyDataSetChanged();
                                }
                            }, new Response.ErrorListener()
                            {
                                @Override
                                public void onErrorResponse(VolleyError error)
                                {
                                }
                            })
                            {
                                  AppController.getInstance().addToRequestQueue(request,second_req);
                             c.setPeople(json.getString("no_people"));
                             c.setEstimate(json.getString("predicated_time"));
                            current.add(c);
                        }
                    }
                    catch (JSONException e)
                    {
                    }
                    adapter.notifyDataSetChanged();
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error)
                {
                    if (error != null)
                    {
                        //  Log.d(TAG, error.toString())
                        // Toast.makeText(getApplicationContext(), "Enter Valid Username and Password", Toast.LENGTH_LONG).show();
                    }
                }
            })
    };
    AppController.getInstance().addToRequestQueue(req,json_req);
}

您可以尝试使用SearchView并在ActivityFragment中实现SearchView.OnQueryTextListener

    @Override
public boolean onQueryTextSubmit(String query) {
    return false;
}
@Override
public boolean onQueryTextChange(String newText) {
    //write your filtering code here and notify (with optional animation) your adapter
}

过滤逻辑是您实施的主观,我使用了以下方式:

    private ArrayList<FilterItems> filter(ArrayList<FilterItems> filterItemList, String query) {
    query = query.toLowerCase();
    final ArrayList<FilterItems> filteredfilterItemList = new ArrayList<>();
    for (FilterItems filterItem : filterItemList) {
        final String text = filterItem.getItem().toLowerCase();
        if (text.contains(query)) {
            filteredfilterItemList.add(filterItem);
        }
    }
    return filteredfilterItemList;
}

将您的getFilter方法更改为

@Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                String charString=constraint.toString().toLowerCase();
                 ArrayList<CurrentEntry> myList=new ArrayList<>();
                if(charString.isEmpty())
                {
                    myList.addAll(listItems);
                }
                else
                { 
                    for(CurrentEntry temp:listItems)
                    {
                        if(temp.getName().toLowerCase().contains(charString));
                        myList.add(temp);
                    }
                }
                FilterResults filterResults=new FilterResults();
                filterResults.values= myList;
                return filterResults;
            }
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                             filterList.clear();
                             filterList.addAll((ArrayList<CurrentEntry>) results.values);
                          notifyDataSetChanged();
            }
        };
    }

编辑

将您的constructor更改为

public NewAdapter(ArrayList<CurrentEntry> listItemss, Context context) {
      //  this.listItems = listItems;
       this.listItems=listItemss;
       this.filterList = new ArrayList<>();
       this.filterList.addAll(listItems);
        this.context = context;
    }

相关内容

  • 没有找到相关文章

最新更新