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) {
}
});
Related
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) {
}
});
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.
I need to do join operation for data using the unique key provided by the Firebase database.Table habitat is created under the unique key of snake table.And also need to retrive data for list view or recyclerview.
databaseSnake = FirebaseDatabase.getInstance().getReference()
.child("snake")
.orderByKey()
.addValueEventListener(
new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
for (DataSnapshot child : dataSnapshot.getChildren())
{
// HERE CORRESPONDS TO JOIN
DatabaseReference Databasesnake = FirebaseDatabase.getInstance().getReference()
.child("habitat")
.orderByKey()
.addValueEventListener(
new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
// repeat!!
}
#Override
public void onCancelled(DatabaseError databaseError)
{
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError)
{
}
I have this item in the database
And I have a method for removing items, but is not working, this is the method
public void deleteBag(String bagUid, final FirebaseDeleteBaglListener listener) {
Query query = dbReference.child(FirebaseChild.bags.name()).child(bagUid);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().setValue(null);
listener.notifyBagDeleted();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
It gets the reference well, but the method setValue(null) is doing nothing (I've also tried the removeValues().
I don't get any exception or some kind of response, I hope you can help me.
Thanks!
I beleive you have a model class that inserts your items into the database.
so what you have to do is to use that class to reference the object directly and perform the required action on it. Something like this.
public void deleteBag(String bagUid, final FirebaseDeleteBaglListener listener) {
Query query = dbReference.child(FirebaseChild.bags.name()).child(bagUid);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//This shouldn't be there
//dataSnapshot.getRef().setValue(null);
//instead use the modelclass you used inserting data, to reference the object
Modelclass mode = dataSnapshot.getValue(Modelclass.class);
/** Note this line indicates the getter and setter method for the particular object that is being referenced**/
mode.setItem(null)
listener.notifyBagDeleted();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
When you execute a query against the Firebase Database, there will potentially be multiple results. You're using a ValueEventListener, so the snapshot contains a list of all results. Even if there is only a single result, the snapshot will contain a list of one result.
You need to loop over DataSnapshot.getChildren() to get the individual items matching your query:
public void deleteBag(String bagUid, final FirebaseDeleteBaglListener listener) {
Query query = dbReference.child(FirebaseChild.bags.name()).child(bagUid);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
child.getRef().removeValue();
}
listener.notifyBagDeleted();
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
}
I'm looking to search through my Firebase database and find a field:value pairing that matches my query, and then return either that parent's key, or the parent object so that I may grab other information as well.
The Firebase database looks something like this:
Events{
-KiXlIGhB6k-HpCKfO3n{
name:"Breakfast at Tiffany's",
owner:"Tim",
startTime:{
startHour:1,
startMinute:30
},
...
},
-dFgfh8Efa-Hpwe6Goqp0{...}
}
I'm currently attempting:
public void importSchedule(String ownerName){
DatabaseReference events =
FirebaseDatabase.getInstance().getReference("Events"); //Inside the Events list
Query allOwnersEvents = events.equalTo(ownerName); //Find events equalTo ownerName provided
allOwnersEvents.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot post : dataSnapshot.getChildren()) {
// This is where the parent's could be queried, all events belonging to an "owner" should be cycled through
}
}
public void onCancelled(DatabaseError databaseError) {}
});
However when placing a break-point inside the for loop, it is never triggered. I'm wondering if I'm attempting the query correctly or if there's an easier way to accomplish this.
It's never triggered because your DatabaseReference is wrong. When you query, you are missing a child. In order to have the correct DatabaseReference please use this code:
DatabaseReference events = FirebaseDatabase.getInstance().getReference("Events")
events.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String eventKey = ds.getKey(); //parent key
DatabaseReference allOwnersEvents = FirebaseDatabase.getInstance().getReference("Events").child(eventKey);
allOwnersEvents.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String ownerName = dataSnapshot.child("ownerName").getValue(String.class); //do what you want with ownerName
}
public void onCancelled(DatabaseError databaseError) {}
});
}
}
public void onCancelled(DatabaseError databaseError) {}
});
In which eventId is the unique id generated by the push() method. Hope it helps.