Popular a spinner with data stored in a node in Firebase for Android

Asked

Viewed 333 times

0

I’m performing a service for a client where I need to popular a Spinner with data stored in a node in Firebase. I’ve tried several tutorials, but no result.

I created two entities in my project, an entity Clientes, containing the attributes and getters and setters of clientes and another entity called Prefixos, which also contains attributes and getters and setters prefixed.

I created two activity's, one to register new customers and one to register new prefixes. The clients and prefixes are being registered correctly in us in Firebase, however, when I try to get the data registered in the nodes Clientes and Prefixos to put on a Spinner, I’m not getting it properly populated in a activity containing these spinners.

To summarize: I have one activity separate from activitys registration, where I have a spinner for cliente and one for prefixo, where I want to show on a spinner the name of the customer and the prefix of his car, but I am not succeeding. Thanks in advance.

  • Where is the problematic code?

1 answer

1

@Brian Moreira I had the same problem to popular the spinner, however I managed making this code I hope it helps you.

 reference2.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    final List<String> areas = new ArrayList<String>();
                    for (DataSnapshot areaSnapshot : dataSnapshot.getChildren()) {
                        String areaName = areaSnapshot.child("localRestaurante").getValue(String.class);
                        if (areaName == local) {
                            String nomerestaurante = areaSnapshot.child("nome").getValue(String.class);
                            areas.add(nomerestaurante);

                        } else {
                            break;
                        }

                    }

                    SpinnerPratos = (Spinner) findViewById(R.id.spinner_pratos);
                    ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, areas);
                    areasAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    SpinnerPratos.setAdapter(areasAdapter);

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }

Obs.: I saw that you posted your question three days ago and nobody answered you suggest that in the next question you want to ask, put the code that you are using because, the community thinks that you didn’t try and ta here to receive a ready answer then the staff does not answer you.

Browser other questions tagged

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