1
votes

I am reading the data from the firebase database.Following is snapshot of the data stored in database.

enter image description here

In the snap string starting with "8SS..." is the uid of the user. Following is the code for retrieving the data from firebase database.

//To check if uid of current user and database user matches.
Query q = FirebaseDatabase.getInstance().getReference().child("Location").child(user.getUid()).equalTo(FirebaseAuth.getInstance().getCurrentUser().getUid());
q.addListenerForSingleValueEvent(
        new ValueEventListener() {
             @Override
             public void onDataChange(DataSnapshot dataSnapshot) {
                     for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                            Log.d(TAG, "Yay!!");
                            User us = singleSnapshot.getValue(User.class);
                            String string = "Name: "+ us.getName()+"\nAddress: "+ us.getlat()+ us.getlon()+ "\n\n";
                            n.setText(string);
                   }
                }

             @Override
             public void onCancelled(DatabaseError databaseError) {
                        // read query is cancelled.
                        Log.d(TAG, "loadPost:onCancelled", databaseError.toException());
                    }
             });

User class contains getters and setters.
The error is that only empty Text View appears concluding reading from database fails.

  1. How to evaluate if query is true or false?
  2. What is the error while reading from ValueEventListener()?

I tried using this:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Location").child("8SS0uk4FmiPUtXP208Tx8Cqxt2z2");

And then calling on ref.addListenerForSingleValueEvent() but still nothing gets displayed.

I tried using this:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Location").child(user.getUid());

This gives dataSnapShot : "DataSnapshot={key='-Kn...', value="latitude:.., longitude:..., Name:..."}. But this is not how I expected it to be.
The database structure should have been Location --> Uid --> Name : "Jane", .. .

This is my code for inserting data in the database.

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser() ;
refDatabase = FirebaseDatabase.getInstance().getReference().child("Location").child(user.getUid());
DatabaseReference newPost = refDatabase.push();
//the push() command is already creating unique key
Map<String, String> mapname = new HashMap<String, String>();
mapname.put("Name", n.getText().toString());
mapname.put("latitude", Double.toString(lat));
mapname.put("longitude", Double.toString(longt));
mapname.put("user id", user.getUid());
newPost.setValue(mapname);
2

2 Answers

1
votes

I solved this question by introducing multiple for loops.
So, the snapshot of my first child was dataSnapShot : "DataSnapshot={key='-Kn...', value="latitude:.., longitude:..., Name:..."}.
Below is the code to extract all the values and keys :

mRef.addValueEventListener(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Log.d(TAG, "Children" + dataSnapshot.getKey());
                    for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                        String st = singleSnapshot.getKey();
                        Log.d(TAG, "Yay!!" + singleSnapshot.child(st));
                        st = "";
                        int count=0;
                        for(DataSnapshot singleSnap : singleSnapshot.getChildren()) {
                            Log.d(TAG, "String" + singleSnap.getValue());
                            //n.setText(us.getName());
                            if(count==0) {
                                st = "Name: " + singleSnap.getValue() + '\n';
                            }
                            else if(count==1) {
                                st = st + "Latitude: " + singleSnap.getValue() + '\n';
                            }
                            else if(count==2) {
                                st = st + "Longitude: " + singleSnap.getValue() + '\n';
                            }
                            count++;
                        }
                        final TextView rowTextView = new TextView(Menu5.this.getActivity());
                        rowTextView.setText((CharSequence) st);
                        ll.addView(rowTextView);
                    }
                }

This gives single key and value pair for every unique id of created by push.So, I had to hard code the concatenation and display as the structure will remain same throughout the app.

0
votes

Why are you using equal to and then getting the current user. .child(user.getUid()) should already be your current user which gives you the value of the child you are trying to listen to.

I think the uuid's are the children of "8SSOuk.......". So it should look something like this: FirebaseDatabase.getInstance().getReference().child("Location").child("8SSOuk.......").child(user.getUid());