首页 > 解决方案 > 将 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" ]
   }
}

在此处输入图像描述

标签: androiddatabasefirebaseandroid-recyclerview

解决方案


在这种情况下,您可以使用 HashMap:

 HashMap<String,Objects> map = new HashMap<>();

您可以thumbnail使用以下代码从 hashmap 中检索:

map.get("thumbnail");
                    

推荐阅读