Alertdialog with Listview in Firebase

Asked

Viewed 55 times

0

Hi, I’m new to Android Programming. I’m trying to put a custom Alertdialog in an Activity in this Alertdialog I want to put a listview that calls items present in my Firebase database. But when I click to open the Error Alertdialog and to. Follow the class code:

package com.cpcp.jaykl.pesquisacpcp;

import android.content.DialogInterface;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

public class ListaProdutos extends AppCompatActivity {
private static FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
List<BdProdutos> listProdutos = new ArrayList<BdProdutos>();
ArrayAdapter<BdProdutos> arrayAdapterProdutos;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista_produtos);
    inicializarFireBase();
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
            chamaTela();
        }
    });

}
private void inicializarFireBase()//inicializa o fireBase
{
    if(firebaseDatabase == null)
    {
        firebaseDatabase = FirebaseDatabase.getInstance();
    }
    FirebaseApp.initializeApp(ListaProdutos.this);
    databaseReference = firebaseDatabase.getReference();
}

private void chamaTela()
{
    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.teste_dialog, null);
    dialogBuilder.setView(dialogView);
    dialogBuilder.setMessage("Testando Tela");

    final ListView listViewTeste = (ListView)findViewById(R.id.listViewTeste);

    databaseReference.child("Produtos").orderByChild("nome").addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            for (DataSnapshot objSnapshot : dataSnapshot.getChildren())
            {
                BdProdutos bdProdutos = objSnapshot.getValue(BdProdutos.class);
                listProdutos.add(bdProdutos);
                arrayAdapterProdutos = new ArrayAdapter<BdProdutos>(ListaProdutos.this,
                        android.R.layout.simple_list_item_1, listProdutos);
                listViewTeste.setAdapter(arrayAdapterProdutos);
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError)
        {

        }
    });

    dialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {


        }
    });
    AlertDialog b = dialogBuilder.create();
    b.show();
}

}

  • What is the error displayed in logcat?

  • found this response in stackoverflow en, take a look

No answers

Browser other questions tagged

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