Can't retrive data from firebase firestore and show it in View Text in android studio

Viewed 158

The data is already saved on the Firestore but I can't get it. Also, the created list size is still equal to null. I know I suppose to get data from existUserProfileModelList and not from existUserProfileModel but I don't know how to do it. This my code:

this my database: database1 database2

public class UserProfileActivity extends AppCompatActivity {

 // created List from User Profile Model

private List<ExistUserProfileModel> existUserProfileModelList;
// define some Edit texts
private EditText profile_phone_ET, profile_FullName_ET, profile_Address_ET;
// define FireStore  
private FirebaseFirestore firestore;

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

    profile_phone_ET = findViewById(R.id.profile_Phone);
    profile_FullName_ET = findViewById(R.id.profile_FullName);
    profile_Address_ET = findViewById(R.id.profile_Address);
    firestore = FirebaseFirestore.getInstance();
    existUserProfileModelList = new ArrayList<>();

   firestore.collection("UserData").document().collection(profilePhone)
                                .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                if (task.isSuccessful()) {
                                    for (DocumentSnapshot document : task.getResult()) {
                                        ExistUserProfileModel existUserProfileModel = document.toObject(ExistUserProfileModel.class);
                                        existUserProfileModelList.add(existUserProfileModel);

                                        profile_FullName_ET.setText(existUserProfileModel.getUserName());
                                        profile_Address_ET.setText(existUserProfileModel.getUserAddress());
                                    }
                                        Log.i("Size ", String.valueOf(existUserProfileModelList.size()));
                                }

                                else{
                                       Toast.makeText(UserProfileActivity.this, "Error" + task.getException(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
1 Answers

The problem is likely in the query you are making:

firestore.collection("UserData").document().collection(profilePhone).get()

Since you are not passing any parameters in the .document(), meaning you are not selecting any "parent" UserData documents before selecting it's subdocuments.

As you can see in this documentation you need to select which document you want to get passing it's docId. So this small change in your code will make it work as expected:

firestore.collection("UserData").document(docId).collection(profilePhone).get()

NOTE: How you are suppose to get this docId is up to the earlier stages of your app, if you do not have this available you might need to create some fields in this document to filter it out or get all documents and select the one you need.

Related