Go back to another activity and pass parameters

0

I'm having a problem adding items to a ListView , I have a activity main that calls another to add the met

package com.example.jonathan.agendaonline;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.Profile;
import com.facebook.login.LoginManager;

import java.io.Serializable;
import java.util.ArrayList;

import static android.R.layout.simple_list_item_1;

public class TodosEventos extends AppCompatActivity {

    Button novoevento;
    TextView textoNome;
    ListView listaeventos;
    Button sair;
    ArrayList<String> eventos;
    ArrayAdapter<String> adapter;
    String usuarioUsado = null;
    String idUsuario = null;
    String usuarioUsado2 = null;
    Bundle extras;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_todos_eventos);
        novoevento = (Button) findViewById(R.id.Novo);
        textoNome = (TextView) findViewById(R.id.textViewNome);
        sair = (Button) findViewById(R.id.sairTodosEventos);
        listaeventos = (ListView) findViewById(R.id.listaEventos);
        eventos = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,eventos);
        listaeventos.setAdapter(adapter);

         extras = getIntent().getExtras();
         if(extras != null && extras.containsKey("usuario")) {
            usuarioUsado = (String) extras.getSerializable("usuario");
            idUsuario = (String) extras.getSerializable("IdFacebook");
            textoNome.setText(usuarioUsado);
        }


        String nomeEvento = null;
        if(extras != null && extras.containsKey("NomeEvento")) {
            nomeEvento = (String) extras.getSerializable("NomeEvento");
            usuarioUsado2 = (String) extras.getSerializable("usuario2");
            textoNome.setText(usuarioUsado2);
            Toast.makeText(TodosEventos.this, "Evento"+" "+nomeEvento+" "+"Criado com sucesso", Toast.LENGTH_SHORT).show();
            eventos.add(nomeEvento);
            adapter.notifyDataSetChanged();

        }

        listaeventos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(TodosEventos.this,Eventos.class);
                startActivity(intent);
            }
        });


        sair.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TodosEventos.this, LoginActivity.class);
                LoginManager.getInstance().logOut();
                startActivity(intent);
                finish();
            }
        });

        final String finalIdUsuario = idUsuario;
        novoevento.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(TodosEventos.this, CriacaoDeEventos.class);
                intent.putExtra("IdFacebook", finalIdUsuario);
                startActivity(intent);


            }
        });


    }

    @Override
    protected void onResume() {

        String nomeEvento = null;
        if(extras != null && extras.containsKey("NomeEvento")) {
            nomeEvento = (String) extras.getSerializable("NomeEvento");
            Toast.makeText(TodosEventos.this, "Evento"+" "+nomeEvento+" "+"Criado com sucesso", Toast.LENGTH_SHORT).show();
            eventos.add(nomeEvento);
            adapter.notifyDataSetChanged();

        }
        super.onResume();

    }  
}

This is what should get the data and add the items in the list, but the intent is not passing:

package com.example.jonathan.agendaonline;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;

import static android.R.layout.simple_list_item_1;

public class CriacaoDeEventos extends AppCompatActivity {

    Button criar;
    EditText nome,dataInicio,dataFim,hora,horaAlerta;
    public String nomeString;
    String dataInString;
    String dataFimString;
    String horaString;
    String horaAlertString;




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

        nome = (EditText) findViewById(R.id.nomeEvento);
        dataInicio = (EditText) findViewById(R.id.dataInicio);
        dataFim = (EditText) findViewById(R.id.dataFim);
        hora = (EditText) findViewById(R.id.hora);
        horaAlerta = (EditText) findViewById(R.id.horaAlerta);


        criar = (Button) findViewById(R.id.Criar);
        criar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                nomeString = nome.getText().toString();
                dataInString = dataInicio.getText().toString();
                dataFimString = dataFim.getText().toString();
                horaString = hora.getText().toString();
                horaAlertString = horaAlerta.getText().toString();

                Bundle extras = getIntent().getExtras();
                String idUsuario = null;
                String nomeUsuario = null;
                if(extras != null && extras.containsKey("idFacebook")) {
                    idUsuario = (String) extras.getSerializable("idFacebook");

                }

                RadioGroup reg = (RadioGroup) findViewById(R.id.rgopcoes);
                int op = reg.getCheckedRadioButtonId();

                if(op == R.id.diario){
                    Evento evento = new Evento(nomeString,dataInString,dataFimString,horaString,horaAlertString,1,idUsuario);

                }else{

                    Evento evento = new Evento(nomeString,dataInString,null,horaString,horaAlertString,1,idUsuario);

                }
                Intent intent = new Intent(CriacaoDeEventos.this, TodosEventos.class);
                intent.putExtra("NomeEvento",nomeString);
                intent.putExtra("usuario2",nomeUsuario);
                onBackPressed();

            }

        });




    }




}
    
asked by anonymous 24.11.2016 / 01:19

1 answer

1

The problem is this:

if(extras != null && extras.containsKey("idFacebook")) {
    idUsuario = (String) extras.getSerializable("idFacebook");
}

You use the string " i dFacebook" when fetching the key in intent , but at the time of passing Intent you call the " I dFacebook. "

Replace " idFacebook " with " IdFacebook " that will resolve your issue.

Consider declaring a constant in your Activity CriacaoDeEventos so that this type of problem does not happen.

    
24.11.2016 / 14:23