0

I want to know a method for download data saved into my Firebase database. For adding data into my firebase database, I use a push node so I can add multiple post for the same uid (user id). I saved successfully data into my database but I'm not able to download them. I've learned the firebase documentation and I know that I can use datasnapshoot but I don't know how to implement it on my code. Hope you can help me. Thank you.

Upload data into firebase database. I use this method which saves data in this format:

Data 
  - example-data 
       - uid 
          - key (push key generated by firebase)
                - dataOne : dataOne 
                - dataTwo : dataTwo 
                
..........code of the method .........................

private void addData(String dataOne, String dataTwo) {
    final String key;
    final String uid;

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    uid = user.getUid();

    key = mDatabase.child("Data").push().getKey();
    data data = new data(dataOne, dataTwo);
    Map<String, Object> postValues = data.toMap();

    Map<String, Object> childUpdates = new HashMap<>();
    childUpdates.put("Data" + "/example-data/" + uid + "/" + key, postValues);

    mDatabase.updateChildren(childUpdates)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {

                    Intent intent = new Intent(Prova.this, Test.class);

                    startActivity(intent);
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });
}

Calling method addData for uploading by button:

public void buttonAddData(View view) {
    
    String dataOne = mDataOne.getText().toString();
    String urlSecondaImmagine = mDataTwo.getText().toString();


    addData(dataOne, dataTwo);

}

This is the data class:

public class data {

public String dataOne;
public String dataTwo;

public data(){}

public data (String dataOne, String dataTwo){
    this.dataOne = dataOne;
    this.dataTwo = dataTwo;
}

public String getDataOne() {
    return dataOne;
}
public String getDataTwo() {
    return dataTwo;
}

@Exclude
public Map<String, Object> toMap() {
    HashMap<String, Object> result = new HashMap<>();
    result.put("dataOne", dataOne);
    result.put("dataTwo", dataTwo);

    return result;
}

} 

My main task is to download these two data saved into my database. I need that these two data was displayed into other activity and displayed into a textview.

These two number: 9hiJ0YxyZXZnlrKFxv3jyhQlKUG2 LOca2YbV6Qb5q1RG1ws. First is ID of the user second is the push key generated by firebase database:

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Fetex
  • 90
  • 6

1 Answers1

0

To solve this, please use the following code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Annunci").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String categoria = ds.child("categoria").getValue(String.class);
            Log.d(TAG, categoria);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

The output in your logcat will be:

Electtronic
//other items

If you are using a model class for that object, then you should use the following line of code to get the desired object:

YourModelClass yourModelClass = ds.getValue(YourModelClass.class);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • how i can display the getted value into an array like this – Fetex Oct 14 '18 at 11:14
  • array.add(new data(categoria)); – Fetex Oct 14 '18 at 11:14
  • data is my model class – Fetex Oct 14 '18 at 11:14
  • i can't put the code of this array inside the onDataChange method, because it is used in other method – Fetex Oct 14 '18 at 11:15
  • In **[this](https://stackoverflow.com/questions/48622480/showing-firebase-data-in-listview)** way. Is it ok now? – Alex Mamo Oct 14 '18 at 11:33
  • Hi Dra! Is there everything alright, can I help you with other informations? – Alex Mamo Oct 15 '18 at 07:21
  • sorry but i can't understand how to implement this code, have looked my code. Your is a copy paste from your other answer, but don't fill my code – Fetex Oct 16 '18 at 16:44
  • Is not a copy paste from another answer is a code specific to your db. What have you tried and it isnt't work? – Alex Mamo Oct 17 '18 at 03:14
  • my array adapter is out of the ondatachange method so i'm not abele to get the string categoria... – Fetex Oct 17 '18 at 10:24
  • the string categoria is received only inside this method, but how can i get the string categoria outside this method – Fetex Oct 17 '18 at 10:25
  • Move the ArrayAdapter inside the `onDataChange()` method or if your want to use the list outside that method, I recommend you see the last part of my anwser from this **[post](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** in which I have explained how it can be done using a custom callback. You can also take a look at this **[video](https://www.youtube.com/watch?v=OvDZVV5CbQg)** for a better understanding. – Alex Mamo Oct 19 '18 at 06:41
  • Do you think that my answer/comments helped you? – Alex Mamo Oct 19 '18 at 07:06