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

0

I'm performing a service for a client where I need to populate a Spinner with data stored on a node in Firebase. I've tried several tutorials, but I did not get results.

I have created two entities in my project, a Clientes , which contains the attributes and getters and setters of clientes and another entity called Prefixos , which also contains attributes and getters e setters of car prefixes.

I created two activity's , one to register new clients and another to register new prefixes. The clients and prefixes are being correctly registered in us in Firebase, but when I try to get the data registered in the Clientes and Prefixos nodes to put in a Spinner , I'm not able to populate it correctly in a activity which contains these spinners .

To summarize: I have a activity separated from the activitys of register, where I have a spinner to cliente and one to prefixo , where I want to show in spinner the name of the client and the% prefix his car, but I'm not succeeding. Thanks in advance.

    
asked by anonymous 06.10.2017 / 18:55

1 answer

1

@Brian Moreira I had the same problem to popular the spinner, however I managed to make 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) {

                }
            });

        }

Note: I saw that you posted your question to three days and no one answered you I suggest that in the next question you want to do, put the code that you are using because the community thinks you did not even try it here to receive an answer ready then the staff will not answer you.

    
09.10.2017 / 21:24