1. Are you ready for the Galaxy S20? Here is everything we know so far!

Filter My Recyclerview

Discussion in 'Android Development' started by GotParty, May 30, 2020.

  1. GotParty

    GotParty Lurker
    Thread Starter

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







     


  2. marcelpreda

    marcelpreda Member

    And the question is?
     
  3. GotParty

    GotParty Lurker
    Thread Starter

    How do I get my search view to work?
     
  4. wseng92

    wseng92 Android Enthusiast

    Perhaps playerFull is null?
     
  5. My suggestion is to make sure that item.getName() is actually returning a non-null string, or to check that it is non-null. It appears that the variable returned by getName() is not getting initialized by the Player Class.
     
Loading...

Share This Page

Loading...