Recyclerview, problem inserting an item

0

asked by anonymous 16.03.2017 / 17:56

2 answers

0

The RecyclerView does not persist (saves) the data you send it to, when you open another activity, they get lost.

You need to use a database (SQLite is the easiest on Android) to save the form data, and use that same database to popular RecyclerView in the activity that you show it.

Take a look at this tutorial: link

And here in Portuguese: link

I hope it has become clear what is happening, any doubt leaves a comment that I try to explain to you;)

    
16.03.2017 / 18:01
0

You should use the startActivityForResult method instead of startActivity Within the onActivityResult, you program to receive an object of typeContactContact and add it to the list that is inside the adapter. Finally, give a notifyDataSetChanged in the adapter.

In main activity:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 999) {
            if (resultCode == Activity.RESULT_OK) {
                atualizaMeuAdapter((ModelContato)data.getSerializableExtra("novoUser"));
            }
        } 
    }

In the Click button to go to add contact screen

Intent intent = new Intent(MainActivity.this, NewNoteActivity.class);
startActivityForResult(intent, 999);

When you save the user to the add contact screen:

public void salvar(){
    usuario.salvar();

    Intent returnIntent = new Intent();
    intent.putExtra("novoUser", usuario);
    setResult(Activity.RESULT_OK, returnIntent);
    finish();
}

Remembering that the user entity has to implement Serializable. This is the easiest way I believe.

    
16.03.2017 / 18:23