Search for a value without knowing child - android

Here is a screenshot of my database
Firebase Database Image
I got the user key ID by writing the following code
userID=Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid();
I need to search for the child in that key ROLE to check if it's STUDENT or INSTRUCTOR
The problem is all the solutions I've found it online require a parent node to search through , I don't know if this user is a student or instructor , so I can't just write
FirebaseDatabase.getInstance().getReference().child("students")
or
FirebaseDatabase.getInstance().getReference().child("instructors")
I've tried to access the key directly but with no luck
FirebaseDatabase.getInstance().getReference().child(userID);
So, how could this be implemented?

Check for both
dbRef = FirebaseDatabase.getInstance().getReference().child("students");
dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild("userID"));
{
//isStudent = true;
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
and
dbRef = FirebaseDatabase.getInstance().getReference().child("instructors");
dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild("userID"));
{
//isInstructor = true;
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
The value of isStudent and isInstructor will help you find, whether the user was a student or an instructor. If user id is present only in either student or instructor then only one of the variables isStudent or isInstructor will be true. If both are false then user does not exist.

Related

read data from firebase and set the value of textview

i faced an issue like user entered enrollment number and verify and read all data from that and set the value to text view
If user enter enrollment 66 then verify that enrollment and read all that data from 66 and set value of text view to marks of subjects.what i need to do if i want to solve Above problem?
I tried:
reff= FirebaseDatabase.getInstance().getReference().child("Marks");
reff.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String mm=dataSnapshot.child("mcad").getValue().toString();
String jm=dataSnapshot.child("java").getValue().toString();
String nm=dataSnapshot.child("nma").getValue().toString();
txtmcadmarks.setText(nm);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
First, your Database Reference returns you a list of data so you need to receive your data as a list.
Second, if you want to filter your data base on enrollment then add a query in your database reference.
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
Query query = databaseReference.child("Marks").orderByChild("enrollment").equalTo(66);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot data: dataSnapshot.getChildren()){
String mm=data.child("mcad").getValue().toString();
String jm=data.child("java").getValue().toString();
String nm=data.child("nma").getValue().toString();
txtmcadmarks.setText(nm);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

How can I get the value of an item from firebase using uid?

firebase realtime DB
I pass the id of the item to the next activity. I would like to use the id to retrieve the name of the category and set it to a string. How can I do it?
FirebaseDatabase.getInstance().getReference("categories").child("pass your id")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
In onDataChange you can receive the value.

Jump to Firebase parent Nodes from Child Nodes

Hello all i have a my fire-base structure like this
Firebase Database
I have an array list inside an node in fire-base , I need to loop through this list and jump to each ID to get information i need like the database photo shows.
what I've tried so far
public static void checkIfCurrentUserLiked(final HomeFragment.PostsViewHolder postsViewHolder, String postKey) {
userID = Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid();
likesRef = FirebaseDatabase.getInstance().getReference().child("likes");
postsRef = FirebaseDatabase.getInstance().getReference().child(postKey);
postsRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("likes")) {
for (DataSnapshot ds : dataSnapshot.child("likes").getChildren()) {
likesRef.child(Objects.requireNonNull(ds.getValue()).toString())
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
Like like = snapshot.getValue(Like.class);
if (like != null && like.getLikedUserID().equals(userID)) {
postsViewHolder.likesImage.setImageResource
(R.drawable.ic_item_home_post_like_filled);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }
});
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }
});
}
i have no error , just nothing happens , data are not fetched as expected
Change this:
postsRef = FirebaseDatabase.getInstance().getReference().child(postKey);
into this:
postsRef = FirebaseDatabase.getInstance().getReference().child("posts").child(postKey);
According to your database, you have node posts before the postKey, therefore you need to add it when referencing to a location.

Is there any way to read data offline without setPersistance(true) with firebase

I want to read data offline for my users, but Persistance mode will make OnDataChange in ValueEventListener call twice. how can I prevent it. I need to use ValueEvent for my own purpose. someone help me please.
Update : I am making a friend suggestions list for users in my app, the suggested user must dont have any relationship with the current user. and this is my code ( I use Persistance for offline data as Firebase's suggestion)
valueEventListener_check_relationship=new ValueEventListener() {
#Override
public void onDataChange(#NonNull final DataSnapshot dataSnapshot_root) {
num_fiend_req=0;
num_sent_req=0;
update_badges(num_sent_req,tv_badges_sent_req);
update_badges(num_fiend_req,tv_badges_friend_req);
arrayList_suggestions.clear();
adapter_suggestions.notifyDataSetChanged();
GlobalConstants.ROOT_REF.child(GlobalConstants.CHILD_USERS)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot:dataSnapshot.getChildren()){
Object_User user_suggested=snapshot.getValue(Object_User.class);
if(!user_suggested.user_id.equals(GlobalConstants.getMyUID())){
if(!dataSnapshot_root.hasChild(user_suggested.user_id))
addToList(user_suggested);
else {
RemoveUserFromlist(user_suggested);
if (dataSnapshot_root.child(user_suggested.user_id)
.hasChild(GlobalConstants.CHILD_REQUESTING)){
String values=dataSnapshot_root.child(user_suggested.user_id)
.child(GlobalConstants.CHILD_REQUESTING)
.getValue().toString();
if (values.equalsIgnoreCase(GlobalConstants.VALUE_REQUESTING_SENDER))
num_sent_req++;
else num_fiend_req++;
}
update_badges(num_sent_req,tv_badges_sent_req);
update_badges(num_fiend_req,tv_badges_friend_req);
adapter_suggestions.notifyDataSetChanged();
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
};
GlobalConstants.ROOT_REF.child(GlobalConstants.CHILD_RELATIONSHIP)
.child(GlobalConstants.getMyUID()).keepSynced(true);
GlobalConstants.ROOT_REF.child(GlobalConstants.CHILD_RELATIONSHIP)
.child(GlobalConstants.getMyUID())
.addValueEventListener(valueEventListener_check_relationship);

Retrieving Data from Firebase with certain ID

I have an ID of User, and i want to take his name from the firebase. So, i am trying to use orderByKey() method to find a certain user and after that, take information from his profile. But something goes wrong ...
reference = FirebaseDatabase.getInstance().getReference("Users");
Query query = reference.orderByKey().equalTo(task.author_id);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
fullName = dataSnapshot.getValue(User.class).getFullName();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Firebase Structure
You don't need a query in that case if you already know the specific path to that node. Just add the listener directly.
reference = FirebaseDatabase.getInstance().getReference("Users");
reference.child(task.author_id).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
fullName = dataSnapshot.getValue(User.class).getFullName();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Categories

Resources