2

enter image description here

This is the my code snippet:

I want to get the points from 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;
}
}

but the data returned is null

Please help me to solve this I also need the point which is in number in firestore .I tried a lot but the return is always a null

Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
amaljose
  • 31
  • 4
  • Possible duplicate of [Firestore - object with inner Object](https://stackoverflow.com/questions/48499310/firestore-object-with-inner-object) – Alex Mamo May 17 '18 at 18:16
  • 1
    Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo May 17 '18 at 18:17

1 Answers1

0

You can use Alex Mamo suggestion or set the view directly in the response:

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 calls are async, so you can't return the data in your method because it hasn't "arrived" yet. You can react to the arrival inside the onResponse or create a callback structure as suggested by Alex (which is a more cleaner and more organized way of doing this).

Levi Moreira
  • 11,917
  • 4
  • 32
  • 46