简体   繁体   中英

Reading data from db

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??

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); // 👈
            ...

Also see the Firebase documentation on getting data once , on which I based this answer.


In addition, it seems you have a push key under the email, which will require you to either know the key or use a loop over the child nodes to get to the properties:

public void onComplete(@NonNull Task<DataSnapshot> task) {
    if (task.isSuccessful()) {
        for (DataSnapshot userSnapshot: task.getResult().getChildren()) { // 👈
            User value = userSnapshot.getValue(User.class);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM