Update BD after insert record

Asked

Viewed 142 times

0

I use the following code to enter a record in sqlite:

    public void InsereSenha(String Nome, String Senha, String Dica, String Anotacao) {
    String sql = "INSERT INTO Senha (Nome, Senha, Dica, Anotacao) VALUES (?, ?, ?, ?)";
    try
    {
        database.execSQL(sql, new String[]{Nome, Senha, Dica, Anotacao});
    }
    catch (Exception ex)
    {

    }

}

It inserts the record, but when giving the select to take all the records of this table and show in a listview, the new registration, which I just registered, does not appear. How to give a "refresh" in the comic book to get the new record?

Edit 1:

The Adapter that was supposed to display the records I just entered:

        final DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
    databaseAccess.open();
    List<Senha> senhas = databaseAccess.getSenha();
    databaseAccess.close();

    CustomAdapter adapter = new CustomAdapter(getApplicationContext(), senhas);

    Lista.setAdapter(adapter);

Edit 2:

Based on Márcio Oliveira’s answer, I redid the code. That’s how it’s working:

public class MainActivity extends AppCompatActivity {

ListView Lista;
List<Senha> senhas;
CustomAdapter adapter;
final DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);


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

    Lista = (ListView)findViewById(R.id.Lista_Senhas);

    databaseAccess.open();
    senhas = databaseAccess.getSenha();
    databaseAccess.close();

    if (adapter == null) {
        adapter = new CustomAdapter(getApplicationContext(), senhas);
    }else {
        adapter.notifyDataSetChanged();
    }

    Lista.setAdapter(adapter);

}

}

  • 1

    How is your listview code? Usually what needs to be refreshed is in the listview Adapter, which searches the database data.

  • You can clear the bank before entering the new records

  • @Márciooliveira, I edited the question with the code.

  • @Leonardodias, but I need to keep the records that are already in the bank.

  • @Marceloawq make sure that the data is, in fact, being persisted. Use http://facebook.github.io/stetho/ or any app that reads the app databases on your device. Debug or print something on catch.

1 answer

2


In the database you don’t have to do anything. The way you implemented it, just reload your password list (List<Senha> senhas) and notify Adapter that the list has been updated with a adapter.notifyDataSetChanged().

Consider using a Cursorloader/Cursoradapter in the listview to access the database directly instead of loading data each time to an array. Depending on the amount of data, you can slow down or crash the interface.

  • Could you give an example? It was not clear how to call the notifyDataSetChanged()

  • Right after the code you give Insert, inside Try, you rewrite the variable passwords and call Adapter.notifyDataSetChanged(). I’m guessing all this is in the same activity. If it is not, put the complete code, both from the part you insert and where you load Listview, which is easier to explain.

  • I edited the answer, with the code working.

Browser other questions tagged

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