Listview does not return firebase data

Asked

Viewed 218 times

1

I’m trying to popular a listview, but I didn’t succeed. I even tried to follow what happened by the android monitor, and I realized that it is "populating" the listview with null data, as seen in the image. I am trying to popular the listview with the name and text that are within the occurrence node. Android monitor - occurrence: null Android monitor - ocorrencia: null

Can anyone help me? Below follows the activitys codes.

Codes:

Occurrence Activity

package br.com.douglasprogtg.appcond;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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 br.com.douglasprogtg.appcond.dao.Ocorrencia;
import br.com.douglasprogtg.appcond.dao.OcorrenciaList;

public class OcorrenciaActivity extends AppCompatActivity {

    /*EditText edtUsuario, edtTextOco;
    Button btnEnviarOcorrencia;
    ListView listOcorrencia;

    private ArrayList<String> ocorrencias;
    private ArrayAdapter adapter;
    private ValueEventListener ValueEventListenerOcorrencia;

    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;
    ValueEventListener ocorrenciaListener;

    private FirebaseAuth mAuth; */

    DatabaseReference databaseOcorrencia;

    public ListView listViewOcorrencias;
    public List<Ocorrencia> ocorrenciaList;

    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ocorrencia);

        firebaseAuth = FirebaseAuth.getInstance();

        final FirebaseUser user = firebaseAuth.getCurrentUser();

        databaseOcorrencia = FirebaseDatabase.getInstance().getReference("usuario").child(user.getUid()).child("ocorrencia");

        listViewOcorrencias = (ListView) findViewById(R.id.listViewOcorrencias);
        ocorrenciaList = new ArrayList<>();

    }

    @Override
    protected void onStart() {
        super.onStart();

        databaseOcorrencia.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                ocorrenciaList.clear();

                for (DataSnapshot ocorrenciaSnapshot : dataSnapshot.getChildren()){
                    Ocorrencia ocorrencia = ocorrenciaSnapshot.getValue(Ocorrencia.class);

                    ocorrenciaList.add(ocorrencia);
                }

                OcorrenciaList adapter = new OcorrenciaList(OcorrenciaActivity.this, ocorrenciaList);
                listViewOcorrencias.setAdapter(adapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    public void novaOcorrencia(View v){
        Intent intent = new Intent(this,NovaOcorrenciaActivity.class);
        startActivity(intent);
    }

}

Ocorrencialist.java

package br.com.douglasprogtg.appcond.dao;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

import br.com.douglasprogtg.appcond.R;

/**
 * Created by lap on 03/10/2017.
 */

public class OcorrenciaList extends ArrayAdapter<Ocorrencia> {

    private Activity context;
    private List<Ocorrencia> ocorrenciaList;

    public OcorrenciaList(Activity context, List<Ocorrencia> ocorrenciaList){
        super(context, R.layout.list_layout, ocorrenciaList);
        this.context = context;
        this.ocorrenciaList = ocorrenciaList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();

        View listViewItem = inflater.inflate(R.layout.list_layout, null, true);

        TextView textViewEmail = (TextView) listViewItem.findViewById(R.id.textViewEmail);
        TextView textViewTexto = (TextView) listViewItem.findViewById(R.id.textViewTexto);

        Ocorrencia ocorrencia = ocorrenciaList.get(position);

        textViewEmail.setText(ocorrencia.getUsuario());
        textViewTexto.setText(ocorrencia.getTextOco());
        System.out.println(ocorrenciaList);

        return listViewItem;
    }
}

Newoccurrence

package br.com.douglasprogtg.appcond;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import br.com.douglasprogtg.appcond.dao.Ocorrencia;

public class NovaOcorrenciaActivity extends AppCompatActivity {

    TextView txtNome;
    EditText edtTextOco;
    Button btnEnviarOcorrencia;
    Ocorrencia ocorrencia;

    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;
    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nova_ocorrencia);

        firebaseAuth = FirebaseAuth.getInstance();

        databaseReference = FirebaseDatabase.getInstance().getReference();

        final FirebaseUser user = firebaseAuth.getCurrentUser();

        txtNome = (TextView) findViewById(R.id.txtEmailUsuario);
        edtTextOco = (EditText) findViewById(R.id.edtTextOco);
        btnEnviarOcorrencia = (Button) findViewById(R.id.btnEnviarOcorrencia);

        txtNome.setText(user.getEmail());

        btnEnviarOcorrencia.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ocorrencia = new Ocorrencia();
                ocorrencia.setUsuario(txtNome.getText().toString());
                ocorrencia.setTextOco(edtTextOco.getText().toString());


                //salvarMensagem(mensagem);
                DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
                //databaseReference.child("ocorrencias").push().setValue(firebaseDatabase, new DatabaseReference.CompletionListener() {
                databaseReference.child("usuario").child(user.getUid()).child("ocorrencia").push().setValue(firebaseDatabase, new DatabaseReference.CompletionListener() {
                    @Override
                    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                        //Problem with saving the data
                        if (databaseError != null) {
                            Toast.makeText(NovaOcorrenciaActivity.this, "Erro ao criar ocorrência / erro ao inserir dados!", Toast.LENGTH_LONG).show();
                        } else {
                            //Data uploaded successfully on the server
                            databaseReference.child("nome").setValue(txtNome.getText().toString());
                            databaseReference.child("texto").setValue(edtTextOco.getText().toString());
                            Toast.makeText(NovaOcorrenciaActivity.this, "Ocorrência aberta com sucesso!", Toast.LENGTH_LONG).show();
                            retornaTela();
                        }

                    }
                });

            }
        });

    }

    public void retornaTela(){
        Intent intent = new Intent(NovaOcorrenciaActivity.this, MenuActivity.class);
        startActivity(intent);
        finish();
    }
}

My bank in firebase:

Database no firebase

No answers

Browser other questions tagged

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