中
我正在使用应用程序中的标头进行回收视图,并且我正在尝试在应用程序中实现搜索视图,但它不起作用。当我单击搜索视图并键入某些内容时,结果未过滤,我将其放在日志猫中。
2019-06-23 14:00:48.471 27193-27508/com.example.testapp W/Filter: An exception occured during performFiltering()!
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference
at com.example.testapp.TitleAdapter$1.performFiltering(TitleAdapter.java:92)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:171)
at android.os.HandlerThread.run(HandlerThread.java:65)
我已经呆了几个小时了,无法弄清楚我在做什么错。这是我的适配器。
public class TitleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private List <ItemObject> list;
private List <ItemObject> listFiltered;
private Context context;
public TitleAdapter(Context context, List <ItemObject> list) {
this.context = context;
this.list = list;
this.listFiltered = list;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_layout, parent, false);
return new HeaderViewHolder(layoutView);
} else if (viewType == TYPE_ITEM) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new ItemViewHolder(layoutView);
}
throw new RuntimeException("No match for " + viewType + ".");
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
ItemObject list = this.list.get(position);
if (holder instanceof HeaderViewHolder) {
((HeaderViewHolder) holder).headerTitle.setText(list.getHeaderTitle());
} else if (holder instanceof ItemViewHolder) {
((ItemViewHolder) holder).titleText.setText(list.getPageName());
}
}
public ItemObject getItem(int position) {
return list.get(position);
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
ItemObject mObject = list.get(position);
return mObject.isHeader();
}
@Override
public Filter getFilter() {
return new Filter() {
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint == null || constraint.length() == 0){
listFiltered = list;
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
List<ItemObject> lstFilter = new ArrayList<>();
for (ItemObject row : list) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getPageName().toLowerCase().contains(filterPattern)) {
lstFilter.add(row);
}
}
listFiltered = lstFilter;
}
FilterResults filterResults = new FilterResults();
filterResults.values = listFiltered;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
listFiltered = (List <ItemObject>) results.values;
// refresh the list with filtered data
notifyDataSetChanged();
}
};
}
public class HeaderViewHolder extends RecyclerView.ViewHolder {
TextView headerTitle;
public HeaderViewHolder(View itemView) {
super(itemView);
headerTitle = itemView.findViewById(R.id.headerText);
}
}
public class ItemViewHolder extends RecyclerView.ViewHolder{
TextView titleText;
public ItemViewHolder(View itemView) {
super(itemView);
titleText = itemView.findViewById(R.id.titleText);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
Intent intent = new Intent(context, ViewerActivity.class);
intent.putExtra("PageName",list.get(position).getPageName());
intent.putExtra("PageUrl",list.get(position).getPageUrl());
context.startActivity(intent);
}
});
}
}
}
顺便说一句,如果您想知道这是第92行
if (row.getPageName().toLowerCase().contains(filterPattern)) {
lstFilter.add(row);
}
如您所知,null
在getPageName()
上有例外,因此只需检查
if(row.getPageName()!=null && !row.getPageName().isEmpty())
if (row.getPageName().toLowerCase().contains(filterPattern)) {
lstFilter.add(row);
}
我看到您在getItemCount()
中有list
对象,因此我认为主要"列表"是list
,因此您必须备份此对象,并且在此行中执行以下操作:
@Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
//listFiltered = (List <ItemObject>) results.values;
//back up `list` some where else for reloading the list without filter
list.clear();
list.addAll((List <ItemObject>) results.values);
// refresh the list with filtered data
notifyDataSetChanged();
}
备份主数据并将过滤器放入