Sending data between View and Edit Activity

Asked

Viewed 278 times

2

I am developing a service scheduling system and in a Listview I can take a customer’s data and move to a Query Activity. I created an editing menu and would like to pass this same data to the new Activity, but I can only open the Activity without passing the data of this client.

Can you help me?

The code is attached. In this Activity he selects an item from the list and plays the client object for a second Activity, which serves only to query the data:

public class AgendadoDiaActivity extends AppCompatActivity {

private ListView listadeAtendimentos;

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

    listadeAtendimentos = (ListView) findViewById(R.id.lista_atendimentos);

    listadeAtendimentos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> lista, View item, int position, long id) {
            Clientes cliente = (Clientes) listadeAtendimentos.getItemAtPosition(position);
            Intent pesquisa = new Intent(AgendadoDiaActivity.this, ViewCadastroAtendimentosActivity.class);
            pesquisa.putExtra("cliente", cliente);
            startActivity(pesquisa);
        }
    });

In onCreate of this Activity it takes the object I sent in the extra and populates through the method filler() the items in the list.

public class Viewcadastromeets Activity extends Appcompatactivity {

private HelperViewCadastroAtendimentos helper;
private HelperCadastroAtendimentos helperCadastro;
private Clientes cliente;
private Intent pesquisa;

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

    helper = new HelperViewCadastroAtendimentos(this);

    pesquisa = getIntent();
    cliente = (Clientes) pesquisa.getSerializableExtra("cliente");
    if(cliente != null){
        helper.preencheFormulario(cliente);
    }
}

In this Activity has an edit button, which calls another edit Activity. It is in it that I cannot take these same dice and play the values of the comic.

  • 1

    To use putExtra() with class Customers it should implement the Serializable interaction or preferably the Parcelable interface, see in this reply how to implement it.

2 answers

3

To complement the @Leonardo Dias answer the use of the code passing the extra by Intent and more advised to do for primitive data such as int, byte, short, double and long and String that is an object but in this case specific and treated as primitive type. When it is necessary to send an object it is more advisable to use it as follows:

Bundle bundle = new Bundle();
bundle.putSerializable("value", SeuObjeto);
intent.putExtras(bundle);

....

Remembering that your object must implement Serializable.

1

Raul, show your code, it’s better so we can help.

Here is an example of how to pass data between Activitys:

Intent intent = new Intent(LoginActivity.this, UserSignupActivity1.class);
intent.putExtra("user_name", nomeUser);
intent.putExtra("user_mail", emailUser);
intent.putExtra("facebookId", userId);
startActivity(intent);

And here is an example of how to receive in the second Activity:

Bundle extras = getIntent().getExtras();
if(extras != null){
     Intent in = getIntent();
     String nomeUserFacebook = in.getStringExtra("user_name");
     String emailUserFacebook = in.getStringExtra("user_mail");
     String idUserFacebook = in.getStringExtra("facebookId");
}
  • Why check if 'extras != null' if for example always the parameter will be passed?

  • 1

    It may be that of some fault in the time to pass the data, or some data did not catch, only for guarantee even, to prevent the crash app, but is not mandatory

  • I get it........

  • I edited my question and posted the code...!

Browser other questions tagged

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