André Silva
André Silva

Reputation: 121

Filter results by an Edit Text

In my ExploreFragment.java I have an EditText which basically allows the user to search country names that are stored in my Firebase Realtime Database. What I want to achieve is, when the user types something in the EditText and then presses "submit" in their keyboard, it makes a query that retrieves every country which has the string they typed in their name, and get redirected to my fragment FilteredResultsFragment.java which displays the names of the countries retrieved. My fragment has a recycler view with an Adapter. I haven't achieved this yet. I need to retrieve a List with the urls and a List with the names. The query should look like this:

FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
Log.d("TAMANHO","size = "+paisEscolhido.size());
for (int i = 0; i<paisEscolhido.size(); i++) {
    Log.d("CONTINETNE","size = "+paisEscolhido.get(i));
    DatabaseReference paisNomeContinentes = mDatabase.getReference().child("paises");
    Query queries = paisNomeContinentes.orderByChild("Continente").equalTo(paisEscolhido.get(i));

    queries.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        List<String> imagemPaisList = new ArrayList<>();
        List<String> nomePaisList = new ArrayList<>();
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            String imagemPais = ds.child("Imagem").getValue(String.class);
            String nomePais = ds.child("Nome").getValue(String.class);
            imagemPaisList.add(imagemPais);
            nomePaisList.add(nomePais);
        }
        int urlCount = imagemPaisList.size();

        //  int randomImage = new Random().nextInt(urlCount);

        for (int i = 0; i < nomePaisList.size(); i++) {

                Integer randomVariavel = new Random().nextInt(urlCount);

                randomImagemPaisList.add(imagemPaisList.get(randomVariavel));
                randomNomePaisList.add(nomePaisList.get(randomVariavel));

        }

UPDATE

I've come up with this query:

FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
DatabaseReference paisNomeContinentes = mDatabase.getReference().child("paises");
Query queries = paisNomeContinentes.orderByChild("Nome").startAt("Po");

queries.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        List<String> imagemPaisList = new ArrayList<>();
        List<String> nomePaisList = new ArrayList<>();
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            String imagemPais = ds.child("Imagem").getValue(String.class);
            String nomePais = ds.child("Nome").getValue(String.class);
            imagemPaisList.add(imagemPais);
            nomePaisList.add(nomePais);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
    }
});

That should retrieve the Name of the country that starts with the value the user writes in my EditText but, when I write, for example "Po", it should give me "Portugal" and "Poland" but, instead, it gives me random images...

This is the data structure:

enter image description here

UPDATE V2 It actually gives me "Poland" and "Portugal" as the top results but it shows my more ImageView besides those two (and with random images too)...

Upvotes: 0

Views: 127

Answers (1)

Andr&#233; Kool
Andr&#233; Kool

Reputation: 4978

If you want to use startAt() to filter your data you also have to use endAt() because startAt() works as a starting point for your query and not a filter. By adding \uf8ff (the last unicode character) behind your searchstring in endAt() you limit your query to get only the values that start with your searchstring:

Query queries = paisNomeContinentes.orderByChild("Nome").startAt("Po").endAt("Po" + "\uf8ff");

Just remember this will only get the items that start with the searchstring, not the items that contain the searchstring.

Upvotes: 2

Related Questions