0

In my solution I save data from Windows form application. There is no problem. I can list them in my android app. On this point I am trying to get key value to update the data, but always I get null.

my firebase database

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myReference = database.getReference();

    Query myTopPostsQuery = myReference.child("DURUSLAR").orderByChild("kayitid").equalTo("1298843637");
    myTopPostsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren())
            {
                //Childadi =postSnapshot.getKey().toString();
                String anahtar=postSnapshot.getKey().toString();
                Toast.makeText(getApplicationContext(),anahtar,Toast.LENGTH_LONG);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Can you please help me?

AL.
  • 36,815
  • 10
  • 142
  • 281

2 Answers2

0

When you execute a query at a location in the Firebase Database, it will search for the property you order/filter on in each child under that location. Since you query in /DURUSLAR, Firebase looks for /DURUSLAR/{something}/kayitid. Such a property does not exist, since you only have /DURUSLAR/{something}/{pushid}/kayitid.

To fix this problem you have two options:

  1. query at a lower level
  2. query to the property at its (fixed) path

The first option is to create the query at a lower level in the tree:

Query myTopPostsQuery = myReference.child("DURUSLAR/K6").orderByChild("kayitid").equalTo("1298843637");

Now the query is looking for /DURUSLAR/{pushid}/kayitid and it will work.

The second option is to query for the known path of the property:

Query myTopPostsQuery = myReference.child("DURUSLAR").orderByChild("K6/-KknsAR4_KFAeZ1HVXPW/kayitid").equalTo("1298843637");

It seems unlikely you want this here, but the approach may be helpful in other situations. Often when you need this approach with push IDs in the path, you'll want to look at http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

The reason you are getting null is because you are doing orderByChild on the wrong level of data . According to your data u need to have a child K4-> which has a unique_id_push_id -> kayitid.Therefore you need to traverse the K4 in order to get 1298843637 for key kayitid.You can follow this tutorial to understand the retrieving of data from firebase .

 Query myTopPostsQuery = myReference.child("DURUSLAR").child("k6").orderByChild("kayitid").equalTo("1298843637");
    myTopPostsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren())
            {
                //Childadi =postSnapshot.getKey().toString();
                String anahtar=postSnapshot.getKey().toString();
                Toast.makeText(getApplicationContext(),anahtar,Toast.LENGTH_LONG);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
DRY Believer
  • 1,001
  • 11
  • 20