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);
}
}
How is your listview code? Usually what needs to be refreshed is in the listview Adapter, which searches the database data.
– Márcio Oliveira
You can clear the bank before entering the new records
– Leonardo Dias
@Márciooliveira, I edited the question with the code.
– Marceloawq
@Leonardodias, but I need to keep the records that are already in the bank.
– Marceloawq
@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
.– Luídne