how to retrieve image url from firebase database - android

i am retrieving data from my database such as any data field.
i am able to get fields such as location, country and address.
what i am struggling with is getting the image url from the database in order to insert it into my picasso function.
here is my java code:
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);
here is where i should be getting my values:
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);
}
}
this is my shop class:
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;
}
}
how will i be able to retrieve the image url into the getShopHeaderImg() method

check this
put this method inside your 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);
}
and use the method inside onBindViewHolder
holder.setHeaderImage(getApplicationContext(),model.getShopHeaderImg());

Related

My Recyclerview layout is not showing after not working for quite some time. It shows relayout returned

The last time I was working on my app, it was working fine as expected. After a month of not working on it, it doesn't pop out the layout as shown when I was last working on it. When I click the button, it will just lead to a blank page
Adapter
public class RestaurantAdapter extends FirebaseRecyclerAdapter<RestaurantDetails,RestaurantAdapter.RestaurantViewHolder> {
public RestaurantAdapter(#NonNull FirebaseRecyclerOptions<RestaurantDetails> options) {
super(options);
}
FirebaseRecyclerOptions<RestaurantDetails> options =
new FirebaseRecyclerOptions.Builder<RestaurantDetails>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Restaurant").child("Info"), RestaurantDetails.class)
.build();
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder holder, final int position, #NonNull final RestaurantDetails details) {
holder.restaurant_Name.setText(details.getName());
holder.restaurant_Category.setText(details.getCategory());
holder.restaurant_Location.setText(details.getLocation());
Picasso.get().load(details.getImage()).into(holder.restaurant_Image);
holder.itemView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
String post_key = getRef(position).getKey();
Intent intent = new Intent(view.getContext(), RestaurantInfoActivity.class);
intent.putExtra("post_key", post_key);
view.getContext().startActivity(intent);
}
});
}
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view =LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_list,parent, false);
return new RestaurantViewHolder(view);
}
class RestaurantViewHolder extends RecyclerView.ViewHolder{
TextView restaurant_Name, restaurant_Category,restaurant_Location;
ImageView restaurant_Image;
public RestaurantViewHolder(#NonNull View itemView){
super(itemView);
restaurant_Name = itemView.findViewById(R.id.restaurant_Name);
restaurant_Category = itemView.findViewById(R.id.restaurant_Category);
restaurant_Location = itemView.findViewById(R.id.restaurant_Location);
restaurant_Image = itemView.findViewById(R.id.restaurant_Image);
}
}
}
ResRecyclerViewActivity
public class ResRecyclerViewActivity extends AppCompatActivity {
private static final String TAG = "ResRecyclerViewActivity";
private RecyclerView restaurantRecyclerView;
private RestaurantAdapter adapter;
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurantlist);
Log.d(TAG, "onCreate: started.");
restaurantRecyclerView = findViewById(R.id.restaurant_rv);
restaurantRecyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<RestaurantDetails> options =
new FirebaseRecyclerOptions.Builder<RestaurantDetails>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Restaurant").child("Info"), RestaurantDetails.class)
.build();
adapter = new RestaurantAdapter(options);
restaurantRecyclerView.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
RestaurantDetails (Model)
public class RestaurantDetails {
private String name;
private String location;
private String address;
private String category;
private String image;
private Double latitude;
private Double longitude;
public RestaurantDetails(String name, String location, String address, String category, String image, Double latitude, Double longitude) {
this.name = name;
this.location = location;
this.address = address;
this.category = category;
this.image = image;
this.latitude = latitude;
this.longitude = longitude;
}
public RestaurantDetails() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) { this.image = image; }
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Double getLatitude() {return latitude;}
public void setLatitude(Double lang) {this.latitude = latitude;}
public Double getLongitude() {return longitude;}
public void setLongitude(Double longitude) {this.longitude = longitude;}
}
When I debug the app,this are the comments that pop up. No errors were shown
It might be due to firebase rules. Check your firebase database rules because after a month firebase rules changed to default for security reasons.

Want to print the parent node into a recyclerview from firebase

