Doucment.getString not giving an output (Firestore)










1















enter image description here



I just wrote a basic code to get multiple documents from Firestore database. The task is getting successful and you could see the document.getId() & document.getData() value in Log, but after that when I use document.getString(), it gives a blank output. I have been trying to find my error but I am unable to and I have a project submission due tomorrow. Ignore all the textview declaration in the given code below:



 private FirebaseFirestore db;
private int month,year;
private Calendar calendar;
private String email;
private String name,points;
private int i,j;
private String q,p,r;
public String data= new String[10][10];
private TextView rankingTextView,a,b;
public TextView t01,t02,t03,t10,t11,t12,t20,t21,t22,t30,t31,t32,t40,t41,t42,t50,t51,t52,t60,t61,t62,t70,t71,t72,t80,t81,t82,t90,t91,t92;

private static final String TAG = "MyApp";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboard);
calendar=Calendar.getInstance();
t01=(TextView)findViewById(R.id.t00);
t02=(TextView)findViewById(R.id.t01);
t03=(TextView)findViewById(R.id.t02);
t10=(TextView)findViewById(R.id.t10);
t11=(TextView)findViewById(R.id.t11);
t12=(TextView)findViewById(R.id.t12);
t20=(TextView)findViewById(R.id.t20);
t21=(TextView)findViewById(R.id.t21);
t22=(TextView)findViewById(R.id.t22);
t30=(TextView)findViewById(R.id.t30);
t31=(TextView)findViewById(R.id.t31);
t32=(TextView)findViewById(R.id.t32);
t40=(TextView)findViewById(R.id.t40);
t41=(TextView)findViewById(R.id.t41);
t42=(TextView)findViewById(R.id.t42);
t50=(TextView)findViewById(R.id.t50);
t51=(TextView)findViewById(R.id.t51);
t52=(TextView)findViewById(R.id.t52);
t60=(TextView)findViewById(R.id.t60);
t61=(TextView)findViewById(R.id.t61);
t62=(TextView)findViewById(R.id.t62);
t70=(TextView)findViewById(R.id.t70);
t71=(TextView)findViewById(R.id.t71);
t72=(TextView)findViewById(R.id.t72);
t80=(TextView)findViewById(R.id.t80);
t81=(TextView)findViewById(R.id.t81);
t82=(TextView)findViewById(R.id.t82);
db= FirebaseFirestore.getInstance();
email = getIntent().getStringExtra("email");
month = calendar.get(Calendar.MONTH);
month = month + 1;
year = calendar.get(Calendar.YEAR);
i=0;
j=0;
dothis();
Toast.makeText(this, "MO", Toast.LENGTH_SHORT).show();
t01.setText(data[0][0]);
t02.setText(data[0][1]);
t11.setText(data[1][0]);
t12.setText(data[1][1]);
t21.setText(data[2][0]);
t22.setText(data[2][1]);
t31.setText(data[3][0]);
t32.setText(data[3][1]);
t41.setText(data[4][0]);
t42.setText(data[4][1]);
t51.setText(data[5][0]);
t52.setText(data[5][1]);

private void dothis()
i=0;
j=0;

db.collection("users")
// .whereGreaterThanOrEqualTo("points" + Integer.toString(month) + Integer.toString(year), "0")
.orderBy("points" + Integer.toString(month) + Integer.toString(year)).limit(10)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task)
if (task.isSuccessful())
for (QueryDocumentSnapshot document : task.getResult())
if(document.exists())
Log.d(TAG, document.getId() + " => " + document.getData() + " ");
data[i][j] = document.getString("username");
data[i][j + 1] = document.getString("points" + Integer.toString(month) + Integer.toString(year));
i++;
Toast.makeText(Leaderboard.this, "OK", Toast.LENGTH_SHORT).show();


else
Log.d(TAG, "Error getting documents: ", task.getException());


);




}



Please don't duplicate my question. I searched a lot before putting.










