我正在尝试填充listview,但没有成功。 在android监视器中,listview在ocorrencia插槽中为null。 Android monitor - ocorrencia: null 可以帮我?我不知道如何解决这个问题。 我想在listview节点中检索节点ocorrencia的nome和texto。
代码:
Ocorrencia活动
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;
}
}
NovaOcorrencia活动
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();
}
}
我的数据库来自firebase。 Database from firebase