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());
}
}
});