Send Data from an Activity to the Fragment using Firebase

0

I'm developing an App that in the first Activity has a RecyclerView with the names of the Patients saved in Firebase. Through the addOnItemTouchListener and onclick I can identify which patient is selected, after selecting the Patient I must pass the identifier to a Fragment that will have all the patient data.

With this identifier I make a new query in Firebase to list the data of this selected patient.

I'm using Parcelable to do the communication, but it's not working, the result inside the Fragment is always null. Follow the fuck, if anyone can help me.

Patient Class with Parcelable Class:

    package com.fisioapp.rodiney.classes;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

import com.fisioapp.rodiney.config.ConfiguracaoFirebase;
import com.fisioapp.rodiney.helper.Base64Custom;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;


/**
 * Created by Rodiney on 20/02/2018.
 */

public class CadastroPaciente implements Parcelable{
    private String editNome;
    private String editNascimento;
    private String editNatural;
    private String editCPF;
    private String editRg;
    private String editCelular;
    private String editEmergencia;
    private String editEmail;
    private String editLogadouro;
    private String editComplemento;
    private String editBairro;
    private String editCidade;
    private String key;

    public CadastroPaciente() {
    }

    public CadastroPaciente(Parcel in) {
        editNome = in.readString();
        editNascimento = in.readString();
        editNatural = in.readString();
        editCPF = in.readString();
        editRg = in.readString();
        editCelular = in.readString();
        editEmergencia = in.readString();
        editEmail = in.readString();
        editLogadouro = in.readString();
        editComplemento = in.readString();
        editBairro = in.readString();
        editCidade = in.readString();
        key = in.readString();
    }

    public static final Creator<CadastroPaciente> CREATOR = new Creator<CadastroPaciente>() {
        @Override
        public CadastroPaciente createFromParcel(Parcel in) {
            return new CadastroPaciente(in);
        }

        @Override
        public CadastroPaciente[] newArray(int size) {
            return new CadastroPaciente[size];
        }
    };

    public void salvarPaciente(){
        FirebaseAuth autenticacao = ConfiguracaoFirebase.getFirebaseAutenticacao();
        String idUsuario = Base64Custom.codificarBase64(autenticacao.getCurrentUser().getEmail());
        DatabaseReference firebase = ConfiguracaoFirebase.getFirebaseDatabase();
        firebase.child("Profissional")
                .child("Paciente")
                .child(idUsuario)
                .push()
                .setValue(this);


    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getEditNome() {
        return editNome;
    }

    public String getEditNatural() {
        return editNatural;
    }

    public void setEditNatural(String editNatural) {
        this.editNatural = editNatural;
    }

    public String getEditEmergencia() {
        return editEmergencia;
    }

    public void setEditEmergencia(String editEmergencia) {
        this.editEmergencia = editEmergencia;
    }

    public String getEditEmail() {
        return editEmail;
    }

    public void setEditEmail(String editEmail) {
        this.editEmail = editEmail;
    }

    public String getEditComplemento() {
        return editComplemento;
    }

    public void setEditComplemento(String editComplemento) {
        this.editComplemento = editComplemento;
    }

    public String getEditCidade() {
        return editCidade;
    }

    public void setEditCidade(String editCidade) {
        this.editCidade = editCidade;
    }

    public void setEditNome(String editNome) {
        this.editNome = editNome;
    }

    public String getEditNascimento() {
        return editNascimento;
    }

    public void setEditNascimento(String editNascimento) {
        this.editNascimento = editNascimento;
    }

    public String getEditCPF() {
        return editCPF;
    }

    public void setEditCPF(String editCPF) {
        this.editCPF = editCPF;
    }

    public String getEditRg() {
        return editRg;
    }

    public void setEditRg(String editRg) {
        this.editRg = editRg;
    }

    public String getEditCelular() {
        return editCelular;
    }

    public void setEditCelular(String editCelular) {
        this.editCelular = editCelular;
    }

    public String getEditLogadouro() {
        return editLogadouro;
    }

    public void setEditLogadouro(String editLogadouro) {
        this.editLogadouro = editLogadouro;
    }

    public String getEditBairro() {
        return editBairro;
    }

    public void setEditBairro(String editBairro) {
        this.editBairro = editBairro;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(editNome);
        parcel.writeString(editNascimento);
        parcel.writeString(editNatural);
        parcel.writeString(editCPF);
        parcel.writeString(editRg);
        parcel.writeString(editCelular);
        parcel.writeString(editEmergencia);
        parcel.writeString(editEmail);
        parcel.writeString(editLogadouro);
        parcel.writeString(editComplemento);
        parcel.writeString(editBairro);
        parcel.writeString(editCidade);
        parcel.writeString(key);
    }

    public void setArguments(Bundle bundle) {
    }
}

In the PatientActivity where I call the method to transfer the data to the Fragments

package com.fisioapp.rodiney.activity;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SearchView;
import android.widget.Spinner;
import android.widget.Toast;

import com.fisioapp.rodiney.Adapter.AdapterExibicaoPaciente;
import com.fisioapp.rodiney.classes.CadastroEvolucao;
import com.fisioapp.rodiney.classes.CadastroPaciente;
import com.fisioapp.rodiney.R;
import com.fisioapp.rodiney.config.ConfiguracaoFirebase;
import com.fisioapp.rodiney.fragment.ProntuarioFragment;
import com.fisioapp.rodiney.helper.Base64Custom;
import com.fisioapp.rodiney.helper.RecyclerItemClickListener;
import com.google.firebase.auth.FirebaseAuth;
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 static java.security.AccessController.getContext;

public class PacienteActivity extends AppCompatActivity {
    private Button botaoNovoPaciente;
    private RecyclerView recyclerViewPaciente;
    private AdapterExibicaoPaciente adapterExibicaoPaciente;
    private List<CadastroPaciente> pacientes = new ArrayList<>();
    private CadastroPaciente chavePaciente;

