不重新启动应用程序就无法显示更新的Firebase数据库吗?

时间:2019-03-20 16:08:26

标签: android firebase firebase-realtime-database kotlin

我正在使用Kotlin和Android Studio构建一个简单的ToDo应用,并遵循以下指南:http://appsdeveloperblog.com/todo-list-app-kotlin-firebase/

它可以正常工作,但是如果我将一个条目添加到列表中,除非关闭应用程序并重新启动,否则看不到它。

这是设置:

class MainActivity : AppCompatActivity(), ItemRowListener { //ItemRowListener points to another file that tracks the ids of the items

lateinit var mDatabase: DatabaseReference        //some global variables
var toDoItemList: MutableList<ToDoItem>? = null
lateinit var adapter: ToDoItemAdapter
private var listViewItems: ListView? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    //reference for FAB
    val fab = findViewById<View>(R.id.fab) as FloatingActionButton //basic activity
    listViewItems = findViewById<View>(R.id.items_list) as ListView
    //Adding click listener for FAB
    fab.setOnClickListener {
        //Show Dialog here to add new Item
        addNewItemDialog()
    }
    mDatabase = FirebaseDatabase.getInstance().reference
    toDoItemList = mutableListOf()
    adapter = ToDoItemAdapter(this, toDoItemList!!)
    listViewItems!!.setAdapter(adapter)
    mDatabase.orderByKey().addListenerForSingleValueEvent(itemListener) //This is the line I refer to

}

我们正在看这里的最后一行(addListenerForSingleValueEvent)。可以将其更改为addValueEventListener,并确实将添加的项目立即推送到屏幕上,但同时也推送其自身的第二个副本,而没有任何数据。如果要删除这些项目,它将导致应用程序崩溃,直到您清除数据库并由于删除这些对象而导致空指针错误而重新安装该应用程序为止。

这是警报窗口:

private fun addNewItemDialog() {
    val alert = AlertDialog.Builder(this)
    val itemEditText = EditText(this)
    alert.setMessage("Add New Item")
    alert.setTitle("Enter To Do Item Text")
    alert.setView(itemEditText)
    alert.setPositiveButton("Submit") { dialog, positiveButton -> //positiveButton is never used
        val todoItem = ToDoItem.create()
        todoItem.itemText = itemEditText.text.toString()
        todoItem.done = false
        //We first make a push so that a new item is made with a unique ID
        val newItem = mDatabase.child(Constants.FIREBASE_ITEM).push()
        todoItem.objectId = newItem.key
        //then, we used the reference to set the value on that ID
        newItem.setValue(todoItem)
        dialog.dismiss()
        Toast.makeText(this, "Item saved with ID " + todoItem.objectId, Toast.LENGTH_SHORT).show()
    }
    alert.show() //This is basically a popup which asks you what you want to add to the list
}

以及将项目添加到数据库的函数:

private var itemListener: ValueEventListener = object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        // Get Post object and use the values to update the UI
        addDataToList(dataSnapshot)
    }
}
private fun addDataToList(dataSnapshot: DataSnapshot) {
    val items = dataSnapshot.children.iterator()
    //Check if current database contains any collection
    if (items.hasNext()) {
        val toDoListindex = items.next()
        val itemsIterator = toDoListindex.children.iterator()

        //check if the collection has any to do items or not
        while (itemsIterator.hasNext()) {
            //get current item
            val currentItem = itemsIterator.next()
            val todoItem = ToDoItem.create()
            //get current data in a map
            val map = currentItem.getValue() as HashMap<String, Any>
            //key will return Firebase ID
            todoItem.objectId = currentItem.key
            todoItem.done = map.get("done") as Boolean?
            todoItem.itemText = map.get("itemText") as String?
            toDoItemList!!.add(todoItem);
        }
    }
    //alert adapter that has changed
    adapter.notifyDataSetChanged()
}

删除功能也可以使用,但是直到关闭并重新启动应用程序,我才能看到任何更改:

override fun onItemDelete(itemObjectId: String) {
    //get child reference in database via the ObjectID
    val itemReference = mDatabase.child(Constants.FIREBASE_ITEM).child(itemObjectId)
    //deletion can be done via removeValue() method
    Toast.makeText(this, "Item deleted", Toast.LENGTH_SHORT).show()
    itemReference.removeValue()

}

感谢任何建议或技巧,也欢迎任何技巧。

该应用程序可以按预期运行,但我在刷新时遇到了一些问题。

0 个答案:

没有答案