Reputation: 41
I have to access a child from a node. I have only "value" for a specific child and to get the Type
value from the same node.
I have to return the value:
I've done a lot of googling and I just can't figure out how this works. My current version of the java code is this.
private String GetCategory(String sName ) {
DatabaseReference db = FirebaseDatabase.getInstance().getReference("Category");
Query query = db.orderByValue().equalTo("Salary");//.orderByValue().equalTo("Salary");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
String eType = userSnapshot.child("Type").getValue(String.class);
Log.d("Catagory","Value"+ eType);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
Upvotes: 1
Views: 658
Reputation: 138824
Creating a reference to the Category
node, attaching a listener to getting all children of the node and do a comparison on the client, is not the best option to go ahead with. If your node will contain 1 MIL children, are you willing to download all of them in order to read only a single one? That's an incredible waste of bandwidth and resources. What you should do, is to create a Query that can return only the information you are interested in. Assuming you need the information from the first child, please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference categoryRef = rootRef.child("Category");
Query queryByName = categoryRef.orderByChild("Name").equalTo("Salary");
queryByName.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
for (DataSnapshot snapshot : task.getResult().getChildren()) {
String type = snapshot.child("Type").getValue(String.class);
Log.d("TAG", type);
}
} else {
Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
}
}
});
The result in the logcat will be:
Income
Assuming that the first object is the only one that satisfies the Query, then you'll get only a single element from the database. This is extremely fast and consumes an incredibly small amount of data.
Upvotes: 0
Reputation: 2342
Category
If you want to get the node which id
is 1, you could try this:
private String GetCategory(String sName ) {
DatabaseReference db = FirebaseDatabase.getInstance().getReference("Category");
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
// check for desired id here
if(userSnapshot.getKey().equals("1")) {
String icon = userSnapshot.child("Icon").getValue().toString();
String name = userSnapshot.child("Name").getValue().toString();
String type = userSnapshot.child("Type").getValue().toString();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
Output of the code above:
icon = "01"
name = "Salary"
type = "Icome"
Category
If you want to read all the node inside Category
, just provide an array
of hash map
or class
and change the onDataChange
method like this:
ArrayList<ExampleClass> array = new ArrayList<>();
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
ExampleClass example = new ExampleClass();
example.setIcon(userSnapshot.child("Icon").getValue().toString());
example.setName(userSnapshot.child("Name").getValue().toString());
example.setType(userSnapshot.child("Type").getValue().toString());
}
but first, you have to create the ExampleClass
file with setter
and getter
functions inside it.
Output:
array[0].getIcon = "01"
array[0].getName = "Salary"
array[0].getType = "Icome"
...
array[2].getIcon = "07"
array[2].getName = "Other Income"
array[2].getType = "Income"
Upvotes: 2