FirebaseRecyclerOptions<RestaurantDetails> options =
new FirebaseRecyclerOptions.Builder<RestaurantDetails>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Restaurant").child("Category"), RestaurantDetails.class)
.build();
#Override
protected void onBindViewHolder(#NonNull CategoryAdapter.CategoryViewHolder holder, int position, #NonNull RestaurantDetails model) {
String post_key = getRef(position).getKey();
holder.restaurant_CategoryList.setText(post_key);
}
#NonNull
#Override
public CategoryAdapter.CategoryViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_filter,parent, false);
return new CategoryViewHolder(view);
}
class CategoryViewHolder extends RecyclerView.ViewHolder{
TextView restaurant_CategoryList;
public CategoryViewHolder(#NonNull View itemView) {
super(itemView);
restaurant_CategoryList = itemView.findViewById(R.id.restaurant_Filter);
}
}
}
I want to print the post_key on my recyclerview but it only shows an empty page. Only white screen. There's no error but I believe I am missing something. I want to only display the Categories. If I use the other node which is "Info" it will print a duplicate of category. For example, Cafe, cafe which I only required word being preview
I have updated the post. Here's my RestaurantDetails.
public class RestaurantDetails {
private String name;
private String location;
private String address;
private String category;
private String image;
private Double latitude;
private Double longitude;
private Boolean filter;
public RestaurantDetails(String name, String location, String address, String category, String image, Double latitude, Double longitude, Boolean filter) {
this.name = name;
this.location = location;
this.address = address;
this.category = category;
this.image = image;
this.latitude = latitude;
this.longitude = longitude;
this.filter = filter;
}
}
Inside your onBindViewHolder() you need to run a loop to get all the keys and print them.
DatabaseReference databaseReference1 = FirebaseDatabase.getInstance().getReference("Restaurant");
Query query1 = databaseReference1.child("Category");
query1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
String post_key = dataSnapshot1.getKey();
System.out.println(post_key);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

How To Display Images with FirebaseUI Firestore RecyclerView

I would like to display an image in my recycler view using FirebaseUI and Firestore. So far i can only display text. I have not been able to find a understandable solution online.
As you can see in the code below, i have been able to bind the text to eventName and eventCity text view elements.
Is there a way, perhaps using picasso, to retrieve the image URI from firebase using getDisplay_Image_URI() and bind it to the eventDisplay imageview? I have added the POJO class i am using for easier comprehnsion.
ExplorePageAdapter.java
public class ExplorePageAdapter extends FirestoreRecyclerAdapter<POJO_explore_page_item, ExplorePageAdapter.EventHolder> {
/**
* Create a new RecyclerView adapter that listens to a Firestore Query. See {#link
* FirestoreRecyclerOptions} for configuration options.
*
* #param options
*/
public ExplorePageAdapter(#NonNull FirestoreRecyclerOptions<POJO_explore_page_item> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull EventHolder eventHolder, int i, #NonNull POJO_explore_page_item pojo_explore_page_item) {
eventHolder.eventName.setText(pojo_explore_page_item.getName());
eventHolder.eventCity.setText(pojo_explore_page_item.getCity());
//eventHolder.eventName.setText(pojo_explore_page_item.getName());
}
#NonNull
#Override
public EventHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.
layout.explore_page_item, parent,false
);
return new EventHolder(v);
}
class EventHolder extends RecyclerView.ViewHolder{
TextView eventName;
TextView eventCity;
ImageView eventDisplay;
//Button bookmark;
public EventHolder(#NonNull View itemView) {
super(itemView);
eventName = itemView.findViewById(R.
id.exp_event_name);
eventCity = itemView.findViewById(R.
id.exp_event_city);
eventDisplay = itemView.findViewById(R.
id.exp_display_image);
//bookmark = itemView.findViewById(R.
// id.bookmark);
}
}
}
POJO_explore_page_item.java
private String Display_Image_URI;
private String Event_ID;
private String capacity;
private String city;
private String country;
private String deadline;
private String end;
private String start;
private String price;
private String venue;
private String id;
private String name;
public POJO_explore_page_item() {
//empty constructor needed
}
public POJO_explore_page_item(String Display_Image_URI, String Event_ID, String capacity, String city, String country, String deadline, String end, String start, String price, String venue, String id, String name) {
this.Display_Image_URI = Display_Image_URI;
this.Event_ID = Event_ID;
this.capacity = capacity;
this.city = city;
this.country = country;
this.deadline = deadline;
this.end = end;
this.start = start;
this.price = price;
this.venue = venue;
this.id = id;
this.name = name;
}
public String getDisplay_Image_URI() {
return Display_Image_URI;
}
public String getEvent_ID() {
return Event_ID;
}
public String getCapacity() {
return capacity;
}
public String getCity() {
return city;
}
public String getCountry() {
return country;
}
public String getDeadline() {
return deadline;
}
public String getEnd() {
return end;
}
public String getStart() {
return start;
}
public String getPrice() {
return price;
}
public String getVenue() {
return venue;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}
Implement Picasso in your project :
implementation 'com.squareup.picasso:picasso:2.71828'
then in your onBindViewHolder make sure you have provided image url not firebase image storage location:
Picasso.get().load(pojo_explore_page_item.getDisplay_Image_URI()).into(holder.eventDisplay);
you can use Glide
add dependencies
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
Add this code in place of your ImageView
Glide.with(context).load(url).into(imageView);
you can load image using glide
Glide.with(your_activity_context).load(pojo_explore_page_item.getDisplay_Image_URI()).placeholder(R.mipmap.placeholder).error(R.mipmap.error).into(holder.eventDisplay);

