I tried to add some data to the Firebase Realtime Database. It works. Also, I retrieved those data successfully. But now I need to add the current date with those data as a new data value, then retrieve that data with other data. I know, that I have to update my data class with a new data type, but the problem is how I generate the current data and how to save it in the database (data type). I attached my data adding code, retrieving code below.
Data adding code:
val notificstionRef = rootRef.child("Notifications")
val eventListener1: ValueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val recharge = amountText.getText().toString()
val title = "Recharge Amount"
val notifications = Notifications(sessionId,title,recharge)
ref1.child(sessionId).setValue(notifications).addOnSuccessListener {
} .addOnFailureListener{
}
}
override fun onCancelled(databaseError: DatabaseError) {
}
}
notificstionRef.addListenerForSingleValueEvent(eventListener1)
data retrieving code
val rootRef = FirebaseDatabase.getInstance().reference
val userRef = rootRef.child("Notifications").child(sessionId)
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val title = dataSnapshot.child("title").getValue(String::class.java)
val body = dataSnapshot.child("body").getValue(String::class.java)
title1Notification.setText(title)
body1Notification.setText(body)
}
override fun onCancelled(error: DatabaseError) {
Log.d("TAG", error.getMessage())
}
}
userRef.addListenerForSingleValueEvent(valueEventListener)
data class
data class Notifications(val emailA: String, val title: String, val body: String)
Can you please help me to solve this problem? I'm new to this field (Student)
Edited
dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'androidx.annotation:annotation:1.2.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.jakewharton.timber:timber:4.7.1'
implementation 'com.google.android.gms:play-services-maps:17.0.1'
implementation ('com.google.zxing:core:3.3.3')
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'com.google.firebase:firebase-database-ktx'
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation platform('com.google.firebase:firebase-bom:28.4.1')
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
}
CodePudding user response:
If you want to add a Timestamp type field inside an object of type Notifications
, then please define the following field in your class:
data class Notifications(
val emailA: String? = null,
val title: String? = null,
val body: String? = null,
val timestamp: Map<String, String>? = null
)
As you can see, the new field is of type Map<String, String>. Also remember, when you set the TIMESTAMP, you set it as a Map, but when you retrieve it, you retrieve it as a Long. Now, when you create an object, pass for the fourth argument ServerValue.TIMESTAMP)
.
Alternatively, you can create a Map and use:
map.put("timestamp", ServerValue.TIMESTAMP);