刪除行我試圖刪除一行給出了具體的ID,看到反映在我的列表視圖更新,但該行不會被刪除。同樣的過程適用於update
,但不能刪除。下面是代碼:該項目的Android SQLite的不是ID
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
的條目,那麼我將刪除從數據庫中的任何一個條目。如果我不嘗試先更新,那我就不能。
這裏是我的表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值。
你知道,如果db.delete實際上是被稱爲? – M0rty
是啊,是被稱爲 –
[Rü確保您的物品點擊ID傳遞.. ??? ....而且這裏留出空間.....「_id =?」 –