从SQLite和List OnClick中删除ListView项

时间:2018-02-17 05:40:12

标签: android sqlite listview

当我点击添加到列表中的每个项目时,我无法从列表和SQLite数据库中删除ListView项目。当我点击每个单独的项目时,我的列表或数据库没有任何反应。以下是我到目前为止的情况:

我的数据库助手:

Enum.zip(columns, rows) |> Enum.into(%{})

}

我的主要活动方法,点击时删除项目。

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.database.Cursor;

public class ExerciseDatabaseHelper extends SQLiteOpenHelper
{
private static final String TAG = "ExerciseDatabaseHelper";
private static final String TABLE_NAME = "exercise_table";
private static final String COL1 = "ID";
private static final String COL2 = "exercise";

public ExerciseDatabaseHelper(Context context)
{
    super(context, TABLE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db)
{
    String createTable = "Create Table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COL2 + " TEXT)";
    db.execSQL(createTable);

}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1)
{
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean addData(String item)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, item);
    Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if(result == -1)
    {
        return false;
    }
    else
    {
        return true;
    }
}

public void deleteRow(long id )
{
    SQLiteDatabase db = this.getWritableDatabase();
   db.delete(TABLE_NAME,COL2 + "=?", new String[] {String.valueOf(id)});
   db.close();


}
public Cursor getData()
{
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_NAME;
    Cursor data = db.rawQuery(query, null);
    return data;
}

0 个答案:

没有答案