Recycler Search视图为过滤方法和应用程序关闭提供了依据

时间:2017-12-27 06:29:22

标签: android android-recyclerview searchview addtextchangedlistener

我想在中搜索项目。我正在使用方法进行搜索。当我开始输入来搜索某个项目而不是给我过滤后的结果时,应用程序会关闭,我收到了错误

12-27 11:29:04.375 18962-18962/com.example.satyabhai.restaurant E/RecyclerView: No adapter attached; skipping layout
12-27 11:31:17.932 18962-18962/com.example.satyabhai.restaurant E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.satyabhai.restaurant, PID: 18962
                                                                                  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference
                                                                                      at com.example.satyabhai.restaurant.CurrentStatus.filter(CurrentStatus.java:121)
                                                                                      at com.example.satyabhai.restaurant.CurrentStatus.access$000(CurrentStatus.java:40)
                                                                                      at com.example.satyabhai.restaurant.CurrentStatus$1.afterTextChanged(CurrentStatus.java:95)  

这是我的搜索

的代码
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) {

        }

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

这是我的Filter方法的代码

private void filter(String s) {
        ArrayList<CurrentEntry> temp=new ArrayList<>();
        for(CurrentEntry d: current)
        {
            if(d.getName().toLowerCase().contains(s)||d.getNo().toLowerCase().contains(s)||d.getPeople().toLowerCase().contains(s)||d.getEstimate().toLowerCase().contains(s));
            {
                temp.add(d);
            }

        }
        adapt.updateList(temp);
    }

这是UpdateList的代码

 public void updateList(ArrayList<CurrentEntry> temp)
    {
       this.listItems=temp;
        notifyDataSetChanged();
    }

这是用于设置

的MainActivity代码
 RecyclerView recyclerView;
    NewAdapter adapt;
    private ArrayList<CurrentEntry> current;
    String userUrl;
    String second_req="second_req";
    EditText search;

    public CurrentStatus() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View v=inflater.inflate(R.layout.current_status,container,false);
        FloatingActionButton fab=(FloatingActionButton)v.findViewById(R.id.fab);
        current=new ArrayList<>();
        recyclerView=(RecyclerView)v.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
      search=(EditText)v.findViewById(R.id.search);

此行正在解决问题

if(!TextUtils.isEmpty(d.getName())|| !TextUtils.isEmpty(d.getPeople())||!TextUtils.isEmpty(d.getNo())|| !TextUtils.isEmpty(d.getEstimate())) {
                if (d.getName().toLowerCase().contains(s) || d.getNo().toLowerCase().contains(s) || d.getPeople().toLowerCase().contains(s) || d.getEstimate().toLowerCase().contains(s));
                {
                    temp.add(d);
                }

这是适配器

的代码
public class NewAdapter extends RecyclerView.Adapter<NewAdapter.ViewHolder> {

     ArrayList<CurrentEntry> listItems;
     Context context;

    public NewAdapter (ArrayList<CurrentEntry> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }

    public void updateList(ArrayList<CurrentEntry> temp)
    {
       this.listItems=temp;
        notifyDataSetChanged();
    }

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



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

        }
    }
    @Override
    public int getItemCount() {
        return listItems.size();
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个

1.在将字符串转换为toLowerCase之前检查字符串是否可用tp避免此问题

    private void filter(String s) {
        ArrayList<CurrentEntry> temp=new ArrayList<>();
        for(CurrentEntry d: current)
        {

            if(!TextUtils.isEmpty(d.getName())&&!TextUtils.isEmpty(d.getNo())&&!TextUtils.isEmpty(d.getPeople())&&!TextUtils.isEmpty(d.getEstimate()))
            {
                if(d.getName().toLowerCase().contains(s)||d.getNo().toLowerCase().contains(s)||d.getPeople().toLowerCase().contains(s)||d.getEstimate().toLowerCase().contains(s));
                {
                    temp.add(d);
                }
            }
            else {
                //String is empty
            }

        }
        adapt.updateList(temp);
    }

2. recyclerview No adapter attached; skipping layout表示此错误。添加recycleview

的布局管理器
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    list.setLayoutManager(llm);
    list.setAdapter( adapter );