As on the below code, I was trying to iteratively get the "AVG" value from a Firebase collection document.
But this code will loop only till addOnCompleteListener
, so it's not populating the updatedYears
array.
Basically what I need to do is, I have two drop-downs, Based on the 1st drop-down selection, the 2nd drop-down value needs to be changed by getting the data from the Firebase database.
public void getYearList(String type) {
for (String year:allYearList) {
int i = 0;
DocumentReference docref = FirebaseFirestore.getInstance().collection(type).document("2016");
docref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
DocumentSnapshot doc = task.getResult();
updatedYears[i] = doc.get("Avg").toString();
}
});
}
}
I was calling this method from the setOnItemSelectedListener
by the 1st dropdown.
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
getYearList(spinner.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});