как получить URL-адрес изображения из базы данных firebase - PullRequest
0 голосов
/ 05 августа 2020

Я получаю данные из своей базы данных, например, любое поле данных.

Я могу получить такие поля, как местоположение, страна и адрес. URL-адрес изображения из базы данных, чтобы вставить его в мою функцию picasso.

вот мой 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 голосов
/ 05 августа 2020

проверьте это

поместите этот метод в свой 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());
...