英文:
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<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();
}
};
}
答案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 && results.count > 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<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();
}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论