Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HomeController {
- public static List<Struttura> Ricerca(String nome_struttura, String citta, String tipo_struttura, int prezzo_min, int prezzo_max){
- FirebaseFirestore db = FirebaseFirestore.getInstance();
- List<Struttura> risultati = new ArrayList<>();
- FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
- .setTimestampsInSnapshotsEnabled(true)
- .build();
- db.setFirestoreSettings(settings);
- // Create a reference to the Strutture collection
- CollectionReference struttureRef = db.collection("Strutture");
- // Create a query against the collection.
- Query query = struttureRef;
- if (!nome_struttura.isEmpty()) {
- query = query.whereEqualTo("nome", nome_struttura);
- }
- if (!citta.isEmpty()) {
- query = query.whereEqualTo("citta", citta);
- }
- if (!tipo_struttura.equals("Tutte")) {
- query = query.whereEqualTo("tipo", tipo_struttura);
- }
- //After creating a query object, use the get() function to retrieve the results
- Task<QuerySnapshot> task = query.get()
- .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
- @Override
- public void onComplete(@NonNull Task<QuerySnapshot> task) {}
- });
- //Waiting for async task to complete
- while(!task.isComplete()){}
- if (task.isSuccessful()) {
- for (QueryDocumentSnapshot document : task.getResult()) {
- Log.d("01", document.getId() + " => " + document.getData());
- Struttura struttura = document.toObject(Struttura.class);
- risultati.add(struttura);
- }
- } else {
- Log.d("01", "Error getting documents: ", task.getException());
- }
- for(int i=0; i<risultati.size(); i++) {
- if((risultati.get(i).getPrezzo_min()<prezzo_min)|(risultati.get(i).getPrezzo_max()>prezzo_max)) {
- risultati.remove(i);
- }
- }
- return risultati;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement