Recyclerview, problem inserting an item

Asked

Viewed 604 times

1

Talk guys, I’m new on android, I have a problem here, I’m trying to insert items in a Recyclerview, when I use the direct method on the button it is inserting statically good:

public void listenersButtons() {

                floatingActionButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                         // Cria uma nova pessoa

                        ModelContato pessoa1 = new ModelContato();
                        pessoa1.setNome("Felipe");

                        //Adiciona a pessoa1 e avisa o adapter que o conteúdo
                        //da lista foi alterado

                        pessoasListas.add(pessoa1);
                        adapter.notifyDataSetChanged();
                    }
                });
            }

But now I need to evolve, I want that when click on that same button open a screen with a form and a save button, there I capture the dice and play back to the main.

I’m doing like this:

public void listenersButtons() {

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

                   Intent it = new Intent(PrincipalActivity.this, Contato.class);
                   startActivity(it);
                   finish();

                }
            });
        }

And from there returns the Strings and I’m trying to insert in Recyclerview like this:

@Override
            protected void onResume() {
                super.onResume();

            Intent itt = getIntent();

            if (itt != null){
                Bundle parametro = itt.getExtras();


                if (parametro != null){

                    ModelContato pessoa1 = new ModelContato();

                    if (parametro.getString("nome") != pessoa1.getNome()) {

                        String nome = parametro.getString("nome");
                        String email = parametro.getString("email");
                        String num = parametro.getString("num");

                        pessoa1.setNome(nome);
                        pessoa1.setEmail(email);
                        pessoa1.setNumero(num);

                        pessoasListas.add(pessoa1);
                        adapter.notifyDataSetChanged();

                    }
                }
            }
        }

The problem is that now when I return it fills the recycleview correctly with the values I filled in the form, only when doing the process again it replaces, that is, it always stays only 1 item in my list.

I put inside onResume for trial and error, I also tested on onCreate and the same thing happens.

Another detail is that when browsing the application, when I go on another Activity and go back to the main a new item is added, same as the previous one, I tried to fix this with the last if of Cod on onResume, but it didn’t work.

No mistake, but it doesn’t work the way I need it.

Thanks in advance, vlw!

2 answers

1

Recyclerview does not persist (save) the data that Voce sends to it, when Voce opens another Activity, they get lost.

You need to use a database (Sqlite is the easiest on Android) to save the data form, and use this same database to popular the Recyclerview in Activity Voce shows it.

Take a look at this tutorial (in English): http://androidheight.blogspot.com.br/2015/11/sqlite-database-in-android-application.html

And this one in Portuguese: http://www.devmedia.com.br/criando-um-crud-com-android-studio-e-sqlite/32815

I hope it’s clear what’s going on, any doubt leaves a comment that I try to explain to you ;)

  • Thanks for the stuff.

0

You should use startActivityForResult instead of startActivity Within onActivityResult, you program to receive an object of type Modelcontact and add in the list that is within Adapter. Finally, give a notifyDataSetChanged on the Adapter.

Na 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"));
            }
        } 
    }

Click the button to go to the contact add screen

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

When Voce saves the user on 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.

Browser other questions tagged

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