M.KJ
M.KJ

Reputation: 15

Showing Specific Data in by using the key in Firebase

I'm new to android development and firebase so this may be a simple question.

I want to develop an application that matches the name in the grandChild in the firebase and then get the data of the key around it but the problem is when I retrieve the data it get the value of all child node in the database.

Here's the code that I have done

private void searchText() {
    final String mSearch = searchBar.getText().toString();

    //return REQUIRED if the searchBar is in filled
    if (TextUtils.isEmpty(mSearch)) {
        searchBar.setError(REQUIRED);
        return;
    }


    ValueEventListener valueEventListener = mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot child : dataSnapshot.getChildren()){


                for (DataSnapshot grandChild : child.getChildren()) {


                    //check if mSearch is equal any grandchild in the firebase'
                    if(grandChild.getValue().toString().equals(mSearch)){

                        TextView mText = findViewById(R.id.searchResult);
                        String string = child.getValue().toString();
                        String string2 = string.replace("{","");
                        String string3 = string2.replace("}","");
                        String string4 = string3.replace(",","\n");

                        //retrieve the data from firebase and show in TextView
                        mText.setText(string4);
                    }
                }

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    mDatabase.addValueEventListener(valueEventListener);
}

the result is

{ Message = "...",Priority = "..",name="...",time="..."}

but the result that I want is only name and priority

{ Message = "..",name=".."}

Is there any way to specify the route of the data that is retrieved?

Upvotes: 0

Views: 72

Answers (3)

Al Walid Ashik
Al Walid Ashik

Reputation: 1779

You can query through your grand child. Suppose you want to search by name from your database. To do this, follow below code

        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Data");

        Query query = databaseReference.orderByChild("name")
             .startAt(mSearchText)
             .endAt(mSearchText+"\uf8ff");

        query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    for (DataSnapshot data:dataSnapshot.getChildren()){

                         String name = data.child("name").getValue().toString();
                         String priority = data.child("Priority").getValue().toString();

                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

Upvotes: 1

Peter Haddad
Peter Haddad

Reputation: 80924

To be able to retrieve the name and the Message you can do the following:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Data");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
     if(dataSnapshot.child("name").getValue().toString().equals(mSearch)){
       String message = dataSnapshot.child("Message").getValue().toString();
        mText.setText(message);
         }
    }

@Override
public void onCancelled(DatabaseError databaseError) {
   }
});

In the above code, you will be able to retrieve the name and the message, and then check if the name is equal to the value that is written in the search bar. After that you can assign the message to a TextView

Upvotes: 1

akhan001
akhan001

Reputation: 66

Why don't you first parse the JSON into an object and then utilise only the required parameters you need. For parsing, you can use SimpleJSON library.

Upvotes: 1

Related Questions