0

This is my code to get the variable from firebase, it still return right when i log it

holder.setItemClickListener(new TaskViewHolder.ItemClickListener() {
    @Override
    public void onClick(View view, int position, boolean isLongClick) {
        Intent intent = new Intent(context, TaskDetail.class);
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference databaseReference = database.getReference("Users");
        databaseReference.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .child("task")
                .orderByChild("id")
                .equalTo(taskList.get(position).getId())
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        for (DataSnapshot ds: snapshot.getChildren()){
                            TaskModel task = ds.getValue(TaskModel.class);
                            Log.d("TEST", "value: "+ task.getTask());
                            intent.putExtra("task", task.getTask().toString());
                            intent.putExtra("id", task.getId());
                            intent.putExtra("impo", task.getImpo());
                            intent.putExtra("done", task.getDone());
                            Log.d("TEST", "value: "+ ds.child("task").getValue());

                        }
                    }

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

                    }
                });
        context.startActivity(intent);
    }
});
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_task_detail);

Intent intent = getIntent();
String task = intent.getStringExtra("task");
int id = intent.getIntExtra(("id"),0);
int done = intent.getIntExtra(("done"),0);
int impo = intent.getIntExtra(("impo"),0);

btnSave = findViewById(R.id.btnSaveEdit);
txtTask = findViewById(R.id.txtTask);
btnSave.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("TEST-ID", "task"+task);
    }
});

enter image description here

putExtra the variable to another activity

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
cx330
  • 1
  • 3
  • 1
    Does this answer your question? [Why does my function that calls an API or launches a coroutine return an empty or null value?](https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-or-launches-a-coroutine-return-an-empty-o/70178210#70178210) - the code inside `onDataChange` is an asynchronous callback, it doesn't run in the order written. It runs later when the data is changed, which is *always* going to happen after your call to `startActivity` – Tyler V Jul 31 '23 at 03:17

1 Answers1

1

Your context.startActivity(intent) happens before the onDataChange is ever called. Any code that needs the data from the database has to be inside the onDataChange method or it will execute at the wrong time.

So:

databaseReference.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
    .child("task")
    .orderByChild("id")
    .equalTo(taskList.get(position).getId())
    .addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot ds: snapshot.getChildren()){
                TaskModel task = ds.getValue(TaskModel.class);
                Log.d("TEST", "value: "+ task.getTask());
                intent.putExtra("task", task.getTask().toString());
                intent.putExtra("id", task.getId());
                intent.putExtra("impo", task.getImpo());
                intent.putExtra("done", task.getDone());
                Log.d("TEST", "value: "+ ds.child("task").getValue());

            }

            context.startActivity(intent); //  move this here
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            throw error.toException(); //  stop ignoring possible errors
        }
    });

This is an incredibly common source of confusion for developers who are new to dealing with asynchronous API, so I recommend checking out:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807