share|improve this question
























  • Null means there is no field with the name you're looking for.

    – Doug Stevenson
    Nov 14 '18 at 19:13











  • I wouldnt say null I would say blank value ...its not giving anything

    – Prakarsh Jain
    Nov 14 '18 at 19:26











  • Please add your database structure and please responde with @AlexMamo

    – Alex Mamo
    Nov 14 '18 at 19:49











  • @AlexMamo Done!.

    – Prakarsh Jain
    Nov 14 '18 at 19:54











  • So you are saying that document.getString("username") inside onComplete() method is returning nothing? Or data[i][j] is returnin nothing outside the onComplete() method?

    – Alex Mamo
    Nov 14 '18 at 19:58















1















enter image description here



I just wrote a basic code to get multiple documents from Firestore database. The task is getting successful and you could see the document.getId() & document.getData() value in Log, but after that when I use document.getString(), it gives a blank output. I have been trying to find my error but I am unable to and I have a project submission due tomorrow. Ignore all the textview declaration in the given code below:



 private FirebaseFirestore db;
private int month,year;
private Calendar calendar;
private String email;
private String name,points;
private int i,j;
private String q,p,r;
public String data= new String[10][10];
private TextView rankingTextView,a,b;
public TextView t01,t02,t03,t10,t11,t12,t20,t21,t22,t30,t31,t32,t40,t41,t42,t50,t51,t52,t60,t61,t62,t70,t71,t72,t80,t81,t82,t90,t91,t92;

private static final String TAG = "MyApp";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboard);
calendar=Calendar.getInstance();
t01=(TextView)findViewById(R.id.t00);
t02=(TextView)findViewById(R.id.t01);
t03=(TextView)findViewById(R.id.t02);
t10=(TextView)findViewById(R.id.t10);
t11=(TextView)findViewById(R.id.t11);
t12=(TextView)findViewById(R.id.t12);
t20=(TextView)findViewById(R.id.t20);
t21=(TextView)findViewById(R.id.t21);
t22=(TextView)findViewById(R.id.t22);
t30=(TextView)findViewById(R.id.t30);
t31=(TextView)findViewById(R.id.t31);
t32=(TextView)findViewById(R.id.t32);
t40=(TextView)findViewById(R.id.t40);
t41=(TextView)findViewById(R.id.t41);
t42=(TextView)findViewById(R.id.t42);
t50=(TextView)findViewById(R.id.t50);
t51=(TextView)findViewById(R.id.t51);
t52=(TextView)findViewById(R.id.t52);
t60=(TextView)findViewById(R.id.t60);
t61=(TextView)findViewById(R.id.t61);
t62=(TextView)findViewById(R.id.t62);
t70=(TextView)findViewById(R.id.t70);
t71=(TextView)findViewById(R.id.t71);
t72=(TextView)findViewById(R.id.t72);
t80=(TextView)findViewById(R.id.t80);
t81=(TextView)findViewById(R.id.t81);
t82=(TextView)findViewById(R.id.t82);
db= FirebaseFirestore.getInstance();
email = getIntent().getStringExtra("email");
month = calendar.get(Calendar.MONTH);
month = month + 1;
year = calendar.get(Calendar.YEAR);
i=0;
j=0;
dothis();
Toast.makeText(this, "MO", Toast.LENGTH_SHORT).show();
t01.setText(data[0][0]);
t02.setText(data[0][1]);
t11.setText(data[1][0]);
t12.setText(data[1][1]);
t21.setText(data[2][0]);
t22.setText(data[2][1]);
t31.setText(data[3][0]);
t32.setText(data[3][1]);
t41.setText(data[4][0]);
t42.setText(data[4][1]);
t51.setText(data[5][0]);
t52.setText(data[5][1]);

private void dothis()
i=0;
j=0;

db.collection("users")
// .whereGreaterThanOrEqualTo("points" + Integer.toString(month) + Integer.toString(year), "0")
.orderBy("points" + Integer.toString(month) + Integer.toString(year)).limit(10)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task)
if (task.isSuccessful())
for (QueryDocumentSnapshot document : task.getResult())
if(document.exists())
Log.d(TAG, document.getId() + " => " + document.getData() + " ");
data[i][j] = document.getString("username");
data[i][j + 1] = document.getString("points" + Integer.toString(month) + Integer.toString(year));
i++;
Toast.makeText(Leaderboard.this, "OK", Toast.LENGTH_SHORT).show();