How do I get only the values insided an array on a texview

I've been trying to show my array of foods on a textview from my Firestore Database. I have been able to view the array on the textview but it is not readable by lame readers, they show in the array braces and the curly brackets. I want to extract the values like produName, Quantity, etc independently
Below is my Model class and Activity class.
public class Request {
private String phone;
private String name;
private String address;
private String total;
private String status;
private List<HashMap<String, String>> foods; //list of food orders
private #ServerTimestamp Date timestamp;
public Request() {
}
public Request(String phone, String name, String address, String total, List<HashMap<String, String>> foods) {
this.phone = phone;
this.name = name;
this.address = address;
this.total = total;
this.foods = foods;
this.status = "0"; //Default is 0, 0: Place, 1: Shipping, 2: Shipped
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTotal() {
return total;
}
public void setTotal(String total) {
this.total = total;
}
public List<HashMap<String, String>> getFoods() {
return foods;
}
public void setFoods(List<HashMap<String, String>> foods) {
this.foods = foods;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
MY ACTIVITY CLASS, I implemented call to firestore at onStart()
#Override
protected void onStart() {
super.onStart();
FirestoreRecyclerOptions<Request> options = new FirestoreRecyclerOptions.Builder<Request>()
.setQuery(query, Request.class)
.build();
adapter = new FirestoreRecyclerAdapter<Request, MainActivity.DataViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull MainActivity.DataViewHolder dataViewHolder, int i, #NonNull final Request mainMenuItem) {
dataViewHolder.setData(mainMenuItem.getTotal(), mainMenuItem.getName(), mainMenuItem.getStatus(), mainMenuItem.getPhone(),
mainMenuItem.getAddress(), mainMenuItem.getTimestamp(), mainMenuItem.getFoods());}
#NonNull
#Override
public MainActivity.DataViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_layout, parent, false);
return new MainActivity.DataViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
public class DataViewHolder extends RecyclerView.ViewHolder {
private View view;
DataViewHolder(View itemView) {
super(itemView);
view = itemView;
}
void setData(String orderTotalValue, String nameOfPerson, String dataOrderStatus, String dataOrderPhone, String dataOrderAddress,
Date dataDateAndTime, final List<HashMap<String,String>>foodsOrdered) {
TextView orderTotal = view.findViewById(R.id.order_total);
TextView orderStatus = view.findViewById(R.id.order_status);
TextView orderPhone = view.findViewById(R.id.order_phone);
TextView orderAddress = view.findViewById(R.id.order_address);
TextView orderName = view.findViewById(R.id.order_name);
TextView orderDateAndTime = view.findViewById(R.id.order_dateTime);
TextView orderFoods = view.findViewById(R.id.order_foods);
orderTotal.setText(orderTotalValue);
orderStatus.setText(convertCodeToStatus(dataOrderStatus));
orderPhone.setText(dataOrderPhone);
orderAddress.setText(dataOrderAddress);
orderName.setText(nameOfPerson);
orderDateAndTime.setText(dataDateAndTime.toString());
//what i must do to foods array when it comes and then finally is attached to the textview!
orderFoods.setText(foodsOrdered.toString());
}
}
You can create a model class (OrderData) with this parameters discount, price, productId, productName, quantity and replace in ActualFoodRequest class
private List<String> foods; //list of food orders
with this
private List<OrderData> foods;

unable to retrieve data from one app to another app from firebase single Database

I have 2 applications(different package names) which use one Firebase database. One app has to write access to the database and another have read access to the database.in my second application, i use recyclerview to retrieve data which is stored by 1st App.
for this I use below code:
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("1:567....259c8f58311") // Required for Analytics.
.setApiKey("AIzaSyA9BRxl......hE03y5qD-c") // Required for Auth.
.setDatabaseUrl("https://mycity-3a561.firebaseio.com/") // Required for RTDB.
.build();
FirebaseApp.initializeApp(this /* Context */, options, "MyCity");
// Retrieve my other app.
FirebaseApp app = FirebaseApp.getInstance("MyCity");
// Get the database for the other app.
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(app);
DatabaseReference data = secondaryDatabase.getInstance().getReference();
data.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()) {
for (DataSnapshot dSnapshot : ds.getChildren()) {
WaterClass waterClass = dSnapshot.getValue(WaterClass.class);
Log.d("Show", waterClass.getName() == null ? "" : waterClass.getName());
list.add(waterClass);
}
adapter = new WaterAdapter(ShowWaterDetails.this, list);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
Adapter class
private class WaterAdapter extends RecyclerView.Adapter<WaterAdapter.ViewHolder> {
ShowWaterDetails showDetail;
List<WaterClass> listData;
public WaterAdapter(ShowWaterDetails showWaterDetails, List<WaterClass> list) {
this.showDetail = showWaterDetails;
this.listData = list;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.show_items, parent, false);
WaterAdapter.ViewHolder viewHolder = new WaterAdapter.ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(WaterAdapter.ViewHolder holder, int position) {
WaterClass AllDetails = listData.get(position);
holder.NameTextView.setText(AllDetails.getName());
holder.DetailTextView.setText(AllDetails.getDetail());
holder.DateTextView.setText(AllDetails.getDate());
holder.LocationTextView.setText(AllDetails.getLocation());
holder.TypeTextView.setText(AllDetails.getType());
Picasso.with(showDetail).load(AllDetails.getImgurl()).resize(120, 60).into(holder.ImageTextView);
}
#Override
public int getItemCount() {
return listData.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView NameTextView;
public TextView DetailTextView;
public TextView DateTextView;
public TextView LocationTextView;
public TextView TypeTextView;
public ImageView ImageTextView;
public ViewHolder(View itemView) {
super(itemView);
NameTextView = itemView.findViewById(R.id.ShowNameTextView);
DetailTextView = itemView.findViewById(R.id.ShowDetailTextView);
DateTextView = itemView.findViewById(R.id.ShowDateTextView);
LocationTextView = itemView.findViewById(R.id.ShowLocationTextView);
TypeTextView = itemView.findViewById(R.id.ShowTypeTextView);
ImageTextView = itemView.findViewById(R.id.ShowImageView);
}
}
}
}
POJO Class
class WaterClass {
private String id;
private String email;
private String name;
private String type;
private String detail;
private String location;
private String date;
private String imgurl;
public WaterClass(){
}
public WaterClass(String id, String currentUserString, String imageUrl, String nameString, String typeString, String detailString, String locationString, String dateString) {
this.id = id;
this.email = currentUserString;
this.name =nameString;
this.type = typeString;
this.detail = detailString;
this.location = locationString;
this.date = dateString;
this.imgurl = imageUrl;
}
public String getImgurl() {
return imgurl;
}
public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
:
there is no error but my recycler not showing anything
go to onStart() and start listening
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
and in your onStop
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
The FirebaseRecyclerAdapter uses a snapshot listener to monitor changes to the Firestore query. To begin listening for data, call the startListening() method. You may want to call this in your onStart() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail.
Be sure that the names of constant in the POJO match exatly the names
of your database structure in your firebase console !!
ps: do not post your api-keys or app-ids in your questions, keep them secret, and consider using firebaserecycleradapter if you are using firebase-database , it will be more easy to setup and to show values.
Your POJO is ok !
Found Solution!!
just change this part of a code
FirebaseApp.initializeApp(this /* Context */, options, "MyCity");
// Retrieve my other app.
FirebaseApp app = FirebaseApp.getInstance("MyCity");
TO
FirebaseApp.initializeApp(this);
// Retrieve my other app.
FirebaseApp app = FirebaseApp.getInstance("[DEFAULT]");

Categories

Resources