无法从文档参考

时间:2018-05-17 18:10:26

标签: android firebase google-cloud-firestore

enter image description here

这是我的代码段:

我想从Document NewDocument获取积分

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
String data;

FirebaseFirestore firebaseFirestore=FirebaseFirestore.getInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Adder("Hello");

        TextView txt=findViewById(R.id.textView);
        txt.setText(Gettingdata());

}

private String Gettingdata() {
    firebaseFirestore.collection("Users").document("NewDocument")
            .get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if (documentSnapshot!=null) {
                        data = documentSnapshot.toString().getString("email");

                    } else {
                        data="else case";
                        Log.d("Tag           ", "else case");
                        // Toast.makeText(this, "Document Does Not exists", Toast.LENGTH_SHORT).show();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            // Toast.makeText(, "", Toast.LENGTH_SHORT).show();
            // Log.d("Tag",e.toString());
            data="Failed";
        }
    });
    return data;
}
}

但返回的数据为空

请帮我解决这个问题 我还需要firestore中的数字。我尝试了很多,但返回总是为空

1 个答案:

答案 0 :(得分:0)

您可以使用Alex Mamo建议或直接在回复中设置视图:

TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Adder("Hello");

      myTextView =findViewById(R.id.textView);
      Gettingdata();


}

private void Gettingdata() {
    firebaseFirestore.collection("Users").document("NewDocument")
            .get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if (documentSnapshot!=null) {
                        data = documentSnapshot.toString().getString("email");
                    myTextView.setText(data);

                    } else {
                        data="else case";
                        Log.d("Tag           ", "else case");
                        // Toast.makeText(this, "Document Does Not exists", Toast.LENGTH_SHORT).show();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            // Toast.makeText(, "", Toast.LENGTH_SHORT).show();
            // Log.d("Tag",e.toString());
            data="Failed";
        }
    });
}
}

Firebase呼叫是异步的,因此您无法在方法中返回数据,因为它没有到达&#34;然而。您可以对onResponse内部的到达作出反应,或者按照Alex的建议创建一个回调结构(这是一种更清洁,更有条理的方式)。