-2

having issues displaying to a textview from firestore db due to asynchronous process

I have tried to notify the textviews but they are not bound by any thread apparently

The displayed data can be logged to console but is not displayed to the textview

I would have used notifyDataSetChanged() but it only exists for the recyclerview to update its content

If notifyall() or notify() is used there is a thread error showing the elements were bound the thread

package com.example.schedule;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

public class ScheduleDetails extends AppCompatActivity implements View.OnClickListener {

    Button backbutton;
    TextView schedName;
    TextView textDesc;
    TextView textDte;
    TextView textTime;
    private FirebaseAuth mAuth;

    @Override
    protected void onStart() {
        super.onStart();
        mAuth = FirebaseAuth.getInstance();
        final FirebaseUser currentUser = mAuth.getCurrentUser();
        final FirebaseFirestore db = FirebaseFirestore.getInstance();


        schedName = findViewById(R.id.schedName);
        schedName.setText(getIntent().getStringExtra("text"));
        final String sched = getIntent().getStringExtra("text");

        db.collection("Schedules")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                if (document.get("Owner").toString().equals(currentUser.getUid())) {
                                    if(document.get("Schedule").toString().equals(sched)){
                                        System.out.println(document.get("Schedule Description").toString());
                                        System.out.println(document.get("Schedule Description").toString());
                                        System.out.println(document.get("Schedule Description").toString());
                                        System.out.println(document.get("Schedule Description").toString());
                                        System.out.println(document.get("Schedule Description").toString());
                                        System.out.println(document.get("Schedule Description").toString());
                                        System.out.println(document.get("Schedule Description").toString());
                                        System.out.println(document.get("Schedule Description").toString());
                                        textDesc.setText(document.get("Schedule Description").toString());
                                        //textDesc.notify();
                                        //Log.d("",document.get("Schedule Description").toString());
                                        //textDte.notify();
                                        textDte.setText(document.get("Schedule Date").toString());
                                        //textTime.notify();
                                        textTime.setText(document.get("Schedule Time").toString());
                                    }
                                }
                            }
                        }else {
                        Log.w("My Contenet", "Error getting your schedules.", task.getException());
                    }
                }
    });


}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schedule_details);

        backbutton = findViewById(R.id.buttonback);
        backbutton.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, Navigator.class);
        startActivity(intent);
    }
}

Expect textview to display details of the schedule

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Steve Nginyo
  • 303
  • 3
  • 12
  • 3
    Please only use the `android-studio` tag for questions about the Android Studio IDE itself. For questions about Android programming in general, use the `android` tag. – Frank van Puffelen Aug 02 '19 at 08:06
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Aug 02 '19 at 08:57
  • check this out, if it helps you out please consider upvoting it https://stackoverflow.com/questions/57330766/how-to-get-data-from-any-asynchronous-operation-in-android/57330767#57330767 – a_local_nobody Aug 02 '19 at 18:40

1 Answers1

2

Since firestore returns data async what i recently did is: Declare monitor as new object

 private static Object monitor = new Object();

asked main thread to wait where i wanted it to for Firestore's data return

synchronized(monitor) {
                try {
                    monitor.wait();
                } catch(InterruptedException e) {

                }
            }

And in the firestore's query onComplete since i had to wait for many tasks to complete i used

Tasks.whenAllSuccess(task).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
                                        @Override
                                        public void onSuccess(List<Object> list) {

                                                synchronized (monitor) {
                                                    monitor.notifyAll();
                                                }

                                        }

                                    });
Gerasimos
  • 83
  • 8
  • have a look at this, i made this answer specifically for these types of problems, consider upvoting it if you agree with it please : https://stackoverflow.com/questions/57330766/how-to-get-data-from-any-asynchronous-operation-in-android/57330767#57330767 – a_local_nobody Aug 02 '19 at 18:40
  • maybe I understood this wrong but the monitor method seems to create a wait but does not resume – Steve Nginyo Aug 03 '19 at 21:02
  • hi @a_local_nobody I tried to use the method you referred me to but I do not seem to understand what value to the passin the foo(Callback callback){ method } should I create a new callback when calling it foo(callback); – Steve Nginyo Aug 03 '19 at 21:03
  • follow the steps in the guide, if you get stuck let me know and i'll help you out. also please consider upvoting the question and answer for others in future :D @SteveNginyo – a_local_nobody Aug 03 '19 at 21:24
  • also,Gerasimos, if you agreed with what I did, also please consider upvoting my question and answer :D A lot of people are struggling with that problem, would help a lot – a_local_nobody Aug 03 '19 at 21:26
  • I got stuck while trying to call the method... foo(callback variable which I do not have); – Steve Nginyo Aug 03 '19 at 21:30