如何从Firebase数据库检索图像URL

时间:2020-08-05 15:14:40

标签: android firebase google-cloud-firestore firebaseui

我正在从数据库中检索数据,例如任何数据字段。

我能够获取位置,国家和地址等字段。

我正在苦苦挣扎的是从数据库中获取图像URL,以便将其插入我的毕加索函数中。

这是我的Java代码:

private FirebaseFirestore firebaseFirestore;
    private RecyclerView FirestoreList;
    private FirestoreRecyclerAdapter adapter;

 FirestoreList = findViewById(R.id.recycler_view);
        firebaseFirestore = FirebaseFirestore.getInstance();

 Query q = firebaseFirestore.collection("Shops");

        //recycle options
        FirestoreRecyclerOptions<Shop> options = new FirestoreRecyclerOptions.Builder<Shop>()
                .setQuery(q, Shop.class)
                .build();

        adapter = new FirestoreRecyclerAdapter<Shop, ShopViewHolder>(options) {
            @NonNull
            @Override
            public ShopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_item,parent,false);
                return new ShopViewHolder(view);
            }

            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            protected void onBindViewHolder(@NonNull ShopViewHolder holder, int position, @NonNull Shop model) {
                holder.list_name.setText(model.getName());
                holder.list_location.setText(model.getLocation());


                 //here is where i should be able to get my image url from the database.
i tried doing it this way but there is always and error or incompatibility. 
      holder.header_img.setText(model.getShopHeaderImg());
      holder.header_img.setImageURI(model.getShopHeaderImg());
//all of these give me an error, so what is the correct methodology to retrieve the image url??

        }
        };

        FirestoreList.setHasFixedSize(true);
        FirestoreList.setLayoutManager(new LinearLayoutManager(this));
        FirestoreList.setAdapter(adapter);

这是我应该得到自己的价值观的地方:

private class ShopViewHolder extends RecyclerView.ViewHolder {
        private TextView list_name;
        private TextView list_location;
        private ImageView header_img;




        public ShopViewHolder(@NonNull View itemView) {
            super(itemView);

            Shop MyShop = new Shop();
            String image = MyShop.getShopHeaderImg();

            list_name = itemView.findViewById(R.id.list_name);
            list_location = itemView.findViewById(R.id.list_location);
            header_img = itemView.findViewById(R.id.header_img);


            Picasso.get().load(image).into(header_img);

        }
    }

这是我的商店课程:

public class Shop {

    private String name;
    private String country;
    private String address;
    private String location;
    private String ShopHeaderImg;



    //private int priority;

    public Shop(){

    }

    public Shop(String name, String country, String address, String location, String ShopHeaderImg) {
        this.name = name;
        this.country = country;
        this.address = address;
        this.location = location;
        this.ShopHeaderImg = ShopHeaderImg;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }


    public String getShopHeaderImg() {
        return ShopHeaderImg;
    }

    public void setShopHeaderImg(String ShopHeaderImg) {
        this.ShopHeaderImg = ShopHeaderImg;
    }
}

我如何能够将图像URL检索到getShopHeaderImg()方法中

1 个答案:

答案 0 :(得分:0)

检查此

将此方法放入您的viewHolder

      public void setHeaderImage(final Context c , final String Image){

         final ImageView headerImg = (ImageView)itemView.findViewById(R.id.headerImage);
          Picasso.with(c).load(Image).into(headerImg);

}

并使用onBindViewHolder内部的方法

 holder.setHeaderImage(getApplicationContext(),model.getShopHeaderImg());