Pass data to a listview in another Canvas Activity

Asked

Viewed 944 times

0

I have a college exercise, I have to fill name field, age and Cpf when you click the "register" button it sends the data to another Activity screen where will be added the registration names in a listview, is in this part I stuck. I’ve already made an array list the main class but don’t know how to send this araylist to another screen to populate the listview there.

public class MainActivity extends MenuActivity {

private EditText nome,idade,cpf;
private Button btncadastra;
String  Nome,Idade,Cpf;
ArrayList<Pessoa> lista = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    nome = (EditText)findViewById(R.id.txNome);
    cpf = (EditText)findViewById(R.id.txtCPF);
    idade = (EditText)findViewById(R.id.txtIdade);
    btncadastra = (Button)findViewById(R.id.btnCadastrar);


    btncadastra.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {




            if(nome.getText().length() == 0){
                AlertDialog.Builder alertaNome = new AlertDialog.Builder(MainActivity.this);
                alertaNome.setIcon(android.R.drawable.stat_sys_warning);
                alertaNome.setTitle("Alerta");
                alertaNome.setMessage("O campo nome não foi preenchido.");
                alertaNome.setNeutralButton("OK",null);
                alertaNome.setIcon(R.mipmap.ic_launcher);
                alertaNome.show();
            }
            if(cpf.getText().length() == 0){
                AlertDialog.Builder alertaCPF = new AlertDialog.Builder(MainActivity.this);
                alertaCPF.setIcon(android.R.drawable.stat_sys_warning);
                alertaCPF.setTitle("Alerta");
                alertaCPF.setMessage("O campo CPF não foi preenchido.");
                alertaCPF.setNeutralButton("OK",null);
                alertaCPF.setIcon(R.mipmap.ic_launcher);
                alertaCPF.show();
            }
            if(idade.getText().length() == 0){
                AlertDialog.Builder alertaIdade = new AlertDialog.Builder(MainActivity.this);
                alertaIdade.setIcon(android.R.drawable.stat_sys_warning);
                alertaIdade.setTitle("Alerta");
                alertaIdade.setMessage("O campo Idade não foi preenchido.");
                alertaIdade.setNeutralButton("OK",null);
                alertaIdade.setIcon(R.mipmap.ic_launcher);
                alertaIdade.show();
            }

             Intent i = new Intent(MainActivity.this, ListaPessoa.class);

             Pessoa pessoa = new Pessoa();
             pessoa.setNome(nome.getText().toString());
             pessoa.setCpf(cpf.getText().toString());
             pessoa.setIdade(idade.getText().toString());


            lista.add(pessoa);
            startActivity(i);

        }
    });

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

}

Screen that lists registered persons:

public class ListaPessoa extends MenuActivity {

private ListView listaPessoas;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista_pessoa);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    listaPessoas = (ListView)findViewById(R.id.lvPessoas);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

}

1 answer

1

Hello,

The simplest way is to make the person class implement Serializable after this save the person in the Internet before calling the other Action

i.putExtra("pessoa", pessoa); 
startActivity(it);

To recover in the other Activity

    Pessoa pessoa = (Pessoa) getIntent().getSerializableExtra("pessoa");
  • that I add inside the button event?

  • The first Voce code puts on the button that calls the second Activity The second to retrieve the person object can be on onCreate

Browser other questions tagged

You are not signed in. Login or sign up in order to post.