0

I'm new to firebase this is my first time using it.

I'm trying to get SkinType field of each user from users collections and use it out side method to run the query in specific collection and it look like the data that has been retrieved is null

public class MorningRoutine extends AppCompatActivity {
    private static final String TAG = "100";
    private RecyclerView recyclerView;

    private FirestoreRecyclerAdapter adapter;
    private User userSkinType = new User();
    String SkinType1;
    String myskintype1;
    FirebaseFirestore db1 = FirebaseFirestore.getInstance();
    FirebaseFirestore db2 = FirebaseFirestore.getInstance();
    private FirebaseAuth mAuth;
    private FirebaseFirestore fstore;
    private String UserId;

    Query query;

    String skintypename;
    DocumentReference docRef;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.morningroutine);
        fstore = FirebaseFirestore.getInstance();
        mAuth = FirebaseAuth.getInstance();
        UserId = mAuth.getCurrentUser().getUid();
        recyclerView = findViewById(R.id.firestore_list);

        docRef = fstore.collection("users").document(UserId);

        readData(new FirestoreCallback() {
            @Override
            public void onCallback(String getSkinType) {
                Log.d(TAG, getSkinType);
            }
        });


        query = fstore.collection(skintypename + "_morning_routine");

// recycler option

        FirestoreRecyclerOptions<morningRoutineStorage> options = new FirestoreRecyclerOptions.Builder<morningRoutineStorage>()
                .setQuery(query, morningRoutineStorage.class)
                .build();


        // MY Adapter
        adapter = new FirestoreRecyclerAdapter<morningRoutineStorage, myViewHolder>(options) {
            @NonNull
            @Override
            public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_items, parent, false);

                return new myViewHolder(view);
            }

            public void deleteItems(int position) {
                getSnapshots().getSnapshot(position).getReference().delete();
            }

            @Override
            protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull morningRoutineStorage storage) {
                holder.Type.setText(storage.getType());
                holder.name.setText(storage.getName());


                Glide.with(holder.image.getContext())
                        .load(storage.image)
                        .placeholder(com.firebase.ui.database.R.drawable.common_google_signin_btn_icon_dark)
                        .circleCrop()
                        .error(com.firebase.ui.database.R.drawable.common_google_signin_btn_icon_dark_normal)
                        .into(holder.image);


                holder.delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        deleteItems(holder.getAbsoluteAdapterPosition());

                    }
                });
            }
        };

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);


    }

    private void readData(FirestoreCallback firestoreCallback) {
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        skintypename = document.get("SkinType").toString();

                    }

                    firestoreCallback.onCallback(skintypename);

                } else {
                    Log.d(TAG, "get failed with ", task.getException());
                }
            }
        });
    }

    private interface FirestoreCallback {

        void onCallback(String getSkinType);
    }

}

I tried so hard so many techniques and it's didn't work I'm using onCallback interface

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ran Bw
  • 1
  • 1
  • The Firestore API is asynchronous, and you have to wait for the results of the query before you try to populate an adapter. – Doug Stevenson May 19 '22 at 18:19
  • There is no way you can do that. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Cloud Firestore using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953). – Alex Mamo May 20 '22 at 07:47

0 Answers0