0

I am building a task-list management application for Android using Java and Firebase Real-time database. I am currently trying to integrate adding a task to the database and viewing this the task data/name on the application. I pretty much have this however, the code I have created points to one record of a task

e.g. (ref.child("Task 02").setValue(task);

This of course overwrites the task 02 record, I want to be able to increment the task number by '1' every time a task is added to the database. Can somebody please provide some assistance?

I have tried completely different approaches to integrating both interfaces together via YouTube tutorials none of them work as well as the code I currently have now and I have now hit a brick wall. This system is for a final project in Computer Science as part of my dissertation.

database = FirebaseDatabase.getInstance(); ref = database.getReference("Task"); task = new Task();

}

private void getValues() {

    task.setDate(dateField.getText().toString());
    task.setAssignedTo(spinnerAssign.getSelectedItem().toString());
    task.setDescription(description.getText().toString());
    task.setName(newTask.getText().toString());


}

public void  addTaskBtn (View v) {
    Intent intent = new Intent(Add_Task.this , Retrieve_Tasks.class);
    startActivity(intent);

    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            getValues();
            ref.child("Task 02").setValue(task);


            Toast.makeText(Add_Task.this, "Task added....", Toast.LENGTH_LONG).show();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });


}

The actual result - Task '02' is overwritten by Firebase database. Expected result - The child ref should increment by one every time a task is added.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Alex
  • 183
  • 16
  • Firebase doesn't have built-in support for such sequential auto-incremental keys, as 1) they don't scale well across massive numbers of users, 2) they don't work when you're offline. For more on this, and a sample of how to implement sequential IDs, see: https://stackoverflow.com/questions/39519021/how-to-create-auto-incremented-key-in-firebase. If you've tried the approach set there and can't make it work, edit your question to include the [minimal, standalone code that reproduces where you got stuck](http://stackoverflow.com/help/mcve). – Frank van Puffelen Jan 20 '19 at 15:50
  • The answer I linked also contains a link that shows how to implement sequential auto-increment in JavaScript. The Firebase SDKs are fairly similar across platforms, so you could try to implement it in Java if you really need it. If you get stuck implementing that i Java, as said, post the [minimum code that reproduces where you got stuck](http://stackoverflow.com/help/mcve). – Frank van Puffelen Jan 20 '19 at 17:11
  • Hi @FrankvanPuffelen , thanks for pointing out that you've already answered this. I believe I may already have tried the push method in my initial attempt. 'mDatabase.push().setValue(taskData).addOnCompleteListener(new OnCompleteListener()' I originally had this but ran into problems with then viewing the information via the app. I may raise this in another question. – Alex Jan 20 '19 at 17:11

0 Answers0