繁体   English   中英

应用程序未使用 Java 从 android studio 中的 firebase 实时数据库中检索图像

[英]App not retrieving images from firebase realtime database in android studio with Java

请我想使用 CardView 将带有文本的图像从 Firebase 实时数据库检索到 RecyclerView,其中包含图像的 ImageView 和价格和标题的两个 TextView,我已经能够检索文本但图像不显示我m 使用 glide,只显示错误持有者图像,我也实现了依赖项和注释处理器,但仍然没有显示图像。 请问我还有什么需要做的吗?

请这是我在 RecyclerAdapter 中的代码

 RecyclerArrayAdapter extends RecyclerView.Adapter<RecyclerArrayAdapter.ViewHolder> {
    Context context;
    ArrayList<Data> ImagesArrayList;

    public RecyclerArrayAdapter(Context context) {
        this.context = context;
    }

    public RecyclerArrayAdapter(Context context, ArrayList<Data> imagesArrayList) {
        this.context = context;
        ImagesArrayList = imagesArrayList;
    }

    @NonNull
    @Override
    public RecyclerArrayAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image,parent, false);
        return new RecyclerArrayAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerArrayAdapter.ViewHolder holder, int position) {


Glide.with(context)
  .asBitmap()
  .override(120,120)
  . placeholder 
   (R.drawable.common_image)
  .dontAnimate()
.error(R.drawable.common_second_image)            
 .load(ImagesArrayList.get(position).getImage())
             .into(holder.mImage);

        holder.mTitle.setText(ImagesArrayList.get(position).getTitle());
        holder.mPrice.setText(String.valueOf(ImagesArrayList.get(position).getPrice()));

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        CircleImageView mImage;
        TextView mTitle;
        TextView mPrice;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mImage = (CircleImageView) itemView.findViewById(R.id.image_locate);
            mTitle = (TextView) itemView.findViewById(R.id.title);
            mPrice =(TextView) itemView.findViewById(R.id.price_tag);
        }
   

请这里是 MainActivity.java 中的代码

Public class Ladies_Dresses_Page extends AppCompatActivity{

private RecyclerArrayAdapter private recyclerArrayAdapter;
private RecyclerView recyclerView;
private ArrayList<Data> modelArrayList;
DatabaseRefrence  = database;
recyclerView = findViewById(R.id.dressRecycler);

 database=FirebaseDatabase.getInstance().getReference("Data");
    database.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            clearAll();

            for (DataSnapshot snapshot1 : snapshot.getChildren()){
                Data data = new Data();
                data.setTitle(snapshot1.child("title").getValue().toString());

                data.setImage(snapshot1.child("image").getValue().toString());
                data.setPrice(Integer.parseInt(snapshot1.child("price").getValue().toString()));
                          modelArrayList.add(data);
            }

            recyclerArrayAdapter = new RecyclerArrayAdapter(getApplicationContext(),modelArrayList);
            recyclerView.setAdapter(recyclerArrayAdapter);



        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
           
            Toast.makeText(Ladies_Dresses_Page.this, "ERROR"+ error.getMessage(),Toast.LENGTH_SHORT).show()
}

private void clearAll() {
    if (modelArrayList != null) {
        modelArrayList.clear();

        if (recyclerArrayAdapter != null) {
            recyclerArrayAdapter.notifyDataSetChanged();
        }

    }
    modelArrayList = new ArrayList<>();
}

下面是我的模型类中的代码

 public class Data {
String title, image;
int price;
public Data(){};

    public Data(String title, String image, int price) {
        this.title = title;
        this.image = image;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public int getPrice() {
        return price;
    public void setPrice(int price) {
        this.price = price;
    }

请在这里我的数据库


  "Data": [
    null,
    {
      "image": "gs://apex-shop.appspot.com/IMG-20210119-WA0029.jpg",
      "price": 100,
      "title": "top and down"
    },
    {
      "image": "gs://apex-shop.appspot.com/IMG-20210122-WA0015.jpg",
      "price": 40,
      "title": "top and down"
    },
    {
      "image": "gs://apex-shop.appspot.com/Untitled-1.jpg",
      "price": 200,
      "title": "top and down"
    },
    {
      "image": "gs://apex-shop.appspot.com/first.jpg",
      "price": 70,
      "title": "top and down"
    },
    {
      "image": "gs://apex-shop.appspot.com/image1.jpg",
      "price": 60,
      "title": "top and down"
    },
    {
      "image": "https://www.decathlon.com.gh/academic-soccer-balls/331004-33947-machine-stitched-football-f100-size-5-white.html#/demodelcolor-8619247/demodelsize-1205?queryID=ed99ba460205f743a374af034b56f7d9&objectID=4242708",
      "price": 100,
      "title": "Ball"
    },
    {
      "image": "gs://apex-shop.appspot.com/Untitled-1.png",
      "price": 10,
      "title": "small image"
    },
    {
      "image": "https://www.bing.com/ck/a?!&&p=ac9681f5a082e2f7377e25eea85f356e857939c6da63e291ef95767fde17da0dJmltdHM9MTY1MjAyNzY3MiZpZ3VpZD0yZjM1MjcxZi00MTk5LTRjNDItYTgyNC1mYTg0ZDIzMDA3ZmImaW5zaWQ9NTQyNw&ptn=3&fclid=bcbd5304-ceec-11ec-9502-3b2dcfcefe67&u=a1L2ltYWdlcy9zZWFyY2g_cT1DYXQmRk9STT1JUUZSQkEmaWQ9MDk1QUNDNjY1MTk5RUFDOUEwODM0Qzg1ODI3RUI3MzM3RjA2QzhFMyZtc2Nsa2lkPWJjYmQ1MzA0Y2VlYzExZWM5NTAyM2IyZGNmY2VmZTY3&ntb=1",
      "price": 50,
      "title": "cat"
    }
  ]
}

问题不在于代码,而在于数据库。 如下所示的 URL:

gs://apex-shop.appspot.com/IMG-20210119-WA0029.jpg

它不是 Glide 可以使用的有效 URL。 要解决此问题,您必须更改Data节点内的所有子节点以保存有效的 URL,这些 URL 以https://...开头并直接指向所需的图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM