Firebase replaces information instead of adding

Asked

Viewed 38 times

-2

I’m making a task app with Kotlin and firebase, but every time I try to add information to my database, it doesn’t add it but replaces the previous one.

This is my code:

class AddActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_add)
}
fun function4(view: View){
    val uid = FirebaseAuth.getInstance().uid ?: ""
    val ref = FirebaseDatabase.getInstance().getReference("users/$uid-posts")
    val user2 = AddActivity.User2(editText3.text.toString(), editText4.text.toString())
    ref.setValue(user2)
    val intent2 = Intent(this, TasksActivity::class.java)
    intent2.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
    startActivity(intent2)
}
class User2(val title: String, val content: String)}

1 answer

1


Consider only the title and the code presented, more specifically users/$uid-posts, I believe your goal is to have a list of "posts" and are using the method incorrectly setValue for this. In this case, the correct method is the push, according to the documentation, this should be the method used to "create new elements given a specified location":

https://firebase.google.com/docs/reference/android/google/firebase/database/DatabaseReference.html#push()

Create a Reference to an auto-generated Child Location.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.