0
I have a listview that displays the items of the database with an update button, this button calls the screen with the values set in editText, I edit, click save, it returns me the updated message successfully, but does not update the data, they continue to appear equal in listview.
method that receives the values and calls the updated method of the bank:
public void atualizar(View view){
Livro livro = new Livro();
livro.setTitulo(edtTitulo.getText().toString());
livro.setAutor(edtAutor.getText().toString());
livro.setEditora(edtEditora.getText().toString());
LivroCRUD livroCRUD = new LivroCRUD(this);
try {
livroCRUD.atualizar(livro);
Toast.makeText(this, "Livro Atualizado com sucesso!!!!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Não foi possivel atualizar.....!!!!", Toast.LENGTH_LONG).show();
}
}
method that updates the information in the bank:
public void atualizar(Livro livro) throws Exception{
ContentValues values = new ContentValues();
values.put("titulo ", livro.getTitulo());
values.put("autor ", livro.getAutor());
values.put("editora ", livro.getEditora());
db.update("livro ", values, "_id = ? ", new String[]{" "+livro.getId()});
}
method filling in the list:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LivroCRUD livroCRUD = new LivroCRUD(this);
try {
List<Livro> lista = livroCRUD.buscarTodos();
ListView lvPrincipal = (ListView) findViewById(R.id.lvPrincial);
lvPrincipal.setAdapter(new LivroAdapter(this, lista));
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Não foi possivel carregar a lista.", Toast.LENGTH_SHORT).show();
}
}
update button click method on Adapter, it takes the values and passes to Activity "Addupdateactivity"
Button btnAtualizar = (Button) layout.findViewById(R.id.btnChamaAtualizar);
btnAtualizar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Livro livro = new Livro();
Intent intent = new Intent(context, AddUpdateActivity.class);
intent.putExtra("titulo", lista.get(auxPosition).getTitulo());
intent.putExtra("autor", lista.get(auxPosition).getAutor());
intent.putExtra("editora", lista.get(auxPosition).getEditora());
intent.putExtra("_id", lista.get(auxPosition).getId());
context.startActivity(intent);
}
});
Why the space after the table name("book")? Why a space before the Id(" "+book.getId())?
– ramaral
In addition that changing the data in the BD may not be sufficient for the Listview be updated.
– ramaral
removed the spaces, and actually the listview does not update on time, I have to go out and go back to the app, to see the entered and removed data, but change doesn’t really work, there is some other way to write the arguments of the update method?
– Vinicius
To give you an answer post the code where you are filling the listview for the first time.
– ramaral
I added the Activity oncreate that always fills the list with all database data , and the update button method that takes the values and passes to the addition Activity.
– Vinicius