这是我的java类
private String locations;
private String images;
public ItemData(){
}
public ItemData(String locations, String images){
this.locations = locations;
this.images = images;
}
public String getLocations() {
return locations;
}
public void setLocations(String locations) {
this.locations = locations;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
}
这是我从Firebase获取数据快照的主要活动。
itemData = new ItemData();
rv = findViewById(R.id.recycler);
rv.setLayoutManager(new LinearLayoutManager(this));
data = new ArrayList<>();
adapter = new RecyclerAdapter(data);
rv.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
imageRef = database.getInstance().getReference("Users").child(userid).child("images");
imageRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot mData : dataSnapshot.getChildren()) {
String mImages = mData.getValue(String.class);
itemData.setImages(mImages);
data.add(itemData);
}
adapter.notifyDataSetChanged();
}
我要做的是显示保存在“图像”键中的图像:例如urlImages1,urlImages2,urlImages3。但是,运行该应用程序仅显示与recyclerview
大小相同的图像。非常感谢您的帮助
编辑:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ItemDataViewHolder>{
List<ItemData> data;
public RecyclerAdapter(List<ItemData> data) {
this.data = data;
}
@NonNull
@Override
public ItemDataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recycler, parent, false);
ItemDataViewHolder holder = new ItemDataViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ItemDataViewHolder holder, int position) {
ItemData itemData = data.get(position);
holder.location.setText(itemData.getLocations());
Picasso.get().load(itemData.getImages())
.rotate(270).into(holder.imageView);
}
@Override
public int getItemCount() {
return data.size();
}
public static class ItemDataViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView title, location;
public ItemDataViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.rImageView);
title = itemView.findViewById(R.id.rTitleTv);
location = itemView.findViewById(R.id.rDescriptionTv);
}
}
}
更新: 我想在适配器中显示“图像”和“位置”的数据。如何在同一事件监听器中获得两个键的引用?
“用户”:{
"X7aVfZH5oZbIaeyLm9FAjOSr5Gd2" : {
"images" : {
"urlImages1" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg1.jpg?alt=media&token=e26fd6a2-2be7-4a01-920e-0aed1fe06436",
"urlImages2" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg2.jpg?alt=media&token=40165918-3b46-4b22-b0ef-255965b7855d",
"urlImages3" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg3.jpg?alt=media&token=cfd7d00d-9b1a-44fd-80d4-a2947f7de743",
"urlImages4" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg4.jpg?alt=media&token=995419a7-70f7-4330-b046-d06a4d54453e",
"urlImages6" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg6.jpg?alt=media&token=ee6f566a-a9e5-4e8f-9d53-64d3f3bc97a6",
"urlImages7" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg7.jpg?alt=media&token=883a3321-fbba-49bd-aae3-14b005b4cc5b"
},
"locations" : {
"locations1" : "14.2830817 -89.7263953",
"locations2" : "14.2661424 -89.7220017",
"locations3" : "14.2846352 -89.7251991",
"locations4" : "14.2684987 -89.7266704",
"locations5" : "14.2684987 -89.7266704",
"locations6" : "14.2684987 -89.7266704",
"locations7" : "14.2684987 -89.7266704"
答案 0 :(得分:0)
您好,请进行以下更改,使其正常运行。
imageRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot mData : dataSnapshot.getChildren()) {
String mImages = mData.getValue(String.class);
ItemData itemDataOne = new ItemData();
itemDataOne .setImages(mImages);
data.add(itemDataOne );
}
adapter.notifyDataSetChanged();
}
说明:问题是您仅创建一次ItemData并将其设置为循环。在Java中,对象(ItemData)是passByReference类型,因此,当您将其添加到列表中时,会添加一个引用,并且由于仅创建了一个对象,因此添加了相同的引用,并且将值设置为最后一个数据。