0

Below is my database structure

enter image description here

I have created a recyclerView in an activity to show all the bookings made by users. The recycler View works fine but the problem is that it shows all the details of bookings made by every user. I want to only display Bookings made by the current logged In user. I have created userID by getUid. Following is Model class to retrieve data from firebase.

public class Booking {
private String userEmail;
private String NameOfPlace;
private String Slotnumber;
private String Duration;
private String StartTime;
private String Price;
private String currentDate;

public Booking() {
}

public Booking(String userEmail, String nameOfPlace, String slotnumber, String duration, String startTime, String price, String currentDate) {
    this.userEmail = userEmail;
    NameOfPlace = nameOfPlace;
    Slotnumber = slotnumber;
    Duration = duration;
    StartTime = startTime;
    Price = price;
    this.currentDate = currentDate;
}

public String getUserEmail() {
    return userEmail;
}

public void setUserEmail(String userEmail) {
    this.userEmail = userEmail;
}


public String getNameOfPlace() {
    return NameOfPlace;
}

public void setNameOfPlace(String nameOfPlace) {
    NameOfPlace = nameOfPlace;
}

public String getSlotnumber() {
    return Slotnumber;
}

public void setSlotnumber(String slotnumber) {
    Slotnumber = slotnumber;
}

public String getDuration() {
    return Duration;
}

public void setDuration(String duration) {
    Duration = duration;
}

public String getStartTime() {
    return StartTime;
}

public void setStartTime(String startTime) {
    StartTime = startTime;
}

public String getPrice() {
    return Price;
}

public void setPrice(String price) {
    Price = price;
}

public String getCurrentDate() {
    return currentDate;
}

public void setCurrentDate(String currentDate) {
    this.currentDate = currentDate;
}
}

Following is the Main Acitivity followed by recyclerviewHolder java class:

FirebaseDatabase mdatabase;
DatabaseReference refbooking;
RecyclerView recycler_booking;
RecyclerView.LayoutManager layoutManager;

FirebaseRecyclerAdapter<Booking, BookingViewHolder> adapter;

String uid;
FirebaseUser user;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v= inflater.inflate(R.layout.view_booking, null);

    mdatabase=FirebaseDatabase.getInstance();
    refbooking=mdatabase.getReference().child("Bookings");

    user= FirebaseAuth.getInstance().getCurrentUser();
    uid=user.getUid();

    recycler_booking=(RecyclerView)v.findViewById(R.id.BookingRecyclerView);
    recycler_booking.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(getActivity());
    recycler_booking.setLayoutManager(layoutManager);

    loadMenu();

    return v;
}

private void loadMenu() {
    adapter=new FirebaseRecyclerAdapter<Booking, BookingViewHolder>(Booking.class, R.layout.viewbooking_layout, BookingViewHolder.class, refbooking) {
        @Override
        protected void populateViewHolder(final BookingViewHolder viewHolder, final Booking model, int position) {

viewHolder.bookedplace.setText(model.getNameOfPlace());
                    viewHolder.bookedplace.setText(model.getNameOfPlace());
                    viewHolder.bookedslot.setText(model.getSlotnumber());
                    viewHolder.bookeddate.setText(model.getCurrentDate());
                    viewHolder.bookedduration.setText(model.getDuration());
                    viewHolder.bookedstarttime.setText(model.getStartTime());


           }
    };
    recycler_booking.setAdapter(adapter);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getActivity().setTitle("View Booking");
}
}

ViewHolder Class:

public class BookingViewHolder extends RecyclerView.ViewHolder {
public TextView bookedplace;
public TextView bookedslot;
public TextView bookeddate;
public TextView bookedduration;
public TextView bookedstarttime;

public BookingViewHolder(View itemView) {
    super(itemView);

    bookedplace= (TextView) itemView.findViewById(R.id.bookedplace);
    bookedslot= (TextView) itemView.findViewById(R.id.bookedslot);
    bookeddate= (TextView) itemView.findViewById(R.id.bookeddate);
    bookedduration=(TextView) itemView.findViewById(R.id.bookedduration);
    bookedstarttime= (TextView) itemView.findViewById(R.id.bookedstarttime);

}
}

Please help me it will be really appreciated. Thankyou

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56

1 Answers1

0

I do not see where you query your Firebase Database, or you do it or not. As far as I see you are not retrieving data from Firebase. Possible solution is to query your "booking" table with userEmail and get the data according to it. Another possible solution is to hold a variable for userId inside "booking" class, and of course in Firebase Database. Then query your "booking" table with the current user's uid.

Nope
  • 111
  • 2
  • 12
  • If i query the database with userEmail, i need to use equalTo? –  May 10 '18 at 12:10
  • I assume you store the current user's email while creating a booking instance. So when you want to list bookings of the current user, you can query with userEmail. In order to check userEmail, since it is a String instance you should use **.equals()** method. – Nope May 11 '18 at 06:25