gpt4 book ai didi

java - 如何在recyclerview中显示图片?

转载 作者:行者123 更新时间:2023-12-02 02:22:14 27 4
gpt4 key购买 nike

These are the list of pictures i want from database我有一个 firebase 数据库,我从中获取图像列表并将其显示在 recyclerview 中。我能够获取所有图像的 url,但无法在 ImageView 中显示图像。

 private void getImagesFromDatabase() {
Log.d("hh", "Reach Here");
// String USERID = FirebaseAuth.getInstance().getCurrentUser().getPhoneNumber();

final DatabaseReference historyDatabase = FirebaseDatabase.getInstance().getReference().child("BeautyCenters").child(nameofspa).child("Images");
historyDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot pictures : dataSnapshot.getChildren()) {
Log.d("SPAA", "pictures ARE" + pictures);
Log.d("SPAA", "data ARE " + dataSnapshot);
ImagesObject imagesObject = pictures.getValue(ImagesObject.class);
uploads.add(imagesObject);
}
imagesAdapter = new ImagesAdapter(DetailsPage.this, uploads);
logorecylerview.setAdapter(imagesAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}

我已经尝试过这段代码,但这给了我错误 com.google.firebase.database.DatabaseException: Can't conversion object of type java.lang.String to type .DetailsPage.ImagesObject。

这是我的 ADPATER 类

public class ImagesAdapter extends  RecyclerView.Adapter<ImagesAdapter.ViewHolder> {
private List<ImagesObject> mUploads;
private Context mcontext;
private LayoutInflater mInflater;
private ImagesAdapter.ItemClickListener mClickListener;
public ImagesAdapter(Context context, List<ImagesObject> uploads) {
this. mcontext = context;
this. mUploads = uploads;
this.notifyDataSetChanged();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

this.mInflater = LayoutInflater.from(mcontext);

View view = mInflater.inflate(R.layout.picturesofspa, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
ImagesObject imagesObject = mUploads.get(position);
Picasso.get()
.load(imagesObject.getImages())
.placeholder(R.drawable.testsaloon)
.error(R.drawable.ic_launcher_background)
.into(holder.PicturesofSPA);

}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public ImageView PicturesofSPA;

public ViewHolder(View itemView) {
super(itemView);
PicturesofSPA = (ImageView) itemView.findViewById(R.id.PicturesofSPA);

itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
}
}

public interface ItemClickListener {
void onItemClick(View view, int position);
}

@Override
public int getItemCount(){
return this.mUploads.size();
}
}

这是我的对象类:

public class ImagesObject {
private String Images;

public ImagesObject(String Images) {
this.Images = Images;
}

public ImagesObject() {
}
//Images
public String getImages(){
return Images;
}
public void setImages(String Images)
{
this.Images = Images;
}
}

这是我获取 URL 的位置:

private List<ImagesObject> uploads;
uploads = new ArrayList<>();

private void getImagesFromDatabase() {
Log.d("hh", "Reach Here");
// String USERID = FirebaseAuth.getInstance().getCurrentUser().getPhoneNumber();

final DatabaseReference historyDatabase = FirebaseDatabase.getInstance().getReference().child("BeautyCenters").child(nameofspa).child("Images");
historyDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot pictures : dataSnapshot.getChildren()) {
Log.d("SPAA", "pictures ARE" + pictures);
Log.d("SPAA", "data ARE " + dataSnapshot);

ImagesObject imagesObject = pictures.getValue(ImagesObject.class);
uploads.add(imagesObject);
}
imagesAdapter = new ImagesAdapter(DetailsPage.this, uploads);
logorecylerview.setAdapter(imagesAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}

使用此代码,它可以计算图片数量,但不显示任何图片”

for (DataSnapshot pictures : dataSnapshot.getChildren()) {
Log.d("SPAA", "pictures ARE" + pictures);
Log.d("SPAA", "data ARE " + dataSnapshot);

ImagesObject imagesObject = dataSnapshot.getValue(ImagesObject.class);
uploads.add(imagesObject);
}

最佳答案

 com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type .DetailsPage.ImagesObject

您收到上述错误是因为您的数据库中图像 url 存储为字符串。

你的数据库就像

Images:
l0: url1
l1: url2
l2: url3

它应该是类 ImagesObject 类型,如下所示

Images:
l0:
Images: url1
l1:
Images: url2
l2:
Images: url3

为了避免该错误,请尝试以下两种解决方案:

  1. 使用 ImagesObject 类将图像网址推送到实时数据库中,或者像上面那样创建数据库。

  • 在您的代码中执行此操作。
  • String url = pictures.getValue(String.class);
    ImagesObject imgObject = new ImagesObject(url);
    uploads.add(imgObject);

    此外,避免在 valueEventListener 内设置适配器。将适配器设置为外部,并在列表上传更改时使用 notifyDataSetChanged。也不要将 ImagesObject 中的字符串图像设为私有(private)。

    关于java - 如何在recyclerview中显示图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57271725/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com