我如何才能从另一个活动的Firestore获取存储的数据

时间:2019-11-22 03:07:01

标签: android firebase google-cloud-firestore

这是我的用户的Editprofile,这是我存储数据的位置。

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

    b1 =(Button)findViewById(R.id.pindot);
    e1 = (EditText)findViewById(R.id.lagay);
    e2 = (EditText)findViewById(R.id.lagay2);
    e3 = (EditText)findViewById(R.id.lagay3);


    mAuth = FirebaseAuth.getInstance();
    user = mAuth.getCurrentUser();
    t1 = (TextView)findViewById(R.id.textView19);
    t1.setText(user.getEmail());
    ActionBar ac = getSupportActionBar();
    ac.hide();

    progressDialog = new ProgressDialog(EditProfile.this);
    progressDialog.setTitle("OSIX");
    progressDialog.setMessage("Saving Profile, Please Wait.....");



    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String firstName = e1.getText().toString();
            final String lastName = e2.getText().toString();
            final String age = e3.getText().toString();
            final String uid= mAuth.getCurrentUser().getUid();

                if (firstName.isEmpty()){
                    e1.setError("Field is Empty");
                    e1.requestFocus();
                }else if (lastName.isEmpty()){
                    e2.setError("Field is Empty");
                    e2.requestFocus();
                }else if (age.isEmpty()){
                    e3.setError("Field is Empty");
                    e3.requestFocus();
                }else if (! (firstName.isEmpty() && lastName.isEmpty() && age.isEmpty())){
                    progressDialog.show();
                    profile edit = new profile(firstName,lastName,age);
                    db2.collection("Profile").document(user.getEmail()).set(edit)
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {

                                    if (task.isSuccessful()) {
                                       startActivity(new Intent(EditProfile.this,Navbar.class));
                                        progressDialog.dismiss();
                                    }
                                }
                            });
                }
        }
    });
}}

所以我的问题是我希望它从个人档案片段中显示出来,以便显示用户信息。我使用firestore并添加了一个对象进行收集。现在,我记录了数据,如何在textview中显示它?我将使用哪种方法?

1 个答案:

答案 0 :(得分:0)

使用此代码通过电子邮件地址获取用户数据并将其显示在textview中 替换xyx

    DocumentReference docRef = db.collection("Profile").document("xyz@abc.com");
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() 
    {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    Log.d(TAG, "DocumentSnapshot data: " + document.getData());
                    profile edit = document.toObject(profile.class);
                    textview.setText(profile.getfirstname());
                } else {
                    Log.d(TAG, "No such document");
                }
            } else {
                Log.d(TAG, "get failed with ", task.getException());
            }
        }
    });