Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. public class HomeController {
  2.  
  3. public static List<Struttura> Ricerca(String nome_struttura, String citta, String tipo_struttura, int prezzo_min, int prezzo_max){
  4.  
  5. FirebaseFirestore db = FirebaseFirestore.getInstance();
  6. List<Struttura> risultati = new ArrayList<>();
  7. FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
  8. .setTimestampsInSnapshotsEnabled(true)
  9. .build();
  10. db.setFirestoreSettings(settings);
  11.  
  12. // Create a reference to the Strutture collection
  13. CollectionReference struttureRef = db.collection("Strutture");
  14.  
  15. // Create a query against the collection.
  16. Query query = struttureRef;
  17.  
  18. if (!nome_struttura.isEmpty()) {
  19. query = query.whereEqualTo("nome", nome_struttura);
  20. }
  21. if (!citta.isEmpty()) {
  22. query = query.whereEqualTo("citta", citta);
  23. }
  24. if (!tipo_struttura.equals("Tutte")) {
  25. query = query.whereEqualTo("tipo", tipo_struttura);
  26. }
  27. //After creating a query object, use the get() function to retrieve the results
  28. Task<QuerySnapshot> task = query.get()
  29. .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
  30. @Override
  31. public void onComplete(@NonNull Task<QuerySnapshot> task) {}
  32. });
  33. //Waiting for async task to complete
  34. while(!task.isComplete()){}
  35.  
  36. if (task.isSuccessful()) {
  37. for (QueryDocumentSnapshot document : task.getResult()) {
  38. Log.d("01", document.getId() + " => " + document.getData());
  39. Struttura struttura = document.toObject(Struttura.class);
  40. risultati.add(struttura);
  41. }
  42. } else {
  43. Log.d("01", "Error getting documents: ", task.getException());
  44. }
  45.  
  46. for(int i=0; i<risultati.size(); i++) {
  47. if((risultati.get(i).getPrezzo_min()<prezzo_min)|(risultati.get(i).getPrezzo_max()>prezzo_max)) {
  48. risultati.remove(i);
  49. }
  50. }
  51.  
  52. return risultati;
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement