0

I m currently working on a android apps which fetch data from parse server. I wanted to filter my recyclerView by using searchview. But it shows me nothing while search. It shows me error in this line mRooms = (ArrayList) filterResults.values; Please help me to edit my code regarding this issues.

RoomCardRecyclerViewAdapter

 private List<ParseObject> mRooms = new ArrayList<>();
 private ParseObject room;
 private String mSection;
 private Context context;

 public RoomCardRecyclerViewAdapter(){
    super(DIFF_CALLBACK);
 }
 public static final DiffUtil.ItemCallback<ParseObject>  DIFF_CALLBACK = new 
 DiffUtil.ItemCallback<ParseObject>() {
    @Override
    public boolean areItemsTheSame(@NonNull ParseObject oldItem, @NonNull ParseObject newItem) {
        return oldItem.getObjectId() == newItem.getObjectId();
    }

    @Override
    public boolean areContentsTheSame(@NonNull ParseObject oldItem, @NonNull ParseObject newItem) {
        return (oldItem.getUpdatedAt().equals(newItem.getUpdatedAt()) && 
 oldItem.getCreatedAt().equals(newItem.getCreatedAt()));
    }
};


public RoomCardRecyclerViewAdapter(String section) {
    this();

    this.mSection = section;
}
public RoomCardRecyclerViewAdapter(Context context, List<ParseObject>arrayList) {
    this();

    this.context = context;
    mRooms = arrayList;
     
}


@Override
public RoomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //inflating the viewholder with the appropriate views
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.room_cardview, parent, 
false);

    return new RoomViewHolder(parent.getContext(), view);
}

@Override
public void onBindViewHolder(@NonNull RoomViewHolder holder, int position) {
    room = getItem(position);

     holder.mRoomLocation.setText(room.getString("roomSuburb"));
    holder.mRoomPrice.setText(Integer.toString(room.getInt("roomMonthlyRent")));
    holder.mInclusiveOrNot.setText(room.getString("roomRentInclusiveOfBills"));
    holder.mPropertyType.setText(room.getString("roomPropertyType"));
    holder.mNumOfBeds.setText(Integer.toString(room.getInt("roomBedrooms")));
    holder.mNumOfBaths.setText(Integer.toString(room.getInt("roomBathrooms")));


  }
  @Override
  public Filter getFilter(){
  return new Filter() {
      @Override
      protected FilterResults performFiltering(CharSequence charSequence) {

FilterResults filterResults = new FilterResults();

          String charString = charSequence.toString();
if (charString.isEmpty()){
     filterResults.values = mRooms;
    filterResults.count = mRooms.size();

}else {
List<ParseObject> filteredList = new ArrayList<>();
for (ParseObject parseObject : mRooms){
    if (parseObject.getString("roomSuburb").toLowerCase().contains(charString.toLowerCase())){
        filteredList.add(parseObject);
    }
}
     filterResults.values = filteredList;
filterResults.count= filteredList.size();
}

return filterResults;

      }

      @Override
      protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
          mRooms = (ArrayList<ParseObject>) filterResults.values;
          notifyDataSetChanged();
      }
  };

}

2 Answers 2

1

You just need to call notifyDataSetChanged() as below -

Use this

 @Override
 protected void publishResults(CharSequence charSequence, FilterResults 
 filterResults) { 
     if (filterResults!= null && results.count > 0) {
         notifyDataSetChanged();
     } else {
         notifyDataSetInvalidated();
     }
 }
Sign up to request clarification or add additional context in comments.

2 Comments

It has been solved the issue. But its not giving me any results while searching
Kindly accept the answer if it worked for you. And also share the whole code to resolve further issue.
0

Define an additional variable mFilteredRooms in the constructor and set it to arrayList

private List<ParseObject> mFilteredRooms = new ArrayList<>();

    public RoomCardRecyclerViewAdapter(Context context, List<ParseObject> arrayList) {
        this();
    
        this.context = context;
        mRooms = arrayList;
        mFilteredRooms = arrayList;
         
    }

Now,

@Override
  public Filter getFilter(){
  return new Filter() {
      @Override
      protected FilterResults performFiltering(CharSequence charSequence) {

          FilterResults filterResults = new FilterResults();
          ArrayList<ParseObject> filteredList = new ArrayList<>();

          
          if (charSequence == null || charSequence.toString().trim().equals("")){
              filteredList.addAll(mFilteredRooms);

          }else {
              String charString = charSequence.toString();
              
              for (ParseObject parseObject : mFilteredRooms){
              if (parseObject.getString("roomSuburb").toLowerCase().contains(charString.toLowerCase())){
                  filteredList.add(parseObject);
               }
          }
          filterResults.values = filteredList;
          filterResults.count= filteredList.size();
      }

      return filterResults;

      }

      @Override
      protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
          mRooms = (List<ParseObject>) filterResults.values;
          notifyDataSetChanged();
      }
  };

1 Comment

Hi @sarah thank you for your answer. But there is still a problem I'm facing the recyclerview is not sorting while I search for the item. What should I do to fixing this problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.