    //Referencia da base de dados para
    private FirebaseAuth autenticacao = ConfiguracaoFirebase.getFirebaseAutenticacao();
    String idUsuario = Base64Custom.codificarBase64(autenticacao.getCurrentUser().getEmail());
    private DatabaseReference firebaseRef = ConfiguracaoFirebase.getFirebaseDatabase();
    private DatabaseReference movimentacaoRef;;
    private ValueEventListener valueEventListenerMovimentacao;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paciente);
        botaoNovoPaciente = findViewById(R.id.btnNVPaciente);
        //Referencia do RecyclerView
        recyclerViewPaciente = findViewById(R.id.recyclerViewPaciente);


        SearchView busca = new SearchView(this);
        busca = findViewById(R.id.searchView);
        busca.setOnQueryTextListener(new SearchFiltro());


        //Configuracao do Adapter do RecyclerView
        adapterExibicaoPaciente = new AdapterExibicaoPaciente(pacientes, this);


        //Configurar Reclyver
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerViewPaciente.setLayoutManager(layoutManager);
        recyclerViewPaciente.setHasFixedSize(true);
        recyclerViewPaciente.setAdapter(adapterExibicaoPaciente);

        //Evento de Clique Reclyver
        recyclerViewPaciente.addOnItemTouchListener(
                new RecyclerItemClickListener(
                        getApplicationContext(),
                        recyclerViewPaciente,
                        new RecyclerItemClickListener.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {

                                //Modelo com Bundle
                                CadastroPaciente enviarCadastro  = pacientes.get(position);
                                CadastroPaciente cadastroPaciente = new CadastroPaciente();
                                Bundle bundle = new Bundle();
                                bundle.putParcelable("Chave",enviarCadastro);
                                cadastroPaciente.setArguments(bundle);
                                abrirDados();
                                Log.i("Teste", String.valueOf(enviarCadastro.getKey()));

                                Toast.makeText(
                                        getApplicationContext(),
                                        "Paciente: " + enviarCadastro.getEditNome() +  " Selecionado com Clique ",Toast.LENGTH_LONG
                                ).show();
                            }

                            @Override
                            public void onLongItemClick(View view, int position) {
                                CadastroPaciente cadastroPaciente = pacientes.get(position);

                                Toast.makeText(
                                        getApplicationContext(),
                                        "Paciente: " + cadastroPaciente.getEditNome() +  " com Clique Longo",Toast.LENGTH_LONG
                                ).show();

                            }

                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                            }
                        }
                )
        );


        botaoNovoPaciente.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                abrirTelaCadastro();

            }


        });

    }


    private class SearchFiltro implements android.support.v7.widget.SearchView.OnQueryTextListener, SearchView.OnQueryTextListener {

        @Override
        public boolean onQueryTextSubmit(String query) {
            Log.i("Script","onQueryTextSubmit"+query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            Log.i("Script","onQueryTextChanget"+newText);
            return false;
        }
    }

    public void abrirTelaCadastro(){
        startActivity(new Intent(this,PerfilPacienteActivity.class));
        finish();

    }
    public void abrirDados(){
        Intent intent = new Intent(this, PerfilPacienteActivity.class);
        startActivity(intent);
    }


    public void recuperarMovimentacao(){
        String emailUsuario = autenticacao.getCurrentUser().getEmail();
        String idUsuario = Base64Custom.codificarBase64( emailUsuario );
        //movimentacaoRef.child("usuarios").child( idUsuario ).child("Evolucao");
        movimentacaoRef = firebaseRef.child("Profissional")
                .child("Paciente")
                .child(idUsuario);

        valueEventListenerMovimentacao = movimentacaoRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                pacientes.clear();
                for (DataSnapshot dados: dataSnapshot.getChildren() ){

                    CadastroPaciente listaPacientes = dados.getValue(CadastroPaciente.class);
                    listaPacientes.setKey(dados.getKey());//Usando para pegar a Chave do Paciente especifico
                    pacientes.add(listaPacientes);
                }
                adapterExibicaoPaciente.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    @Override
    public void onStart() {
        super.onStart();
        recuperarMovimentacao();
    }

    @Override
    public void onStop() {//Metodo utilizado para fechar o EventListener quando sair da tela Evolucao
        super.onStop();
        // Log.i("Evento","Evento encerrado");
        // usuarioRef.removeEventListener(valueEventListenerEvolucao);
        movimentacaoRef.removeEventListener(valueEventListenerMovimentacao);
    }
}
No Fragment onde quero receber a informação.
 //Recebendo os valores passados como String
        chavePaciente = new CadastroPaciente(Parcel.obtain());
        Bundle bundle = this.getArguments();
        if (bundle != null) {
            chavePaciente= bundle.getParcelable("Chave");
            Log.i("Certo", String.valueOf(chavePaciente.getKey()));
        }else{
          Log.i("Certo", "Erro no recebimento");

        }

Thanks in advance for your attention.

    
asked by anonymous 24.03.2018 / 21:51

0 answers