• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Filter My Recyclerview

GotParty

Lurker
Nov 27, 2019
3
0
I'm trying to implement a search view for my hockey app this is the error I get.

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference

Here is my code

public class HockeyAdapter extends RecyclerView.Adapter<HockeyAdapter.ViewHolder> implements Filterable {

private ArrayList<Player> playerList;
private List<Player> playerFull;
RequestOptions option;
private Context context;

public HockeyAdapter(ArrayList<Player> playerList, Context context) {
this.playerList = playerList;
this.context = context;
playerFull = new ArrayList<>(playerList);


//request glide
option = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);

}


@override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
view = layoutInflater.inflate(R.layout.hockey_cell, parent, false);
return new ViewHolder(view);
}


@override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final Player player = playerList.get(position);


//Load image with glide
Glide.with(context).asBitmap()
.load(player.getImage())
.centerCrop()
.apply(option)
.into(holder.pImg);


//Load text
holder.name.setText(player.getName());
holder.position.setText(player.getPosition());
holder.team.setText(player.getTeam());


}


@override
public int getItemCount() {
return playerList.size();
}

@override
public Filter getFilter() {
return exampleFilter;
}

private Filter exampleFilter = new Filter() {
@override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList<Player> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(playerFull);
} else {
String filteredPattern = constraint.toString().toLowerCase().trim();
for (Player item : playerFull) {
if (item.getName().toLowerCase().contains(filteredPattern)) {
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}

@override
protected void publishResults(CharSequence constraint, FilterResults results) {
playerList.clear();
if (results.values != null) {
playerList.addAll((Collection<? extends Player>) results.values);
}
notifyDataSetChanged();
}
};


public class ViewHolder extends RecyclerView.ViewHolder {

public final ImageView pImg;
public final TextView name, position, team;


public ViewHolder(@NonNull View view) {
super(view);
this.name = view.findViewById(R.id.name);
this.position = view.findViewById(R.id.position);
this.pImg = view.findViewById(R.id.imageView);
this.team = view.findViewById(R.id.team);
}
}
}







 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones