update method (sqlite) does not update database data

Asked

Viewed 1,011 times

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);

        }
    });
  • 1

    Why the space after the table name("book")? Why a space before the Id(" "+book.getId())?

  • 1

    In addition that changing the data in the BD may not be sufficient for the Listview be updated.

  • 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?

  • To give you an answer post the code where you are filling the listview for the first time.

  • 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.

2 answers

1


Not updating database data because values passed to method db.update() are bad: remove space on "livro " and " "+livro.getId():

db.update("livro", values, "_id = ? ", new String[]{""+livro.getId()});

In addition, in the method atualizar(View view), you must assign Id to Book that wants to update, so that it is passed to the method db.update():

public void atualizar(View view){
    Livro livro = new Livro();
    livro.setTitulo(edtTitulo.getText().toString());
    livro.setAutor(edtAutor.getText().toString());
    livro.setEditora(edtEditora.getText().toString());

    livro.setId = id;//O id deve ser recuperado do Extra passado à AddUpdateActivity

    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();
    }
}

Changing the data in the BD may not be enough for the Listview is updated, unless you are using Loadermanager together with a Cursorloader.

The way it has structured code it is not easy to put here a solution ready.

What you need to do is inform Bookbinder of the amendments to update the Listview.

There are several ways, one possible is to declare a method in the Bookbinder updating the internal list.

public void actualizaLista(List<Livro> lista){

    this.nomeDaSuaLista = lista; 
    notifyDataSetChanged();
}

After the data is changed in the BD call so:

List<Livro> lista = livroCRUD.buscarTodos();
lvPrincipal.getAdapter().actualizaLista(lista);
  • i was already searching for a way to update the list after an insertion or change, thanks for the tips but unfortunately even removing the spaces it seems that the database does not update by the update method of my code.

  • See the Reply Edition.

  • I was finally able to update, thanks for the @ramaral tips, what I did was instantiate my book class out of all methods, I don’t know if global is the correct term. now I will search on the listview update at the time of change by the tips you gave me, thanks

  • I’m having some difficulty with the notifyDataSetChanged();, I created the method atualizalista in the adapter, but the line lvPrincipal.getAdapter().actualizaLista(lista); doesn’t work. I put her in onResume because when I add or update a data, my app goes back to Activity containing the list.

  • Before calling the method is doing List<Livro> lista = livroCRUD.buscarTodos();?

  • yes, the problem is that after the .getAdapter() it does not detect my method atualizaLista(lista) of LivroAdapter.

  • Without knowing this new context it is difficult to say anything. You better ask another question by putting the relevant part of the code.

Show 2 more comments

-2

When you will pass the parameter is informing an array, but it is using " = ? " removes this "new String", getting:

db.update("livro ", values, "_id = ? ", livro.getId());
  • 2

    That’s not gonna compile!

  • really update method requires a whereArgs, without the array does not compile

Browser other questions tagged

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