У меня есть фильтр на 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)», но все еще ошибка