在Firebase Android Studio中输入数据时出现复制错误

时间:2019-01-23 18:11:00

标签: java android kotlin

将数据添加到Firebase时出现问题,而不是将新数据添加到数据库中,而是将已有的数据重新存储到数据库中,然后重复自身,然后添加新数据。

例如,如果我有标题“ Red Dead”和“ GTA”,而我​​想添加“ COD”。而不是像“ Red Dead”,“ GTA”和“ COD”那样的数据库,而是“ Red Dead”,“ GTA”,“ Red Dead”,“ GTA”,然后是“ COD”。

有关如何解决此问题的任何想法,请??

package com.example.gearoidodonovan.books

import android.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.renderscript.Sampler
import android.widget.Toast
import com.google.firebase.database.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.add_note.view.*
import java.text.SimpleDateFormat
import java.util.*


class MainActivity : AppCompatActivity() {

var mRef:DatabaseReference? = null
var mNoteList:ArrayList<Note>?= null





override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    val database = FirebaseDatabase.getInstance()
    mRef = database.getReference("Notes")
    mNoteList = ArrayList()



    add_new_note.setOnClickListener {

        showDialogAddNote()


    }
}

override fun onStart(){
    super.onStart()
    mRef?.addValueEventListener(object : ValueEventListener{
        override fun onCancelled(pO: DatabaseError) {
        }

        override fun onDataChange(p0: DataSnapshot) {
            for (n in p0!!.children) {
                var note = n.getValue(Note::class.java)
                mNoteList?.add(note!!)

            }

            val noteAdapter = NoteAdapter(applicationContext, mNoteList!!)
            note_list_view.adapter = noteAdapter

        }

    })

}


fun showDialogAddNote() {
    val alertBuilder = AlertDialog.Builder(this)

    val view = layoutInflater.inflate(R.layout.add_note, null)

    alertBuilder.setView(view)

    val alertDialog = alertBuilder.create()
    alertDialog.show()


    view.btnSaveNote.setOnClickListener {
        val title = view.etTitle.text.toString()
        val note = view.etNote.text.toString()

        if (title.isNotEmpty() && note.isNotEmpty()) {
            var id = mRef!!.push().key!! //reference 1

            var myNote = Note(id, title, note, getCurrentDate())
            mRef!!.child(id).setValue(myNote)
            alertDialog.dismiss()


        } else {
            Toast.makeText(this, "Empty", Toast.LENGTH_LONG).show()
        }


    }

}

    fun getCurrentDate(): String{
        val calendar = Calendar.getInstance()
        val mdformat = SimpleDateFormat("EEEE hh:mm a ")
        val strDate = mdformat.format(calendar.time)
        return strDate


}
}

这是我的笔记课

package com.example.gearoidodonovan.books


import com.google.firebase.database.Exclude
import com.google.firebase.database.ServerValue



class Note {
var id:String?= null
var title:String? = null
var note:String?= null
var timestamp: String? = null

constructor(){

}

constructor(id: String, title: String, note: String, timestamp: String){
    this.id = id
    this.title = title
    this.note = note
    this.timestamp = timestamp
}







}

这是我的适配器类

package com.example.gearoidodonovan.books

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.note_layout.view.*
import kotlin.collections.ArrayList

class NoteAdapter(context: Context, noteList: ArrayList<Note>)
: ArrayAdapter<Note>(context, 0, noteList) {


override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
   val view = LayoutInflater.from(context).inflate(R.layout.note_layout,parent ,false)

    val note:Note = getItem(position)
    view.titleText.text = note.title
    view.timeText.text = note.timestamp.toString()
    return view


}



}

2 个答案:

答案 0 :(得分:0)

使用childEventListener代替valueEventListener

答案 1 :(得分:0)

如果您要从Firebase列出数据时发生这种情况,请在添加新数据之前初始化.sticky { position: sticky; } .fixed { position: fixed; } 。因为ArrayList方法从Firebase获取所有数据并在ListView中显示。
因此,您必须在onDataChangeonDataChange

之间进行初始化