RecyclerView中没有数据时过滤错误空指针

时间:2019-05-10 21:56:39

标签: android android-recyclerview

我在RecylerView上有一个过滤器,并且可以正常工作,但是当我的recyclerView没有来自API的数据并且有人在过滤器上搜索了一些文本时,我的应用程序将崩溃错误空指针

这是我的adapter

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder>
    {
        private ArrayList<DataAssetReceive> dataList;
        private List<DataAssetReceive> filterlist = null;

        public ListAdapter(ArrayList<DataAssetReceive> data)
        {
            this.dataList = data;
            this.filterlist = new ArrayList(dataList);
        }

        public class ViewHolder extends RecyclerView.ViewHolder
        {
            TextView idWarehouse;
            TextView textViewAssetDetail;
            TextView textViewAgreement;
            TextView textViewCustomer;
            TextView textViewLicense;
            Button btnReceive;

            public ViewHolder(View itemView)
            {
                super(itemView);
                this.idWarehouse = (TextView) itemView.findViewById(R.id.id_warehous);
                this.textViewAssetDetail = (TextView) itemView.findViewById(R.id.assetDetail);
                this.textViewAgreement = (TextView) itemView.findViewById(R.id.agreementNo);
                this.textViewCustomer = (TextView) itemView.findViewById(R.id.customername);
                this.textViewLicense = (TextView) itemView.findViewById(R.id.licenseplate);
                this.btnReceive = (Button) itemView.findViewById(R.id.receiveBtn);
            }
        }

        @Override
        public ListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false);

            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(ListAdapter.ViewHolder holder, final int position)
        {
            holder.textViewAgreement.setText(filterlist.get(position).getAgreement_no());
            holder.textViewAssetDetail.setText(filterlist.get(position).getAsset_description() +" - "+filterlist.get(position).getManufacturing_year());
            holder.textViewCustomer.setText(filterlist.get(position).getCustomer_name());
            holder.textViewLicense.setText(filterlist.get(position).getLicense_plate());

            holder.btnReceive.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    //Toast.makeText(getActivity(), "Item " + position + " is clicked.", Toast.LENGTH_SHORT).show();
                    cekValidate(filterlist.get(position).getWarehouse_order_id(),new VolleyCallback(){
                        @Override
                        public void onSuccess(String result){
                            //Toast.makeText(getActivity(), "validate " + result, Toast.LENGTH_SHORT).show();

                            if(result.equals("1")){
                                setFlagging(filterlist.get(position).getWarehouse_order_id());
                                db.addReceive(filterlist.get(position).getWarehouse_order_id(),filterlist.get(position).getCustomer_name(),filterlist.get(position).getAgreement_no(),filterlist.get(position).getLicense_plate(),filterlist.get(position).getAsset_description(),filterlist.get(position).getManufacturing_year(),filterlist.get(position).asset_type,"Y");
                                Intent intent = new Intent(getActivity(),
                                        ReceiveActivity.class);
                                intent.putExtra("name",filterlist.get(position).getCustomer_name());
                                intent.putExtra("code",filterlist.get(position).getAgreement_no());
                                intent.putExtra("plat",filterlist.get(position).getLicense_plate());
                                intent.putExtra("desc",filterlist.get(position).getAsset_description());
                                intent.putExtra("year",filterlist.get(position).getManufacturing_year());
                                intent.putExtra("asset_type",filterlist.get(position).getAsset_type());
                                intent.putExtra("idwarehouse",filterlist.get(position).getWarehouse_order_id());
                                startActivity(intent);
                            }else{
                                Toast.makeText(getActivity(), "maaf, data sedang di ceklis, silahkan coba data yang lain", Toast.LENGTH_SHORT).show();
                            }

                        }
                    });

                }
            });
        }

        @Override
        public int getItemCount()
        {
            if(filterlist != null){
                return filterlist.size();
            }
            return 0;
        }

        public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            if(filterlist != null) {
                filterlist.clear();
                if (charText.length() == 0) {
                    filterlist.addAll(dataList);
                } else {
                    for (DataAssetReceive wp : dataList) {
                        if (wp.getCustomer_name().toLowerCase().contains(charText.toLowerCase()) || wp.getAgreement_no().toLowerCase().contains(charText.toLowerCase())
                                || wp.getAsset_description().toLowerCase().contains(charText.toLowerCase()) || wp.getLicense_plate().toLowerCase().contains(charText.toLowerCase())) {
                            //Toast.makeText(getActivity(), "data " + wp.getCustomer() , Toast.LENGTH_SHORT).show();
                            filterlist.add(wp);
                        }
                    }
                }
            }
            notifyDataSetChanged();
        }

    }

这是afterTextChangeListener

searchHome.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence query, int i, int i1, int i2) {


            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(editable.toString().trim().length()>0) {
                    String text = searchHome.getText().toString().toLowerCase(Locale.getDefault());
                    mListadapter.filter(text);
                }
            }
        });

如何防止此过滤器出错,我已经添加了“ if(filterlist!= null)”,但仍然出错

1 个答案:

答案 0 :(得分:0)

您的应用程序崩溃,因为当列表为空时,可能没有用于过滤的字符串,我建议您:

  • ISet<>上添加List<>,以避免在列表为空时崩溃
  • 或者在乞求中检查您的列表是否为空,请不要执行搜索并显示public class RequestDto { public RequestDto() { TypeNames = new HashSet<string>(); TypeNames.Add(new string("Type1")); TypeNames.Add(new string("Type2")); TypeNames.Add(new string("Type3")); } public string CompanyName { get; set; } public string Version { get; set; } public ISet<string> TypeNames { get; set; } } 来通知用户列表为空。