Reputation: 5260
I have some list of notes that I have stored in FirebaseRealtime Database and displayed in recyclerView.
By clicking on an item in the list, I open a fragment passing it a "guid". But since there are still name, description fields in this element, I want to display them in a fragment.
Q: how can I get the rest of the data from FirebaseRealtime Databse knowing the id?
class TargetEditFragment : Fragment() {
private var nameEditText: TextInputEditText? = null
private var descriptionEditText: TextInputEditText? = null
private var button: Button? = null
private var databaseReference: DatabaseReference? = null
private var presenter = TargetEditPresenter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.getString("guid", "")
Log.d("some", "onCreate ${arguments?.getString("guid", "")}")
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_target_add, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
databaseReference = FirebaseDatabase.getInstance().getReference("targets")
setupViews()
fetchData()
}
private fun setupViews() {
nameEditText = view?.findViewById(R.id.nameEditText)
descriptionEditText = view?.findViewById(R.id.descriptionEditText)
button = view?.findViewById(R.id.addNote)
button?.setOnClickListener { addTarget() }
}
private fun addTarget() {
val name = nameEditText?.text.toString().trim()
val description = descriptionEditText?.text.toString().trim()
if (!TextUtils.isEmpty(name)) {
val id: String = databaseReference?.push()?.key.toString()
val target = Target(guid = id, name = name, description = description)
databaseReference?.child(id)?.setValue(target)
} else Log.d("some", "Enter a name")
}
//Here I want to get the data name and decription
private fun fetchData() {
// nameEditText?.text = Editable.Factory.getInstance().newEditable(target?.name ?: "name")
// descriptionEditText?.text = Editable.Factory.getInstance().newEditable(target?.description ?: "description")
}
companion object {
fun newInstance(guid: String): TargetEditFragment =
TargetEditFragment().apply {
arguments = Bundle().apply { putString("guid", guid) }
Log.d("some", "argumeeents $arguments")
}
}
}
Also my Target class:
data class Target(val guid: String = "",
val name: String = "",
val description: String= "")
Upvotes: 0
Views: 374
Reputation: 9476
If you know the guid then you can probably do something like
fun fetchData(guid: String) {
// Attach a listener to read the data at the target id
databaseReference?.child(guid)?.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
// handle error
}
override fun onDataChange(dataSnapshot: DataSnapshot) {
// data here
val data = dataSnapshot.value as HashMap<String, String>
val desc = data["description"]
val name = data["name"]
}
})
}
the dataSnapshot will be a hashmap of String -> String with the name and description in it
Upvotes: 3