0
private ArrayList<String> idRecords = new ArrayList<>();

i try to put string in here but, when i using db.collection(collection).whereEqualTo(fields, values).get

private void getIdRecords(){
    db.collection("records")
            .whereEqualTo("idUsers", user.getUid())
            .whereEqualTo("status", "progress")
            .get()
            .addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        idRecords.add(document.getId());                           
                    }
                } else {
                    Log.d("get Id Records : ", "Error getting documents: ", task.getException());
                }
            });
}

it cant assign the value to ArrayList full code below :

public class WorkOffActivity extends AppCompatActivity {

private ArrayList<String> idRecords = new ArrayList<>();

private FirebaseAuth mAuth = FirebaseAuth.getInstance();
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private FirebaseUser user = mAuth.getCurrentUser();

private TextView tv_times, tv_activity, tv_place, tv_startDate;
private Button btn_draft, btn_cancel, btn_finish;
private EditText et_progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_work_off);

    getIdRecords();

    initializeComponent();
    setTextComponent();
}

private void initializeComponent() {
    tv_times = findViewById(R.id.offWork_tvTimes);
    tv_activity = findViewById(R.id.offWork_tvActivity);
    tv_place = findViewById(R.id.offWork_tvPlace);
    tv_startDate = findViewById(R.id.offWork_tvStartDate);
    et_progress = findViewById(R.id.offWork_etProgress);

    btn_draft = findViewById(R.id.offWork_btnSaveTemp);
    btn_cancel = findViewById(R.id.offWork_btnCancelActivity);
    btn_finish = findViewById(R.id.offWork_btnFinishActivity);
}

private void getIdRecords() {
    db.collection("records").whereEqualTo("idUsers", user.getUid())
            .whereEqualTo("status", "progress")
            .get()
            .addOnCompleteListener(task     -> {
                String temp = null;
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        temp = document.getId();
                    }
                    idRecords.add(temp);
                } else {
                    Log.d("get Id Records : ", "Error getting documents: ", task.getException());
                }
            });
}

private String[] getDataRecords(){
    String[] recordsData = new String[6];
    db.collection("records").document(idRecords.get(0))
            .get().addOnCompleteListener(task -> {
        DocumentSnapshot doc = task.getResult();
        recordsData[0] = doc.getString("activity");
        recordsData[1] = doc.getString("idUsers");
        recordsData[2] = doc.getString("idWorkspaces");
        recordsData[3] = doc.getString("progress");
        recordsData[4] = doc.getString("status");
        recordsData[5] = doc.getString("times");
    });

    return recordsData;
}

private void setTextComponent() {
      Toast.makeText(getApplicationContext(), idRecords.get(0), Toast.LENGTH_SHORT).show();
}

i cant take the value from that array with this idRecords.get(0)

nwfl
  • 11
  • 2
  • I'm not sure I understand. What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Dec 23 '21 at 11:54
  • i try to put the id that i get from collection with function whereEqualTo but on the for(QueryDocumentSnapshot document : task.getResult()) can't put the value that i get inside that for loop into outside for loop, – nwfl Dec 23 '21 at 12:14
  • There is no way you can use `idRecords.get(0)` outside the onComplete method. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Cloud Firestore using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953). – Alex Mamo Dec 23 '21 at 12:48
  • ohhhh oke thanks btw – nwfl Dec 23 '21 at 12:58

0 Answers0