Firebase RecyclerView Adapter - Join Query - android

I am trying to get UserID from one table(homeaccount) and get the user's info from another table(userinfo) using the user id. I tried to do it by the following way. But, it is overwriting the cardviews one on top of another.
databaseReference.child("homeaccount/"+globalSharedPrefs.getUserDetail("currenthome")+"/Users")
.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String str_iteration_uid = dataSnapshot.getKey();
userQuery = databaseReference.child("userinfo").orderByChild("uid").equalTo(str_iteration_uid);
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<HomeUsers, HomeUsersViewHolder>(
HomeUsers.class,
R.layout.cardview_manage_user,
HomeUsersViewHolder.class,
userQuery
){
#Override
protected void populateViewHolder(final HomeUsersViewHolder viewHolder, final HomeUsers model, int position) {
//viewHolder.setFirstName(model.getFirstName());
//viewHolder.setProfilePicture(model.getProfilePicture());
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Related

how to get data from firebase database?

how can i get data from firebase Realtimedatabase?
i very new on Android an have no plan how to do this. can someone suggest me?
is this ok?
ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
Post newPost = dataSnapshot.getValue(Post.class);
System.out.println("Author: " + newPost.author);
System.out.println("Title: " + newPost.title);
System.out.println("Previous Post ID: " + prevChildKey);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
you can use this:
DatabaseReference.getInstance().getReference("user").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Child child = dataSnapshot.getValue(Child.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

GraphView appendData() only connects points until 2nd datapoint that is being retrieved from firebase ChildEventListener - onChildAdded()

So I retrieved the data from Firebase database which is the "height". Whenever I add a height data, its supposed to plot it and connect to the previous height data.
So this is my series.
graph.addSeries(seriesheight);
seriesheight.setColor(Color.rgb(0,0,255));
seriesheight.setThickness(6);
seriesheight.setTitle("Height");
seriesheight.setDrawDataPoints(true);
seriesheight.setDataPointsRadius(8);
seriesheight.setOnDataPointTapListener(new OnDataPointTapListener() {
#Override
public void onTap(Series series, DataPointInterface dataPoint) {
Toast.makeText(getActivity(), dataPoint+ "\n" + date, Toast.LENGTH_SHORT).show();
}
});
and this is where I append the data
lastlastref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String heightu = dataSnapshot.child("height").getValue(String.class);
date = dataSnapshot.child("time_stamp").getValue(String.class);
if (heightu != null) {
height = Double.parseDouble(heightu);
seriesheight.appendData(new DataPoint(months, height), true, 100);
lastlastref.keepSynced(true);
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
this is the result
It only connects to the 2nd data. How can I connect all datapoints?

how can i order recyclerview items in firebase by replay timestamps

how can I order my recyclerview items like this
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
productQuery = mDatabaseReference.child("Blog").orderByChild("replaydate");
"replaydate" is located in Replay>psotkey>replaydate I want order blog items by repaydate, is this possiple?
If you want to get the messages by timestamp order you can use
database.orderByChild("replydate").limitToLast(10)
.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
Model newModel = dataSnapshot.getValue(Model.class);
//Model is you model which you are using for showing data in recyclerview
arraylist.add(newModel);
adapter.notifyDatasetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

how to get value of user id on firebaseRecyclerAdapter

my database:
I want to retrieve user id from firebase database. How can I do that?
my code is :
///////////// Recycler Firebase /////////////
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(
Post.class,
R.layout.post_row,
PostViewHolder.class,
dbpostShow
) {
#Override
protected void populateViewHolder(final PostViewHolder viewHolder, Post model, int position) {
String listPostKey = getRef(position).getKey();
viewHolder.setTextPost(model.getText_post());
viewHolder.setHospitalName(model.getHospital_name());
viewHolder.setFullNameUserPoster(model.getPoster_name());
viewHolder.setAgeUserPoster(model.getPoster_age());
String lastSeenAgo = GetTimeAgo.getTimeAgo(model.getTime_post_ago(), getContext());
viewHolder.setTimePostAgo(lastSeenAgo);
viewHolder.setTypeBloodneedPoster(model.getPoster_type_blood());
Toast.makeText(getActivity(), model.getPoster_type_blood(), Toast.LENGTH_SHORT).show();
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
If you have created the variable userId in Post class and use getter to get the value.
Otherwise you can use addListenerForSingleValueEvent to get the value:
here is the example how to use it.
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(
Post.class,
R.layout.post_row,
PostViewHolder.class,
dbpostShow
) {
#Override
protected void populateViewHolder(final PostViewHolder viewHolder, Post model, int position) {
String listPostKey = getRef(position).getKey();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Post").child("country").child("city").child(listPostKey).child("userId");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String userID = String.valueOf(dataSnapshot.getValue());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
you cant track value when your user id is added on the following code below , and make sure your parent position is correct
yourRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
})

Retrieve Random Object from Firebase Android

I want to be able to return random objects from the following image:
And not just the first object (-Kci1vM1dVBYBcPW7QA) which returns as is already. I have inserted the code from my app where it is currently reading from. The variables one, two and three are currently reading the first object but I want them to be able to retrieve random objects.
public void displayDeals(){
databaseReference.child("FruitDeals").child("bR4sR4flMkYFrw1SzuWA8hpOnY52").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
deals_information = dataSnapshot.getValue(Deals_Information.class);
one = deals_information.getDeal();
two = deals_information.getPrice();
three = deals_information.getAisleNum();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}

Categories

Resources