1

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'
}

Error Screenshot

  • What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Oct 02 '21 at 11:29
  • @AlexMamo no errors. this's the code I use to add data and retrieve data. I need to add `current date` in this code but I don't know how to add it. So For easy I attched my code. So you can clearly understand What I need ? –  Oct 02 '21 at 11:32
  • So you only want to add a Timestamp filed to the Realtime Database? – Alex Mamo Oct 02 '21 at 11:43
  • @AlexMamo Yes, with all other details –  Oct 02 '21 at 11:55
  • In that case, please show us the content of your `Notifications` class. – Alex Mamo Oct 02 '21 at 11:56

1 Answers1

1

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);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • when I add `@ServerTimestamp` It marked red and have to create `annotation class ServerTimestamp` this on data class. but still not work. –  Oct 02 '21 at 13:41
  • Are you sure you have the correct dependencies? Please edit your question and add them. Eventually, a screenshot of the error might also help. – Alex Mamo Oct 02 '21 at 14:01
  • not getting error, but not add data also have to put `annotation class` . **Project dependencies** updated. –  Oct 02 '21 at 14:19
  • There is no need to create any annotation class. Show me the error in a screenshot. – Alex Mamo Oct 02 '21 at 14:21
  • updated. but it doesn't show exactly –  Oct 02 '21 at 14:30
  • I need to see the error in your class, not in the logcat. – Alex Mamo Oct 02 '21 at 14:33
  • updated error Screenshot –  Oct 02 '21 at 14:41
  • I got it now. I'm so sorry for providing the wrong answer. I just updated it with the correct data. Give it a try and tell me if it works. – Alex Mamo Oct 02 '21 at 14:50
  • If you don't mind can I ask one thing? Is there any way to convert timestamp to `DD-MM-YYYY` format. when retrieving? –  Oct 02 '21 at 15:21
  • Sure. You can [format](https://stackoverflow.com/questions/4772425/change-date-format-in-a-java-string) it. – Alex Mamo Oct 02 '21 at 16:27
  • 1
    okay, thanks a lot –  Oct 03 '21 at 10:13