else
Log.d(TAG, "Error getting documents: ", task.getException());


);




}



Please don't duplicate my question. I searched a lot before putting.










share|improve this question
























  • Null means there is no field with the name you're looking for.

    – Doug Stevenson
    Nov 14 '18 at 19:13











  • I wouldnt say null I would say blank value ...its not giving anything

    – Prakarsh Jain
    Nov 14 '18 at 19:26











  • Please add your database structure and please responde with @AlexMamo

    – Alex Mamo
    Nov 14 '18 at 19:49











  • @AlexMamo Done!.

    – Prakarsh Jain
    Nov 14 '18 at 19:54











  • So you are saying that document.getString("username") inside onComplete() method is returning nothing? Or data[i][j] is returnin nothing outside the onComplete() method?

    – Alex Mamo
    Nov 14 '18 at 19:58













1












1








1








enter image description here



I just wrote a basic code to get multiple documents from Firestore database. The task is getting successful and you could see the document.getId() & document.getData() value in Log, but after that when I use document.getString(), it gives a blank output. I have been trying to find my error but I am unable to and I have a project submission due tomorrow. Ignore all the textview declaration in the given code below:



 private FirebaseFirestore db;
private int month,year;
private Calendar calendar;
private String email;
private String name,points;
private int i,j;
private String q,p,r;
public String data= new String[10][10];
private TextView rankingTextView,a,b;
public TextView t01,t02,t03,t10,t11,t12,t20,t21,t22,t30,t31,t32,t40,t41,t42,t50,t51,t52,t60,t61,t62,t70,t71,t72,t80,t81,t82,t90,t91,t92;

private static final String TAG = "MyApp";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboard);
calendar=Calendar.getInstance();
t01=(TextView)findViewById(R.id.t00);
t02=(TextView)findViewById(R.id.t01);
t03=(TextView)findViewById(R.id.t02);
t10=(TextView)findViewById(R.id.t10);
t11=(TextView)findViewById(R.id.t11);
t12=(TextView)findViewById(R.id.t12);
t20=(TextView)findViewById(R.id.t20);
t21=(TextView)findViewById(R.id.t21);
t22=(TextView)findViewById(R.id.t22);
t30=(TextView)findViewById(R.id.t30);
t31=(TextView)findViewById(R.id.t31);
t32=(TextView)findViewById(R.id.t32);
t40=(TextView)findViewById(R.id.t40);
t41=(TextView)findViewById(R.id.t41);
t42=(TextView)findViewById(R.id.t42);
t50=(TextView)findViewById(R.id.t50);
t51=(TextView)findViewById(R.id.t51);
t52=(TextView)findViewById(R.id.t52);
t60=(TextView)findViewById(R.id.t60);
t61=(TextView)findViewById(R.id.t61);
t62=(TextView)findViewById(R.id.t62);
t70=(TextView)findViewById(R.id.t70);
t71=(TextView)findViewById(R.id.t71);
t72=(TextView)findViewById(R.id.t72);
t80=(TextView)findViewById(R.id.t80);
t81=(TextView)findViewById(R.id.t81);
t82=(TextView)findViewById(R.id.t82);
db= FirebaseFirestore.getInstance();
email = getIntent().getStringExtra("email");
month = calendar.get(Calendar.MONTH);
month = month + 1;
year = calendar.get(Calendar.YEAR);
i=0;
j=0;
dothis();
Toast.makeText(this, "MO", Toast.LENGTH_SHORT).show();
t01.setText(data[0][0]);
t02.setText(data[0][1]);
t11.setText(data[1][0]);
t12.setText(data[1][1]);
t21.setText(data[2][0]);
t22.setText(data[2][1]);
t31.setText(data[3][0]);
t32.setText(data[3][1]);
t41.setText(data[4][0]);
t42.setText(data[4][1]);
t51.setText(data[5][0]);
t52.setText(data[5][1]);

private void dothis()
i=0;
j=0;

