java.lang.ClassCastException: java.util.ArrayList无法转换为com.parse.ParseObject

huangapple go评论70阅读模式
英文:

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

问题

// 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().equals(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) {
    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();
        }
    };
}

(Note: The provided code is the translated version of the Java code snippet you provided. I've made sure to include only the code portion and omit any additional explanations or comments.)

英文:

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<ParseObject>) filterResults.values;
Please help me to edit my code regarding this issues.

RoomCardRecyclerViewAdapter

 private List&lt;ParseObject&gt; mRooms = new ArrayList&lt;&gt;();
private ParseObject room;
private String mSection;
private Context context;
public RoomCardRecyclerViewAdapter(){
super(DIFF_CALLBACK);
}
public static final DiffUtil.ItemCallback&lt;ParseObject&gt;  DIFF_CALLBACK = new 
DiffUtil.ItemCallback&lt;ParseObject&gt;() {
@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()) &amp;&amp; 
oldItem.getCreatedAt().equals(newItem.getCreatedAt()));
}
};
public RoomCardRecyclerViewAdapter(String section) {
this();
this.mSection = section;
}
public RoomCardRecyclerViewAdapter(Context context, List&lt;ParseObject&gt;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(&quot;roomSuburb&quot;));
holder.mRoomPrice.setText(Integer.toString(room.getInt(&quot;roomMonthlyRent&quot;)));
holder.mInclusiveOrNot.setText(room.getString(&quot;roomRentInclusiveOfBills&quot;));
holder.mPropertyType.setText(room.getString(&quot;roomPropertyType&quot;));
holder.mNumOfBeds.setText(Integer.toString(room.getInt(&quot;roomBedrooms&quot;)));
holder.mNumOfBaths.setText(Integer.toString(room.getInt(&quot;roomBathrooms&quot;)));
}
@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&lt;ParseObject&gt; filteredList = new ArrayList&lt;&gt;();
for (ParseObject parseObject : mRooms){
if (parseObject.getString(&quot;roomSuburb&quot;).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&lt;ParseObject&gt;) filterResults.values;
notifyDataSetChanged();
}
};
}

答案1

得分: 1

你只需要按照以下方式调用notifyDataSetChanged() -

使用这个

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

You just need to call notifyDataSetChanged() as below -

Use this

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

答案2

得分: 0

请注意,由于格式限制,某些特殊字符可能无法在代码部分完美显示,我将尽力保留原始代码的结构。

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

public RoomCardRecyclerViewAdapter(Context context, List<ParseObject> arrayList) {
    this();

    this.context = context;
    mRooms = arrayList;
    mFilteredRooms = arrayList;
}

@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();
        }
    };
}
英文:

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

private List&lt;ParseObject&gt; mFilteredRooms = new ArrayList&lt;&gt;();
public RoomCardRecyclerViewAdapter(Context context, List&lt;ParseObject&gt; 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&lt;ParseObject&gt; filteredList = new ArrayList&lt;&gt;();
if (charSequence == null || charSequence.toString().trim().equals(&quot;&quot;)){
filteredList.addAll(mFilteredRooms);
}else {
String charString = charSequence.toString();
for (ParseObject parseObject : mFilteredRooms){
if (parseObject.getString(&quot;roomSuburb&quot;).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&lt;ParseObject&gt;) filterResults.values;
notifyDataSetChanged();
}
};

huangapple
  • 本文由 发表于 2020年10月8日 12:27:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64255787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定