I'm trying to popular a listview, but I was not successful. I even tried to monitor the android monitor what happened, and I realized that it is "populating" the listview with null data, as seen in the image. I'm trying to populate the listview with the name and text that are inside the node's instance. Android monitor - occurrence: null
Cananyonehelpme?Belowistheactivitycode.
Codes:
Occurrenceofactivity
packagebr.com.douglasprogtg.appcond;importandroid.content.Intent;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.ListView;importcom.google.firebase.auth.FirebaseAuth;importcom.google.firebase.auth.FirebaseUser;importcom.google.firebase.database.DataSnapshot;importcom.google.firebase.database.DatabaseError;importcom.google.firebase.database.DatabaseReference;importcom.google.firebase.database.FirebaseDatabase;importcom.google.firebase.database.ValueEventListener;importjava.util.ArrayList;importjava.util.List;importbr.com.douglasprogtg.appcond.dao.Ocorrencia;importbr.com.douglasprogtg.appcond.dao.OcorrenciaList;publicclassOcorrenciaActivityextendsAppCompatActivity{/*EditTextedtUsuario,edtTextOco;ButtonbtnEnviarOcorrencia;ListViewlistOcorrencia;privateArrayList<String>ocorrencias;privateArrayAdapteradapter;privateValueEventListenerValueEventListenerOcorrencia;FirebaseDatabasefirebaseDatabase;DatabaseReferencedatabaseReference;ValueEventListenerocorrenciaListener;privateFirebaseAuthmAuth;*/DatabaseReferencedatabaseOcorrencia;publicListViewlistViewOcorrencias;publicList<Ocorrencia>ocorrenciaList;privateFirebaseAuthfirebaseAuth;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_ocorrencia);firebaseAuth=FirebaseAuth.getInstance();finalFirebaseUseruser=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);
}
}
OccurrenceList.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;
}
}
New Activity activity
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: