Morozov
Morozov

Reputation: 5250

Update current list item

I open the list item, get the data in fetchData(), then I expect that by calling the addTarget() method I will update the current item(name and description). Instead, I create a new one.

Q: How can I update the current one?

enter image description here

class TargetEditFragment : Fragment() {

private var nameEditText: TextInputEditText? = null
private var descriptionEditText: TextInputEditText? = null
private var button: Button? = null
private var databaseReference: DatabaseReference? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.getString(KEY_TARGET_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(guid = arguments?.getString(KEY_TARGET_GUID, "") ?: "")
}

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")
}

private fun fetchData(guid: String) {
    // Attach a listener to read the data at the target id
    databaseReference?.child(guid)?.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {
            val data = dataSnapshot.value as HashMap<String, String>
            val name = data["name"] ?: ""
            val description = data["description"] ?: ""

            if (name.isEmpty()) Log.d("some", "nameIsEmpty")
            else {
                updateViewsContent(name = name, description = description)
            }
        }

        override fun onCancelled(p0: DatabaseError) {
            Log.d("some", "onCancelled")
        }
    })
}

private fun updateViewsContent(name: String?, description: String?) {
    nameEditText?.text = Editable.Factory.getInstance().newEditable(name)
    descriptionEditText?.text = Editable.Factory.getInstance().newEditable(description)
}

companion object {

    fun newInstance(guid: String): TargetEditFragment =
        TargetEditFragment().apply {
            arguments = Bundle().apply { putString(KEY_TARGET_GUID, guid) }
        }
}
}

Upvotes: 1

Views: 59

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

There is no way to update an element using the push() method because everytime you call this method a new unique key is generated. In order to perform an update you need to know the key of the element you want to update and use it in your reference. For more informations, please see my answer from the following post:

Upvotes: 1

Related Questions