I am making an android app, in one activity there are the following two functions.
public void display(RequestData rd[],int j)
{
Toast.makeText(Student_see_request_status.this,j+" h"+r[0]+"",Toast.LENGTH_SHORT).show();
//using current value of j
}
public void set()
{
FirebaseFirestore fs=FirebaseFirestore.getInstance();
DocumentReference doc=fs.collection("Request").document(FirebaseAuth.getInstance().getCurrentUser().getEmail());
doc.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
Map<String, Object> m = documentSnapshot.getData();
r[0] = (long) m.get("Request Number");
if(r[0]==0)
{
Toast.makeText(Student_see_request_status.this,"NO REQUESTS HERE",Toast.LENGTH_SHORT).show();
return;
}
rd=new RequestData[(int)r[0]+2];
for(int j[]={1};j[0]<=r[0];j[0]++)
{
doc.collection("Request " + j[0]).document("Request").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
Map<String, Object> details = documentSnapshot.getData();
rd[j[0]]=new RequestData();
rd[j[0]].setHeader("Change " + details.get("Change what") + " to " + details.get("Value"));
rd[j[0]].setReason((String) details.get("Reason"));
display(rd,j[0]);
}
});
}
}
});
}
Here rd and r are global variables. The code does not run properly with for loop. First, the loop in set() method gets completed and then the control moves to display() with j=r[0]. I need every value of j to reach display() Can anyone please help me with this?