Skip to content
Advertisement

Get specific child data from firebase

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:

Structure

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();
        }
    });
}

Advertisement

Answer

Get a specific node inside 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"

Get all nodes inside 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"
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement