0

I'm trying to build a mobile app for museums. And I'm trying to reach an object in Firestore which has a field like integer 1,2,3... I wrote a query for that but It always ended up with failure. Can you see there I'm mistaken ? Thanks! I updated my problematic query now. I guess the problem is in the inner class variables. When I tried to use final variable in query It wont be successful.

final int index = Integer.parseInt(inputString) - 1;

CollectionReference mapLocation = firebaseFirestore.collection("mapLocations");

Query query = mapLocation.whereEqualTo("artifactID", inputString);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if(task.isSuccessful()){
            for(QueryDocumentSnapshot queryDocumentSnapshot : task.getResult()){
                Map<String,Object> currentArtifact = new HashMap<>();
                currentArtifact = queryDocumentSnapshot.getData();

                int cX = Integer.parseInt(currentArtifact.get("X").toString());
                int cY = Integer.parseInt(currentArtifact.get("Y").toString());

                artifacts[index].setBackgroundColor(artifacts[index].getContext().getResources().getColor(R.color.Green));
                artifacts[index].setX(cX);
                artifacts[index].setY(cY);

                documentReferenceUser.update("path",artPath);

            }
        }
        else{
            Toast.makeText(context,"Failure",Toast.LENGTH_LONG);
        }
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        return;
    }
});
int index = Integer.parseInt(inputString) - 1;
                        final int tempIndex = index;
                        final String tempStr = inputString;
                        Task<QuerySnapshot> documentReference = firebaseFirestore.collection("mapLocations")
                                .whereEqualTo("artifactID",index)
                                .get()
                                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                        boolean flag = task.isSuccessful();
                                        Log.d("flag",String.valueOf(flag));
                                        if (task.isSuccessful()) {
                                            for (QueryDocumentSnapshot document : task.getResult()) {
                                                Log.d(TAG, document.getId() + " => " + document.getData());
                                                Map<String, Object> currArtifact = new HashMap<>();
                                                currArtifact = document.getData();

                                                int coordinateX = Integer.parseInt(currArtifact.get("X").toString()) ;
                                                int coordinateY = Integer.parseInt(currArtifact.get("Y").toString()) ;

                                                artifacts.get(tempIndex).setBackgroundColor(artifacts.get(tempIndex).getContext().getResources().getColor(R.color.Green));
                                                playerButton.setX(coordinateX);
                                                playerButton.setY(coordinateY);
                                                artPath.add(tempStr);
                                                documentReferenceUser.update("path",artPath);

                                            }

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

                                    }
                                });
Taha Ateş
  • 111
  • 1
  • 3
  • 13
  • 1
    Could you provide the error you are getting in your Logcat ? – Hritik Gupta May 31 '20 at 07:33
  • 1
    and if you can, screenshot of your database collection ( the one you trying to get ). – Yakir Malka May 31 '20 at 07:44
  • If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question. Also, edit your question and add your database structure as a screenshot. Please respond with @AlexMamo – Alex Mamo Jun 01 '20 at 09:52
  • I'm sorry that I'm being late on answer, thank you for all kindness. I figured it out that the main problem is here that passing an ArrayList of buttons one function to another. I mean I need to pass an arraylist out of onviewcreated method to use. I initialize array in there but I must use it out of onviewcreated. Can you help ? @AlexMamo – Taha Ateş Jun 03 '20 at 12:29
  • Could you please share the onviewcreated method? Do you have the methods in the same class? Could you please share the whole code and mention the two methods you from where you want to pass the arraylist to. – Nibrass H Jun 04 '20 at 11:46
  • @NibrassH can you please look updated question? I have only one method in onviewcreated method and its only a button's listener. But I'm not trying pass an array list I want to use inner variables in query. – Taha Ateş Jun 04 '20 at 14:10

1 Answers1

0

So the answer to this issue is that you can't use or pass a data which has not been loaded yet. The call to Firestore is an asynchronous call, it means that it launched a processs to do the query to the Firestore and once it's done, it executes the onComplete listener. That's why you are getting failures, as you can't get the data immediately after making a call to the database.

Here what you can do is, to have a data handler method that takes the ArrayList and and process the data. This method will be called from onComplete listener once you've retrieved or gotten all the data.

For example:

Task<QuerySnapshot> documentReference = firebaseFirestore.collection("mapLocations")
  .whereEqualTo("artifactID",index)
  .get()
  .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
   @Override
   public void onComplete(@NonNull Task<QuerySnapshot> task) {
       boolean flag = task.isSuccessful();
       Log.d("flag",String.valueOf(flag));
       if (task.isSuccessful()) {
           List<Data> list = new ArrayList<>();
           for (QueryDocumentSnapshot document : task.getResult()) {
             Log.d(TAG, document.getId() + " => " + document.getData());

             list.add(new Data(document.getData()));
       }
       metho1(list);
     } else {
       Log.d(TAG, "Error getting documents: ", task.getException());
     }

  }
});

metho1(list){
 for (int i=0; i< list.length(); i++){
     currArtifact = list[i].getData();
     int coordinateX = Integer.parseInt(currArtifact.get("X").toString()) ;
     int coordinateY = Integer.parseInt(currArtifact.get("Y").toString()) ;
  artifacts.get(tempIndex).setBackgroundColor(artifacts.get(tempIndex).getContext().getResources().getColor(R.color.Green));
     playerButton.setX(coordinateX);
     playerButton.setY(coordinateY);
     artPath.add(tempStr);

     documentReferenceUser.update("path",artPath);
 }
}

Please have a look into this similar post as well.

Nibrass H
  • 2,403
  • 1
  • 8
  • 14