So basically I am making an utill class so it can read my firebase database and depending on the status it will return an array list of jobs base on status.
This method work, however first time it runs it will return empty array. if I run the method again by click the function again (not re running my program) it will return the right values
public class K {
private static final String TAG = "ListingJob";
private static FirebaseFirestore mFirebaseFirestore;
private static ArrayList<Job> myListJobs = new ArrayList();//this will be returned
public static ArrayList<Job> firebaseEnquiry(Context context,String status){
ArrayList<Applications> listApplications = new ArrayList();
mFirebaseFirestore=FirebaseFirestore.getInstance();
mFirebaseFirestore.collection("Applications")
.whereEqualTo("status",status)
.whereEqualTo("User_id", FirebaseAuth.getInstance().getCurrentUser().getUid())
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
Applications applications=document.toObject(Applications.class);
listApplications.add(applications);
}
if(listApplications.size()>0){
//Fetch All the Jobs and compare with the pending list
fetchAllJobs(listApplications);
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
Log.d(TAG, myListJobs +"this is the myListJob" );// this is empty array 1st time running
return myListJobs;
}
public static void fetchAllJobs(ArrayList<Applications> listApplications){
myListJobs.clear();
mFirebaseFirestore.collection("Jobs")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
for (int i=0;i<listApplications.size();i++){
String testVariable = document.getId();
if(listApplications.get(i).getJob_id().trim().equals(testVariable.trim())){
Job job=document.toObject(Job.class);
myListJobs.add(job);
}
}
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
}
}
Can anyone help me out ?!