Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
50 views
in Technique[技术] by (71.8m points)

java - Android FireStore display data

I'm looking for how I could display user info in the fields below.

Interface

I have done a lot of research and tried quite a few solutions but none of them work. I am using a FireStore database in which the user's information registers when the user creates an account.

DataBase

Here is the full code of the class in which I want to display this data. I also left in the code two tests that I carried out but which did not work, the fields are erased as if the data were going to be displayed but in the end they remain empty.

public class SettingsFragment extends Fragment {

private TextView pseudo, name, surname, mail, age;

private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;
private FirebaseUser firebaseUser;

private Button logout, deleteAccount, modifyAccount;
ProgressDialog progressDialog;

public SettingsFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_settings, container, false);

    firebaseAuth = FirebaseAuth.getInstance();
    firebaseFirestore = FirebaseFirestore.getInstance();
    firebaseUser = firebaseAuth.getCurrentUser();

    logout = (Button) view.findViewById(R.id.btLogout);
    deleteAccount = (Button) view.findViewById(R.id.btDelete);
    modifyAccount = (Button) view.findViewById(R.id.btModify);
    pseudo = (TextView) view.findViewById(R.id.tvUserPseudo);
    name = (TextView) view.findViewById(R.id.tvUserName);
    surname = (TextView) view.findViewById(R.id.tvUserSurname);
    mail = (TextView) view.findViewById(R.id.tvUserEmail);
    age = (TextView) view.findViewById(R.id.tvUserAge);

    progressDialog = new ProgressDialog(view.getContext());

    String currentuser = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DocumentReference documentReference = firebaseFirestore.collection("Users").document(currentuser);
    documentReference.addSnapshotListener(getActivity(), new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
            pseudo.setText(documentSnapshot.getString("Pseudo"));
            name.setText(documentSnapshot.getString("Name"));
            surname.setText(documentSnapshot.getString("Surname"));
            mail.setText(documentSnapshot.getString("Mail"));
            age.setText(documentSnapshot.getString("Age"));
        }
    });

    modifyAccount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getActivity().finish();
            Intent intent = new Intent(getActivity(), EditProfileActivity.class);
            getActivity().startActivity(intent);
        }
    });

    logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "Déconnexion réussie", Toast.LENGTH_SHORT).show();
            firebaseAuth.signOut();
            getActivity().finish();
            Intent intent = new Intent(getActivity(), ConnectionActivity.class);
            getActivity().startActivity(intent);
        }
    });

    deleteAccount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(view.getContext());
            dialog.setTitle("êtes-vous sur ?");
            dialog.setMessage("Supprimer ce compte sera définitif et vous ne pourrez plus revenir en arrière.");
            dialog.setPositiveButton("Supprimer", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    progressDialog.setMessage("Suppression du compte en cours");
                    progressDialog.show();
                    firebaseUser.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(getActivity(), "Compte supprimer avec succès", Toast.LENGTH_SHORT).show();
                                progressDialog.dismiss();
                                getActivity().finish();
                                Intent intent = new Intent(getActivity(), ConnectionActivity.class);
                                getActivity().startActivity(intent);
                            } else {
                                Toast.makeText(getActivity(), "Une erreur est survenue", Toast.LENGTH_SHORT).show();
                                progressDialog.dismiss();
                            }
                        }
                    });
                }
            });

            dialog.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });

            AlertDialog alertDialog = dialog.create();
            alertDialog.show();

        }
    });

    return view;

}

private void getData() {
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    String currentuser = FirebaseAuth.getInstance().getCurrentUser().getUid();

    db.collection("Users")
            .whereEqualTo("uId", currentuser)
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (DocumentSnapshot document : task.getResult()) {

                            pseudo.setText((CharSequence) document.get("Pseudo"));
                            name.setText((CharSequence) document.get("Name"));
                            surname.setText((CharSequence) document.get("Surname"));
                            mail.setText((CharSequence) document.get("Email"));
                            age.setText((CharSequence) document.get("Age"));

                        }
                    }
                }
            });
}

I think the problem is retrieving the data with the user id but despite that I can not solve the problem.

question from:https://stackoverflow.com/questions/65883526/android-firestore-display-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When using the following line of code:

db.collection("Users")
        .whereEqualTo("uId", currentuser)

You are asking Firestore to return all documents where the "uId" property holds the value of "currentuser", which actually doesn't work since in your document there is no property that holds such a value. What you should do instead, is to create a DocumentReference object:

DocumentReference currentuserRef = db.collection("Users").document(currentuser);

And get the data according to the docs:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...