Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView无法从firebase检索数据_Android_Listview_Firebase_Firebase Realtime Database - Fatal编程技术网

Android ListView无法从firebase检索数据

Android ListView无法从firebase检索数据,android,listview,firebase,firebase-realtime-database,Android,Listview,Firebase,Firebase Realtime Database,我正在尝试填充listview,但没有成功。 在android monitor中,listview在ocorrencia插槽中为空。 你能帮我吗?我不知道怎么解决这个问题。 我想在listview中检索节点ocorrencia的nome和texto 代码: 奥科瑞西亚活动 package br.com.douglasprogtg.appcond; import android.content.Intent; import android.support.v7.app.AppCompatActi

我正在尝试填充listview,但没有成功。 在android monitor中,listview在ocorrencia插槽中为空。 你能帮我吗?我不知道怎么解决这个问题。 我想在listview中检索节点ocorrencia的nome和texto

代码:

奥科瑞西亚活动

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);
    }

}
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的数据库。

您尝试过使用断点进行调试吗?@Supradip.M是的,但没有成功解决我的问题。
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();
    }
}