【问题标题】:Update current list item更新当前列表项
【发布时间】:2019-03-21 12:41:08
【问题描述】:

我打开列表项,获取fetchData() 中的数据,然后我希望通过调用addTarget() 方法来更新当前项(namedescription)。相反,我创建了一个新的。

问:如何更新当前的?

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

【问题讨论】:

    标签: android firebase firebase-realtime-database kotlin


    【解决方案1】:

    无法使用push() 方法更新元素,因为每次调用此方法都会生成一个新的唯一键。为了执行更新,您需要知道要更新的元素的键并在参考中使用它。有关更多信息,请参阅我在以下帖子中的回答:

    【讨论】:

    • 不清楚(我进入一个特定的注释,id 我知道。如果我知道id 为什么我不能更新这个id 的数据?
    • 为了创建更新,您很可能应该使用updateChildren() 方法并传递Map 作为参数。您的代码应如下所示:databaseReference.child("-LaQq-QDnLJHWjR3ru4R").updateChildren(map)(确保具有与数据库中相同的密钥)。是这样的吗?
    • 我更新了问题。该结构在图像中可见。原来我需要在fetchData() 或之后进行更新?
    • 您仍在使用push()no 可以使用它更新元素。您是否尝试过在参考中使用硬编码密钥?有用吗?
    • 我不明白我需要在代码中的什么地方使用它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 2016-07-04
    • 2010-12-21
    • 1970-01-01
    • 2010-12-07
    相关资源
    最近更新 更多