db.collection("users")
// .whereGreaterThanOrEqualTo("points" + Integer.toString(month) + Integer.toString(year), "0")
.orderBy("points" + Integer.toString(month) + Integer.toString(year)).limit(10)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task)
if (task.isSuccessful())
for (QueryDocumentSnapshot document : task.getResult())
if(document.exists())
Log.d(TAG, document.getId() + " => " + document.getData() + " ");
data[i][j] = document.getString("username");
data[i][j + 1] = document.getString("points" + Integer.toString(month) + Integer.toString(year));
i++;
Toast.makeText(Leaderboard.this, "OK", Toast.LENGTH_SHORT).show();


else
Log.d(TAG, "Error getting documents: ", task.getException());


);




}



Please don't duplicate my question. I searched a lot before putting.










share|improve this question
















enter image description here



I just wrote a basic code to get multiple documents from Firestore database. The task is getting successful and you could see the document.getId() & document.getData() value in Log, but after that when I use document.getString(), it gives a blank output. I have been trying to find my error but I am unable to and I have a project submission due tomorrow. Ignore all the textview declaration in the given code below:



 private FirebaseFirestore db;
private int month,year;
private Calendar calendar;
private String email;
private String name,points;
private int i,j;
private String q,p,r;
public String data= new String[10][10];
private TextView rankingTextView,a,b;
public TextView t01,t02,t03,t10,t11,t12,t20,t21,t22,t30,t31,t32,t40,t41,t42,t50,t51,t52,t60,t61,t62,t70,t71,t72,t80,t81,t82,t90,t91,t92;

private static final String TAG = "MyApp";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboard);
calendar=Calendar.getInstance();
t01=(TextView)findViewById(R.id.t00);
t02=(TextView)findViewById(R.id.t01);
t03=(TextView)findViewById(R.id.t02);
t10=(TextView)findViewById(R.id.t10);
t11=(TextView)findViewById(R.id.t11);
t12=(TextView)findViewById(R.id.t12);
t20=(TextView)findViewById(R.id.t20);
t21=(TextView)findViewById(R.id.t21);
t22=(TextView)findViewById(R.id.t22);
t30=(TextView)findViewById(R.id.t30);
t31=(TextView)findViewById(R.id.t31);
t32=(TextView)findViewById(R.id.t32);
t40=(TextView)findViewById(R.id.t40);
t41=(TextView)findViewById(R.id.t41);
t42=(TextView)findViewById(R.id.t42);
t50=(TextView)findViewById(R.id.t50);
t51=(TextView)findViewById(R.id.t51);
t52=(TextView)findViewById(R.id.t52);
t60=(TextView)findViewById(R.id.t60);
t61=(TextView)findViewById(R.id.t61);
t62=(TextView)findViewById(R.id.t62);
t70=(TextView)findViewById(R.id.t70);
t71=(TextView)findViewById(R.id.t71);
t72=(TextView)findViewById(R.id.t72);
t80=(TextView)findViewById(R.id.t80);
t81=(TextView)findViewById(R.id.t81);
t82=(TextView)findViewById(R.id.t82);
db= FirebaseFirestore.getInstance();
email = getIntent().getStringExtra("email");
month = calendar.get(Calendar.MONTH);
month = month + 1;
year = calendar.get(Calendar.YEAR);
i=0;
j=0;
dothis();
Toast.makeText(this, "MO", Toast.LENGTH_SHORT).show();
t01.setText(data[0][0]);
t02.setText(data[0][1]);
t11.setText(data[1][0]);
t12.setText(data[1][1]);
t21.setText(data[2][0]);
t22.setText(data[2][1]);
t31.setText(data[3][0]);
t32.setText(data[3][1]);
t41.setText(data[4][0]);
t42.setText(data[4][1]);
t51.setText(data[5][0]);
t52.setText(data[5][1]);

private void dothis()
i=0;
j=0;

