0

I am writing data to the database using another class with a constructor, but I still need to read data from the database. In the documentation, when using other classes, you need to create HashMap lists for each element, but I have 2 classes (since I need to write more than 255 entries to the database) and in each class, I will have to write a HashMap. How can I load the name of a DB variable that is identical to the name in the file itself? For example int b = 0; and in the database - b: 0 and how can you get the value of each variable from the database? I send data like this:

if (user != null) {
if(database.child(getEmail)==null) {
User newUser = new User(getEmail, coins, ....);
User1 newUser1 = new User1(a256, a257, ....);
database.child(getEmail).push().setValue(newUser1);
database.child(getEmail).push().setValue(newUser);
}

I read data like this:

ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot ds : snapshot.getChildren()){
User user = ds.getValue(User.class);
//saving information witch will read from db in SharedPreferences
PreferenceConfig.GetEmail(getApplicationContext(), getEmail);
PreferenceConfig.GetCoin(getApplicationContext(), getEmail);
PreferenceConfig.GetA256(getApplicationContext(), a256);
...
}
}
database.addValueEventListener(valueEventListener);

But i can`t understend how can i get data from db without hashmap JSON file:

{
  "User": {
    "mail@gmail:com": {
      "-NHTVinbEVUAqJwK8Umt": {
        "getEmail": "mail@gmail.com",
        "coins": 100,
        .....
      },
      "-NHTVinpCPOJ4UPZvgpN": {
        "a256":0,
        "a257":0
          ...............
      }
    }
  }
}
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • 1
    Please edit your question and add the code doesn't work the way you expect. Please also show us a concrete example of what you want to achieve. – Alex Mamo Nov 22 '22 at 14:30
  • @AlexMamo, Is this anought? – Dmitriy Parkhomenko Nov 23 '22 at 09:42
  • Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Nov 23 '22 at 09:52
  • @AlexMamo, i haven't any errors (maybe i didn't see them) i have problem with reading data. I have variable `coins` in db and in code of application. But in application i have number 0, but in db i have 5000 – Dmitriy Parkhomenko Nov 23 '22 at 10:56
  • Please edit your question and add your database structure as a JSON file. You can simply get it by clicking the Export JSON in the overflow menu (⠇) in your [Firebase Console](https://console.firebase.google.com/u/0/project/_/database/data). – Alex Mamo Nov 23 '22 at 12:35

1 Answers1

1

You won't be able to read the data under fields that are dynamically created:

{
  "User": {
    "mail@gmail:com": {
      "-NHTVinbEVUAqJwK8Umt": {
        "getEmail": "mail@gmail.com",
        "coins": 100,
      },
      "-NHTVinpCPOJ4UPZvgpN": {
        "a256":0, //
        "a257":0 //
      }
    }
  }
}

You'll be able to read all the data if your second child will have the same fields as the first one:

{
  "User": {
    "mail@gmail:com": {
      "-NHTVinbEVUAqJwK8Umt": {
        "getEmail": "mail@gmail.com",
        "coins": 100,
      },
      "-NHTVinpCPOJ4UPZvgpN": {
        "getEmail": "other@gmail.com", //
        "coins": 200, //
      }
    }
  }
}

Now, to read the data from such a structure, you have to create a reference that points to mail@gmail:com node and make a get() call as you can see in the following lines of code:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference emailRef = db.child("User").child("mail@gmail:com");
emailRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String email = ds.child("getEmail").getValue(String.class);
                Log.d("TAG", email);
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

The result in the logcat will be:

mail@gmail.com
other@gmail.com

Or you can map each DataSnapshot object into an object of type User:

User user = ds.getValue(User.class);

This operation will work, only if your User class contains two fields called getEmail and coins. The first being a string and the second one a number.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • but, for example i have 25 variables with zero data, like: `"a":0, "b":0"` i can`t read them? – Dmitriy Parkhomenko Nov 25 '22 at 13:30
  • The fields in the database should **always** match the ones in the class. Is this happening? If no, make it like that. – Alex Mamo Nov 25 '22 at 13:37
  • But... Can i do something? I have offline app and person send data into db when he pressed the button/ And when person first time login in app i need to check data in db and when this email is not missing - app will read data – Dmitriy Parkhomenko Nov 25 '22 at 13:59
  • This is how you can [check if a user exists](https://stackoverflow.com/questions/47893328/checking-if-a-particular-value-exists-in-the-firebase-database/47893879). – Alex Mamo Nov 25 '22 at 14:05
  • So try make your own attempt given the information in the answer and ask another question if something else comes up. – Alex Mamo Nov 25 '22 at 14:05