无法更新数据库中的项目[sqlite]

时间:2016-11-08 13:42:21

标签: java android sqlite

无法在代码中找到错误。我敢肯定,ID​​可能存在一些问题。也许查询错了。有点新的东西。

MyDBHelper

public void onClick(DialogInterface dialog, int whichButton) {
                String EditTextValue = edittext.getText().toString();
                Task t = new Task(EditTextValue);
                dbHandler.updateTask(t);


                if (dbHandler.updateTask(t) == true){
                    Toast.makeText(MainActivity.this, "IR", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(MainActivity.this, "naah", Toast.LENGTH_LONG).show();
                }
                  displayTaskList();
             }

更新功能:

 public boolean updateTask(Task t){
    boolean result = false;
    String q = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_ID + " = " + t.getID();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c = db.rawQuery(q, null);
    if (c.moveToFirst())
    {
        String q2 = "UPDATE " + TABLE_NAME + " SET " + COLUMN_TASK + " = "
                + t.getTASK() + " WHERE " + COLUMN_ID + " = " + t.getID();
        db.execSQL(q2);
        result = true;
    }
    db.close();
    return result;
}

任务类:

public class Task {
private int _id;
private String _task;

public Task() {}
public Task(String task){
    this._task = task;
}
public Task (int id, String task){
    this._id = id;
    this._task = task;
}

// SETS&GETS
public void setID(int id) {
    this._id = id;
}
public int getID() {
    return this._id;
}
public void setTASK(String task){
    this._task = task;
}
public String getTASK(){
    return this._task;
}
}

Mainactivity:

// To detect which list item is selected (by ID)
private long _id;
public void setID(long id) { this._id = id; }
public long getID() { return this._id; }

// Attaches a long click listener to the listview
private void setupListViewListener() {
    lvTasks.setOnItemLongClickListener(
            new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> adapter,
                                               View item, int pos, long id) {
                    item1.setVisible(true); item2.setVisible(true); item3.setVisible(true); // Showing all buttons
                    setID(id); //Catches the selected items ID
                    return true;
                }

            });
}

问题可能是它混淆了ID?例如,在Mainactivity中,我获得的ID是任务(onlongclick),但在Task / MyDBHelper类ID中是新创建的任务 - 即使它们没有连接(?)。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

您可以使用这样的更新功能:

 public int updateTask(Task t){
 //i assume you open your db here
 SQLiteDatabase db = this.getWritableDatabase();

 //put values that needs to be updated in values with column names and  values
 ContentValues values = new ContentValues();
 values.put(COLUMN_TASK,t.getTASK());
 values.put(COLUMN_ID,t.getID());

 // db.update(String table,ContentValues values,String whereClause,String[] whereArguments)
 int result = db.update(TABLE_NAME,values,COLUMN_ID+"=?"+new String[] {String.ValueOf(t.getID())});
 db.close();
 return result;
 }

我想这可以解决你的问题。