android - 将 firebase 数据放入 recyclerview 时出错,“反序列化时需要一个列表,但有一个类 java.lang.String”
问题描述
您好,我正在尝试使用 Model 类获取数据,并使用 Model 类在 recyclerview 中显示数据。
我得到了 ArrayList 的缩略图数据,我认为这导致了错误,但我不知道该怎么办。
我有适配器,我测试了缩略图数据是否只是字符串(不是列表),它运行良好。
出了什么问题,如何检索缩略图数据列表?初学者太难了......
这是我的片段 java 代码的一部分
Query newPlaceDatabaseQuery = FirebaseDatabase.getInstance().getReference(dataA).child(placeA).limitToLast(5);
newPlaceDatabaseQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnap) {
homeNewArrayList.clear();
for (DataSnapshot snapshot : dataSnap.getChildren()) {
//Error caused in here.
ItemHomeNew itemHomeNew = snapshot.getValue(ItemHomeNew.class);
homeNewArrayList.add(itemHomeNew);
}
newRecyclerView.setAdapter(newAdapter);
newAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
这是我的模型类 ItemHomeNew 代码
public class ItemHomeNew {
private ArrayList<String> thumbnail;
private String placeAddress;
private String placeName;
private String placeStyle;
public ItemHomeNew(){
}
public ArrayList<String> getThumbnail() {
return thumbnail;
}
public void setThumbnail(ArrayList<String> thumbnail) {
this.thumbnail = thumbnail;
}
public String getPlaceAddress() {
return placeAddress;
}
public void setPlaceAddress(String placeAddress) {
this.placeAddress = placeAddress;
}
public String getPlaceName() {
return placeName;
}
public void setPlaceName(String placeName) {
this.placeName = placeName;
}
public String getPlaceStyle() {
return placeStyle;
}
public void setPlaceStyle(String placeStyle) {
this.placeStyle = placeStyle;
}
@Override
public String toString() {
return "thumbnail" + thumbnail + ',' ;
}
}
适配器代码
public class AdapterHomeNew extends RecyclerView.Adapter<AdapterHomeNew.ViewHolder>{
ArrayList<ItemHomeNew> array;
Context context;
@ColorInt int starColor;
@ColorInt int startextColor;
public AdapterHomeNew(ArrayList<ItemHomeNew> array, Context context) {
this.array = array;
this.context =context;
}
@NonNull
@Override
public AdapterHomeNew.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_new,parent,false);
return new AdapterHomeNew.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AdapterHomeNew.ViewHolder holder, int position) {
Glide.with(context)
.load(array.get(position).getThumbnail())
.centerCrop()
.dontAnimate()
.into(holder.homeNewPlaceImg);
holder.homeNewAddress.setText(String.valueOf(array.get(position).getPlaceAddress()));
holder.homeNewplaceName.setText(String.valueOf(array.get(position).getPlaceName()));
holder.homeNewPlaceStyle.setText(String.valueOf(array.get(position).getPlaceStyle()));
}
@Override
public int getItemCount() {
return array.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView homeNewPlaceImg;
TextView homeNewAddress;
TextView homeNewplaceName;
TextView homeNewPlaceStyle;
public ViewHolder(@NonNull View itemView) {
super(itemView);
homeNewPlaceImg =itemView.findViewById(R.id.homeNewPlaceImg);
homeNewAddress =itemView.findViewById(R.id.homeNewAddress);
homeNewplaceName =itemView.findViewById(R.id.homeNewplaceName);
homeNewPlaceStyle =itemView.findViewById(R.id.homeNewPlaceStyle);
}
}
}
供参考我的数据结构
"dataA" : {
"placeA" : {
"20210411130750241" : {
"call" : "02020",
"extraD" : "TEST ExtraD",
"mapx" : "2380",
"mapy" : "505",
"memberTag" : [ "ga, na, gren" ],
"placeAddress" : "Ave Unit D 07072",
"placeCategory" : "restaurant",
"placeFootprint" : 0,
"placeName" : "hihihi",
"placeStyle" : "Visit",
"placeUploader" : "OYyLCJxiNiZAaEK0T6jVnZW69kg1",
"runningTime" : "Mon: 09:00-20:00\nTue: 09:09-19:00\n",
"shortD" : "TestShortD",
"thumbnail" : [ "https://search.pstatic.net/common/?autoRotate=true&quality=95&type=w750&src=https%3A%2F%2Fldb-phinf.pstatic.net%2F20200501_166%2F1588260117507zY7Er_JPEG%2FyLWif5MfrxciIvUrODQsKxkb.jpeg.jpg", "https://search.pstatic.net/common/?autoRotate=true&quality=95&type=w750&src=https%3A%2F%2Fldb-phinf.pstatic.net%2F20200501_159%2F1588260202284uxxDL_JPEG%2FjMEr_bE2PYkwwTccqGYPVptF.jpeg.jpg" ]
}
}
解决方案
在这种情况下,您可以使用 HashMap:
HashMap<String,Objects> map = new HashMap<>();
您可以thumbnail
使用以下代码从 hashmap 中检索:
map.get("thumbnail");
推荐阅读
- c++ - 指向常量和常量指针的指针
- lua - 使用 lua 过滤器更新段落
- javascript - 显示来自 JSON API 响应的帖子列表
- java - java.io.FileNotFoundException 即使文件路径正确
- sql - 显示 db2 表中当前数据 1 年前的所有数据
- javascript - 导航栏不在类之间切换
- android - 如何隐藏 Button 和 TextView 的边框?
- excel - 如何在excel的一列行中匹配一列字符串?
- c++ - 如何在 OpenSSL 库的名称中添加后缀“d”?
- javascript - 所有事件都有一个监听器数组还是所有“发射器”都有一个监听器数组?