使用“回收站”视图中的“搜索”视图搜索项目时退出应用程序

时间:2017-12-19 05:41:05

标签: android android-recyclerview android-volley searchview

我在SearchView中使用RecyclerView来搜索项目。当我在SearchView中输入内容时,不会显示过滤结果并退出应用。它不提供任何日志。我很困惑在哪里使用notifyDataSetChanged方法 这是Adapter类的代码。

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);

}

2 个答案:

答案 0 :(得分:0)

您可以尝试使用SearchView并在SearchView.OnQueryTextListenerActivity中实施Fragment

    @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;
}

答案 1 :(得分:0)

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;
    }