db.collection("users")
// .whereGreaterThanOrEqualTo("points" + Integer.toString(month) + Integer.toString(year), "0")
.orderBy("points" + Integer.toString(month) + Integer.toString(year)).limit(10)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task)
if (task.isSuccessful())
for (QueryDocumentSnapshot document : task.getResult())
if(document.exists())
Log.d(TAG, document.getId() + " => " + document.getData() + " ");
data[i][j] = document.getString("username");
data[i][j + 1] = document.getString("points" + Integer.toString(month) + Integer.toString(year));
i++;
Toast.makeText(Leaderboard.this, "OK", Toast.LENGTH_SHORT).show();


else
Log.d(TAG, "Error getting documents: ", task.getException());


);




}



Please don't duplicate my question. I searched a lot before putting.







java android firebase google-cloud-firestore






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 13:38









Alex Mamo

45.8k82964




45.8k82964










asked Nov 14 '18 at 19:06









Prakarsh JainPrakarsh Jain

176




176












  • Null means there is no field with the name you're looking for.

    – Doug Stevenson
    Nov 14 '18 at 19:13











  • I wouldnt say null I would say blank value ...its not giving anything

    – Prakarsh Jain
    Nov 14 '18 at 19:26











  • Please add your database structure and please responde with @AlexMamo

    – Alex Mamo
    Nov 14 '18 at 19:49











  • @AlexMamo Done!.

    – Prakarsh Jain
    Nov 14 '18 at 19:54











  • So you are saying that document.getString("username") inside onComplete() method is returning nothing? Or data[i][j] is returnin nothing outside the onComplete() method?

    – Alex Mamo
    Nov 14 '18 at 19:58

















  • Null means there is no field with the name you're looking for.

    – Doug Stevenson
    Nov 14 '18 at 19:13











  • I wouldnt say null I would say blank value ...its not giving anything

    – Prakarsh Jain
    Nov 14 '18 at 19:26











  • Please add your database structure and please responde with @AlexMamo

    – Alex Mamo
    Nov 14 '18 at 19:49











  • @AlexMamo Done!.

    – Prakarsh Jain
    Nov 14 '18 at 19:54











  • So you are saying that document.getString("username") inside onComplete() method is returning nothing? Or data[i][j] is returnin nothing outside the onComplete() method?

    – Alex Mamo
    Nov 14 '18 at 19:58
















Null means there is no field with the name you're looking for.

– Doug Stevenson
Nov 14 '18 at 19:13





Null means there is no field with the name you're looking for.

– Doug Stevenson
Nov 14 '18 at 19:13













I wouldnt say null I would say blank value ...its not giving anything

– Prakarsh Jain
Nov 14 '18 at 19:26





I wouldnt say null I would say blank value ...its not giving anything

– Prakarsh Jain
Nov 14 '18 at 19:26













Please add your database structure and please responde with @AlexMamo

– Alex Mamo
Nov 14 '18 at 19:49





Please add your database structure and please responde with @AlexMamo

– Alex Mamo
Nov 14 '18 at 19:49













@AlexMamo Done!.

– Prakarsh Jain
Nov 14 '18 at 19:54





@AlexMamo Done!.

– Prakarsh Jain
Nov 14 '18 at 19:54













So you are saying that document.getString("username") inside onComplete() method is returning nothing? Or data[i][j] is returnin nothing outside the onComplete() method?

– Alex Mamo
Nov 14 '18 at 19:58





So you are saying that document.getString("username") inside onComplete() method is returning nothing? Or data[i][j] is returnin nothing outside the onComplete() method?

– Alex Mamo
Nov 14 '18 at 19:58












1 Answer
1






active

oldest

votes


















1














According to your comment, you say that when are you calling the following line of code:



t01.setText(data[0][0]);


Nothing is set to your t01 TextView and this is the normal behaviour since the data that is coming from the Firebase database is asynchronous. This means that the onComplete() method returns immediately after it's invoked, and the callback from the Task it returns, will be called some time later.



There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available. Because this method returns immediately, the value of your data[0][0] variable you're trying to use outside the onComplete() method, will not have been populated from the callback yet.



Basically, you're trying to use a value synchronously from an API that's asynchronous. That's not a good idea. You should handle the APIs asynchronously as intended.



