Android Studio - Firebase - Search & edit data

Asked

Viewed 935 times

2

I’m creating an app using Android Studio, which stores customer information in Firebase : inserir a descrição da imagem aqui

I need to create a way to search for these clients by name or CPF, if it is necessary to change something, then I wonder if someone can give some hint or know some interesting tutorial for this..

2 answers

0

You can find the desired record using a sequential check with the command for, as per code example below :

    mDatabase = FirebaseDatabase.getInstance().getReference();

    mDatabase.child("clientes").addListenerForSingleValueEvent(new ValueEventListener() {
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot snap : dataSnapshot.getChildren()) {
                if (Objects.equals(snap.child("cnpj").getValue(), "999.999.999-99")) {

                    // seu codigo aqui

                }
            }
        }

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

The above code will read sequentially from the first to the last record and if there is the cnpj informed, you can collect the other customer data.

  • This way you have to always bring all customers and filter on the client. Using firebase Query can filter data and decrease time and amount of data traffic.

0

You will have to use a query in Firebase, to be able to filter. Try to do it this way:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("clientes");

Query clienteCnpj = ref.orderByChild("cnpj").equalTo("CNPJ");

clienteCnpj.addValueEventListener( new ValueEventListener(){
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot clienteSnap : dataSnapshot.getChildren() ){

        }
    }

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.