Last active
September 30, 2021 12:48
-
-
Save jrgavilanes/e9e161e7705ca74057b60e4ecc455c47 to your computer and use it in GitHub Desktop.
firebase realtime. basic access.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.codekai.firebase_curso | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.Toast | |
import com.google.firebase.database.DataSnapshot | |
import com.google.firebase.database.DatabaseError | |
import com.google.firebase.database.ValueEventListener | |
import com.google.firebase.database.ktx.database | |
import com.google.firebase.ktx.Firebase | |
import es.codekai.firebase_curso.databinding.ActivityMainBinding | |
class MainActivity : AppCompatActivity() { | |
private lateinit var mBinding: ActivityMainBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
mBinding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(mBinding.root) | |
Firebase.database.setPersistenceEnabled(true) | |
val db = Firebase.database.reference | |
val dataRef = db.child("holaFirebase").child("datax") | |
val myContext = this | |
val listener = object : ValueEventListener { | |
override fun onDataChange(snapshot: DataSnapshot) { | |
mBinding.tvTexto.text = snapshot.getValue(String::class.java) | |
} | |
override fun onCancelled(error: DatabaseError) { | |
Toast.makeText(myContext, "Error ${error.message}", Toast.LENGTH_LONG ).show() | |
} | |
} | |
dataRef.addValueEventListener(listener) | |
mBinding.btnActualizar.setOnClickListener { | |
dataRef.setValue(mBinding.etValor.text.toString()) | |
} | |
mBinding.btnActualizar.setOnLongClickListener { | |
dataRef.removeValue() | |
true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment