Home > OS >  Reading data from db
Reading data from db

Time:12-31

i have db and here JSON file from it:

{
  "User": {
    "mail@gmail::com": {
      "-NKTbcSqxmB4XImNrpDq": {
        "Q": 0,
        "W": 0,
        "QW": 0,
        "WQ": 0,
        }
    },
mail@gmail:com": {
      "-NKTbcTUBxMsXYopKMO-": {
        "Q1": 0,
        "W1": 0,
        "QW1": 0,
        "WQ1": 0,
        }
    }
  }
}

I need to read data once time, when button is pressed, so i need use this listener:

database.child("User").child(getMail1).get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            User value = dataSnapshot.child("User").child(getEmail1).getValue(User.class);
            //saving data in sharedPreferences
            PreferenceConfig.Q1(getApplicationContext(), Q1);
            PreferenceConfig.W1(getApplicationContext(), W1);
            PreferenceConfig.QW1(getApplicationContext(), QW1);
            PreferenceConfig.WQ1(getApplicationContext(), WQ1);
        }
    });

getEmail - variable mail@gmail::com USER_KEY - variable whith have id of user Immediately after this, I create for the second mail exactly the same code as above, only with other values:

database.child(USER_KEY).child(getMail).get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            User value = dataSnapshot.child("User").child(getEmail).getValue(User.class);
            //saving data in sharedPreferences            PreferenceConfig.Q(getApplicationContext(), Q);
            PreferenceConfig.W(getApplicationContext(), W);
            PreferenceConfig.QW(getApplicationContext(), QW);
            PreferenceConfig.WQ(getApplicationContext(), WQ);
        }
    });

getEmail - var whitch have data mail@gmail:com and here is full code:

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
database.child("User").child(getMail1).get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            User value = dataSnapshot.child("User").child(getEmail1).getValue(User.class);
            PreferenceConfig.Q1(getApplicationContext(), Q1);
            PreferenceConfig.W1(getApplicationContext(), W1);
            PreferenceConfig.QW1(getApplicationContext(), QW1);
            PreferenceConfig.WQ1(getApplicationContext(), WQ1);
        }
    });
database.child("User").child(getMail1).get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            User value = dataSnapshot.child("User").child(getEmail).getValue(User.class);
            PreferenceConfig.Q(getApplicationContext(), Q);
            PreferenceConfig.W(getApplicationContext(), W);
            PreferenceConfig.QW(getApplicationContext(), QW);
            PreferenceConfig.WQ(getApplicationContext(), WQ);
        }
    });
});

But the data does not come from the database. There is no error, although I display it like this:

Log.e("TaskEEEE", String.valueOf(task.getException())); Why is the data not read??

CodePudding user response:

Since you get the data at path /User/$getMail1 from your database, the task variable contains a result snapshot of only that data. You can get this result by calling:

database.child("User").child(getMail1).get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            User value = task.getResult().getValue(User.class); //            
  • Related