【发布时间】:2021-07-08 17:51:31
【问题描述】:
您好,我正在尝试使用 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" ]
}
}
【问题讨论】:
-
这段代码中到底有什么不符合您的预期?告诉我们共享代码有什么问题。你有什么错误吗?
-
ItemHomeNew 中的错误原因 itemHomeNew = snapshot.getValue(ItemHomeNew.class); homeNewArrayList.add(itemHomeNew);我得到了这个错误。 “反序列化时需要一个列表,但有一个类 java.lang.String” 我想获取 placeName、placeAddress、placeStyle 和缩略图数据,但我未能获取缩略图数据。我认为原因是缩略图数据与其他数据的类型不同。但是不知道怎么解决。
-
你的列表中有缩略图数据吗?(我看到你的模型似乎是这样)问题是firebase期待一个列表,但它正在获取一个字符串
标签: android database firebase android-recyclerview