Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

android - Kotlin and Firebase read and write data

I have learned Kotlin for 3 weeks so I would like to read the data from my Firebase database.

This is the MainActivity.kt to write the data.

val database = Firebase.database
val latitude = latitude.text.toString().toDouble()
    val reflatitude = database.getReference("/user/time/$currenttime/latitude")

    //saved location to the Firebase Database
    reflatitude.setValue(latitude)
        .addOnSuccessListener {
            Log.d("MainActivity", "Saved the diary latitude to Firebase Database")
        }

It worked well, but when I want to call it from another activity such as MapActivity. It still have some problems.

val database = Firebase.database
val reflatitude = database.getReference("/user/time/$currenttime/latitude")
reflatitude.addValueEventListener(object :ValueEventListener){
        override fun onDataChange(dataSnapshot: DataSnapshot){
            val latitude= dataSnapshot.getValue<Double>()
        }
        override fun onCancelled(error: DatabaseError) {
            // Failed to read value
            Log.w(TAG, "Failed to read value.", error.toException())
        }
    }

My Ref does not work in reading the data.

The errors I get:

  • Expecting a class body

  • Too many arguments for @NonNull public open fun addValueEventListener(@NonNull p0: ValueEventListener): ValueEventListener defined in com.google.firebase.database.DatabaseReference

  • Modifier 'override' is not applicable to 'local function'

  • No type arguments expected for fun getValue(): Any?

  • Modifier 'override' is not applicable to 'local function'

  • Cannot access 'TAG': it is invisible (private in a supertype) in 'AppCompatActivity'

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...