我正在尝试删除给定特定ID的行,并查看我的listview中反映的更新,但该行未被删除。同一过程适用于update
,但不适用于删除。这是代码:
MainActivity.java
...
@Override
public void onPositiveClick(DialogFragment dialog, int which) {
//Shared Preferences
SharedPreferences pref = getSharedPreferences(EditLog.PREF_FILENAME, 0);
long id = 0;
String date = "";
String hours = "";
String lessonType = "";
String weatherCondition = "";
if (!(pref == null)) {
id = pref.getLong("rowId", 0);
date = pref.getString("date", "");
hours = pref.getString("hours", "");
lessonType = pref.getString("lessonType", "");
weatherCondition = pref.getString("weatherCondition", "");
}
if (which == 2) {
LessonLogDBHelper dbhelp = new LessonLogDBHelper(getApplicationContext());
final SQLiteDatabase db = dbhelp.getWritableDatabase();
db.delete(LessonLogContract.LogEntry.TABLE_NAME, "_id=?", new String[]{Long.toString(id)});
} else {
LessonLogDBHelper dbhelp = new LessonLogDBHelper(getApplicationContext());
final SQLiteDatabase db = dbhelp.getWritableDatabase();
final ContentValues values = new ContentValues();
values.put(LessonLogContract.LogEntry.CN_DATE, date);
values.put(LessonLogContract.LogEntry.CN_HOURS, hours);
values.put(LessonLogContract.LogEntry.CN_LESSON_TYPE, lessonType);
values.put(LessonLogContract.LogEntry.CN_WEATHER, weatherCondition);
db.update(LessonLogContract.LogEntry.TABLE_NAME, values, "_id=" + id, null);
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_main, new DrivingLog());
transaction.commit();
}
...
如果= = 2,这意味着用户点击了delete
按钮,则点击了项目的id(在数据库中),并且我呼叫delete
。此过程适用于更新,但不能删除。
编辑:整洁的事情
我发现奇怪的是,如果我尝试update
一个条目,那么我将从db中删除任何单个条目。如果我不先尝试更新,那么我就不能。
这是我的表格def:
LessonLogContract
package com.example.name.drivinglessona3;
import android.provider.BaseColumns;
import android.util.StringBuilderPrinter;
public class LessonLogContract {
private LessonLogContract() {}
public static class LogEntry implements BaseColumns {
public static final String TABLE_NAME = "lesson";
public static final String CN_DATE = "date";
public static final String CN_HOURS = "hours";
public static final String CN_LESSON_TYPE = "lesson_type";
public static final String CN_WEATHER = "weather";
}
}
LessonLogDBHelper
package com.example.name.drivinglessona3;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class LessonLogDBHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + LessonLogContract.LogEntry.TABLE_NAME + " (" +
LessonLogContract.LogEntry._ID + " INTEGER PRIMARY KEY," +
LessonLogContract.LogEntry.CN_DATE+ " TEXT," +
LessonLogContract.LogEntry.CN_HOURS+ " TEXT," +
LessonLogContract.LogEntry.CN_LESSON_TYPE+ " TEXT," +
LessonLogContract.LogEntry.CN_WEATHER + " TEXT)";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + LessonLogContract.LogEntry.TABLE_NAME;
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "LogEntry.db";
public LessonLogDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
解决
当我点击我的删除按钮时,我没有将_id
保存到SharedPreferences,因此在MainActivity
中我得到了错误的id值。
答案 0 :(得分:0)
试试这个:
public void delete(int id) {
SQLiteDatabase db = getWritableDatabase();
db.delete("MST_Item", "ItemID=" + id, null);
db.close();
}
答案 1 :(得分:0)
只需在删除行中更改 String.valueOf()而不是Long.toString()。 这意味着用以下代码替换db.delete()行:
subscribe()
我希望这种方法适用于你。