Problem with database update (Android)

0

I can not update the data that is in the database.

In the database class are the two functions refresh and searchConfigurations.

public void atualizar(MessageEB dados){
    ContentValues valores = new ContentValues();
    valores.put("data", dados.getData());

    bd.update("dados", valores, "_id = ?", new String[]{"" + dados.getId()});
    Log.i("a", dados.getData());

    bd.close();
}


public MessageEB buscarConfiguracoes() {
    MessageEB messageEB = new MessageEB();
    String sqlSelectTodosLivros = "SELECT * FROM dados";

    Cursor c = bd.rawQuery(sqlSelectTodosLivros, null);

    if(c.moveToNext()) {
        messageEB.setId(c.getLong(0));
        messageEB.setData(c.getString(1));
        Log.i("LOG", "funcao buscar");
    }

    Log.i("LOG", "fora do if "+(c.getString(1)));

    bd.close();
    return messageEB;
}

In the fragment where EditText has this code inside one:

public void onClick(View v) {


        MessageEB dados = new MessageEB();
        dados.setData(editar.getText().toString());
        BD bd = new BD(getActivity());
        bd.atualizar(dados);

    }

In the fragment that will display the data:

txt = (TextView) view.findViewById(R.id.campoData2);
    BD db = new BD(getActivity());
    String s = db.buscarConfiguracoes().getData();
    txt.setText(s);

When the data passed through the function refresh the data is changed, I saw this with a Log, but when it passes through the getConfigurations () the received data is the same as before the change.

    
asked by anonymous 23.04.2015 / 23:06

1 answer

0

You are creating a new instance of MessageEB , assigning it the editText value, and then invoking the method to update the data.

However, this method needs the Id value, which is not "set" in dados . You will need to change the code to assign the Id of the record you want to change.

public void onClick(View v) {

    MessageEB dados = new MessageEB();
    dados.setData(editar.getText().toString());

    dados.setId = //Não sei onde você o guardou

    BD bd = new BD(getActivity());
    bd.atualizar(dados);
}
    
23.04.2015 / 23:27