Home > database >  I don't want the data registration in Firebase to be numbered Android
I don't want the data registration in Firebase to be numbered Android

Time:10-07

I do not want numbering to appear, I want the time to appear as written in the code, so what is the reason for such a problem to appear?

deviceModels.add(new DeviceModel(newDeviceName, newDeviceWat, newDeviceUse, currentDateAndTime));

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    DatabaseReference databaseReference = firebaseDatabase.getReference();
    databaseReference.child("DeviceInfo").child(currentUser).child(currentDateAndTime).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            // data base reference will sends data to firebase.
            databaseReference.setValue(deviceModels);
            Toast.makeText(MainActivity.this, "تم أضافة الجهاز", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
            finish();
            startActivity(getIntent());
        }

enter image description here

CodePudding user response:

It looks like your deviceModels is an array or a List, and this is how the Firebase Realtime Database persists arrays/lists. There is no way to configure it to do it differently, but you can of course use a different data type in your code.

More idiomatic in Firebase is to use the push() method when adding items to a list on the database. To learn more about why that is, have a look at Best Practices: Arrays in Firebase.

In your case you'd write the data for each individual DeviceModel:

databaseReference.push().setValue(new DeviceModel(newDeviceName, newDeviceWat, newDeviceUse, currentDateAndTime));

This will then end up in the database as:

DeviceInfo: {
  "IN0r....T2": {
    "-N.....": {
      deviceId: ...,
      deviceName: ...
    }
  }
}

And that entire list would then map back to a Map<String, DeviceModel> in your Java code.

  • Related