A quick solve for this problem would be set all those texts to your TextViews only inside the onComplete() method. If you need to set them outside, I recommend dive into the asynchronous worlds and see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.






share|improve this answer























  • Thanks a lot man!

    – Prakarsh Jain
    Nov 15 '18 at 22:17










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307156%2fdoucment-getstring-not-giving-an-output-firestore%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














According to your comment, you say that when are you calling the following line of code:



t01.setText(data[0][0]);


Nothing is set to your t01 TextView and this is the normal behaviour since the data that is coming from the Firebase database is asynchronous. This means that the onComplete() method returns immediately after it's invoked, and the callback from the Task it returns, will be called some time later.



There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available. Because this method returns immediately, the value of your data[0][0] variable you're trying to use outside the onComplete() method, will not have been populated from the callback yet.



Basically, you're trying to use a value synchronously from an API that's asynchronous. That's not a good idea. You should handle the APIs asynchronously as intended.



A quick solve for this problem would be set all those texts to your TextViews only inside the onComplete() method. If you need to set them outside, I recommend dive into the asynchronous worlds and see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.






share|improve this answer























  • Thanks a lot man!

    – Prakarsh Jain
    Nov 15 '18 at 22:17















1














According to your comment, you say that when are you calling the following line of code:



t01.setText(data[0][0]);


Nothing is set to your t01 TextView and this is the normal behaviour since the data that is coming from the Firebase database is asynchronous. This means that the onComplete() method returns immediately after it's invoked, and the callback from the Task it returns, will be called some time later.



There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available. Because this method returns immediately, the value of your data[0][0] variable you're trying to use outside the onComplete() method, will not have been populated from the callback yet.



Basically, you're trying to use a value synchronously from an API that's asynchronous. That's not a good idea. You should handle the APIs asynchronously as intended.



A quick solve for this problem would be set all those texts to your TextViews only inside the onComplete() method. If you need to set them outside, I recommend dive into the asynchronous worlds and see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.






share|improve this answer























  • Thanks a lot man!

    – Prakarsh Jain
    Nov 15 '18 at 22:17













1












1








1







According to your comment, you say that when are you calling the following line of code:



t01.setText(data[0][0]);


Nothing is set to your t01 TextView and this is the normal behaviour since the data that is coming from the Firebase database is asynchronous. This means that the onComplete() method returns immediately after it's invoked, and the callback from the Task it returns, will be called some time later.



There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available. Because this method returns immediately, the value of your data[0][0] variable you're trying to use outside the onComplete() method, will not have been populated from the callback yet.



Basically, you're trying to use a value synchronously from an API that's asynchronous. That's not a good idea. You should handle the APIs asynchronously as intended.



A quick solve for this problem would be set all those texts to your TextViews only inside the onComplete() method. If you need to set them outside, I recommend dive into the asynchronous worlds and see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.






share|improve this answer













According to your comment, you say that when are you calling the following line of code:



t01.setText(data[0][0]);


Nothing is set to your t01 TextView and this is the normal behaviour since the data that is coming from the Firebase database is asynchronous. This means that the onComplete() method returns immediately after it's invoked, and the callback from the Task it returns, will be called some time later.



There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available. Because this method returns immediately, the value of your data[0][0] variable you're trying to use outside the onComplete() method, will not have been populated from the callback yet.



Basically, you're trying to use a value synchronously from an API that's asynchronous. That's not a good idea. You should handle the APIs asynchronously as intended.



A quick solve for this problem would be set all those texts to your TextViews only inside the onComplete() method. If you need to set them outside, I recommend dive into the asynchronous worlds and see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 13:35









Alex MamoAlex Mamo

45.8k82964




45.8k82964












  • Thanks a lot man!

    – Prakarsh Jain
    Nov 15 '18 at 22:17

















  • Thanks a lot man!

    – Prakarsh Jain
    Nov 15 '18 at 22:17
















Thanks a lot man!

– Prakarsh Jain
Nov 15 '18 at 22:17





Thanks a lot man!

– Prakarsh Jain
Nov 15 '18 at 22:17



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307156%2fdoucment-getstring-not-giving-an-output-firestore%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo