Redeem Firebase information on a Textview on Android

Asked

Viewed 100 times

2

All right people? I would like to rescue the information from Firebase in a Textview on Android. I can already enter the information in the Database, as shown in the following image:

inserir a descrição da imagem aqui

I would like to "pull" this information from "name", "raccoon", "sex", and others, in an Activity for a Textview. Does anyone know how to accomplish this? Thanks in advance!

  • I did something like this:;mDatabase = FirebaseDatabase.getInstance().getReference("Pets");

 mDatabase.addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
 // String playerName = dataSnapshot.getValue(String.class);
 Pets ModelPets = dataSnapshot.getValue(Pets.class);
 ((TextView) findViewById(R.id.lblNomePet)).setText(ModelPets.getNome());
 } However the text is null when running, nothing appears in Textview.

1 answer

0

I got it as follows, populating a list and assigning the values of this list to XML textviews:

    public class TelaApresentacoes extends AppCompatActivity {

    TextView edtNome, edtRaca, edtSexo;
    ListView listV_dadosB;

    FirebaseDatabase firebaseDatabaseB;
    DatabaseReference databaseReferenceB;
    Pets petsSelecionadaB;
    private List<Pets> listPetsB = new ArrayList<Pets>();
    private ArrayAdapter<Pets> arrayAdapterPetsB;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lyt_tela_apresentacoes);

        inicializarFirebase();
        eventoDatabase();

        edtNome = (TextView) findViewById(R.id.lblNomePet);
        edtRaca = (TextView) findViewById(R.id.lblRacaPet);
        edtSexo = (TextView) findViewById(R.id.lblSexoPet);
        listV_dadosB = (ListView) findViewById(R.id.listV_dados2);

        listV_dadosB.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                petsSelecionadaB = (Pets) parent.getItemAtPosition(position);
                edtNome.setText(petsSelecionadaB.getNome());
                edtRaca.setText(petsSelecionadaB.getRaca());
                edtSexo.setText(petsSelecionadaB.getSexo());

                Log.e(null, String.valueOf(position)); //apenas para setar log da posição
            }

        });

    }

    private void eventoDatabase() {
        databaseReferenceB.child("Pets").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                listPetsB.clear();
                for (DataSnapshot objSnapshot : dataSnapshot.getChildren()) {
                    Pets p = objSnapshot.getValue(Pets.class);
                    listPetsB.add(p);
                }
                arrayAdapterPetsB = new ArrayAdapter<Pets>(TelaApresentacoes.this,
                        android.R.layout.simple_list_item_1, listPetsB);
                listV_dadosB.setAdapter(arrayAdapterPetsB);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private void inicializarFirebase() {
        FirebaseApp.initializeApp(TelaApresentacoes.this);
        firebaseDatabaseB = FirebaseDatabase.getInstance();
        firebaseDatabaseB.setPersistenceEnabled(true);
        databaseReferenceB = firebaseDatabaseB.getReference();
    }}

Browser other questions tagged

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