Android Studio - Firebase - Search and edit data

2

I'm creating an app using Android Studio, which stores customer information in Firebase:

I need to create a way to search for these clients by name or CPF, in case something needs to be changed, then I'd like to know if anyone can give a hint or know some interesting tutorial for this.

    
asked by anonymous 23.08.2017 / 22:17

2 answers

0

You can find the desired record using a sequential check with the for command, as shown in the 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 cnpj is informed, you can collect the other customer data.

    
11.09.2017 / 21:10
0

You'll have to use a query in Firebase to filter. Try to do this:

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) {}
});
    
11.09.2017 / 21:46