简体   繁体   English

java.lang.ClassCastException: java.util.ArrayList 不能转换为 com.parse.ParseObject

[英]java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.parse.ParseObject

I m currently working on a android apps which fetch data from parse server.我目前正在开发一个从解析服务器获取数据的 android 应用程序。 I wanted to filter my recyclerView by using searchview.我想使用 searchview 过滤我的 recyclerView。 But it shows me nothing while search.但是在搜索时它什么也没显示。 It shows me error in this line mRooms = (ArrayList) filterResults.values;它在这一行中显示了错误mRooms = (ArrayList) filterResults.values; Please help me to edit my code regarding this issues.请帮助我编辑有关此问题的代码。

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

}

You just need to call notifyDataSetChanged() as below -您只需要按如下方式调用 notifyDataSetChanged() -

Use this用这个

 @Override
 protected void publishResults(CharSequence charSequence, FilterResults 
 filterResults) { 
     if (filterResults!= null && results.count > 0) {
         notifyDataSetChanged();
     } else {
         notifyDataSetInvalidated();
     }
 }

Define an additional variable mFilteredRooms in the constructor and set it to arrayList在构造函数中定义一个额外的变量 mFilteredRooms 并将其设置为 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();
      }
  };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:无法将java.util.ArrayList强制转换为com.inrev.crm.bean.IRSurveyQuestions - java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.inrev.crm.bean.IRSurveyQuestions java.lang.ClassCastException:com.mongodb.client.internal.AggregateIterableImpl 无法转换为 java.util.ArrayList - java.lang.ClassCastException: com.mongodb.client.internal.AggregateIterableImpl cannot be cast to java.util.ArrayList java.lang.ClassCastException: java.util.Arrays$ArrayList 不能转换为 java.util.ArrayList - java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList 如何修复:java.lang.ClassCastException:java.util.ArrayList无法强制转换为java.lang.Integer - How to fix: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Integer java.lang.ClassCastException: java.util.ArrayList 不能转换为 java.lang.Object[] - java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Object[] java.lang.ClassCastException:java.util.ArrayList无法强制转换为java.lang.String - java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String java.lang.ClassCastException:无法将java.util.ArrayList强制转换为…CrExcessMaster - java.lang.ClassCastException: java.util.ArrayList cannot be cast to …CrExcessMaster 处理 java.lang.ClassCastException: java.util.ArrayList 不能强制转换为 checksAccount.BankEntry - Dealing java.lang.ClassCastException: java.util.ArrayList cannot be cast to checkingAccount.BankEntry 如何修复:java.lang.ClassCastException:java.util.ArrayList无法强制转换为double [] - How to fix: java.lang.ClassCastException: java.util.ArrayList cannot be cast to double[] java.lang.ClassCastException:无法将java.util.ArrayList强制转换为antlr.collections.List - java.lang.ClassCastException: java.util.ArrayList cannot be cast to antlr.collections.List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM