gpt4 book ai didi

java - 如何通过ID从SQLITE数据库中删除数据

转载 作者:行者123 更新时间:2023-12-02 09:01:06 30 4
gpt4 key购买 nike

我在找出如何使用 ID 从我的 sqlite 数据库中删除数据时遇到了很多麻烦。如何使用 ID 从我的 sqlite 数据库中删除数据?

final ItemTouchHelper.SimpleCallback itemTouchHelper = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}



@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
list.remove(viewHolder.getAdapterPosition());
adapter.notifyDataSetChanged();

/////// I want to delete this data from my sqlite Data Base by using it's id. But how do I get the id?///////

}
};

应该在 OnBindViewHolder 中完成吗?

 @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.notePadTextView.setText(arrayListNote.get(position).getNote());
}

这是我的自定义适配器:

公共(public)类NotesCustomAdapter扩展了RecyclerView.Adapter{ 私有(private)ArrayList arrayList注意; 私有(private)Context上下文;

public NotesCustomAdapter(ArrayList<newNote> arrayListNote, Context context) {
this.arrayListNote = arrayListNote;
this.context = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notepad_model,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.notePadTextView.setText(arrayListNote.get(position).getNote());
}

@Override
public int getItemCount() {
return arrayListNote.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{

LinearLayout NotePadMode;
TextView notePadTextView;

public ViewHolder(@NonNull View itemView) {
super(itemView);

NotePadMode= itemView.findViewById(R.id.NotePadModel);
notePadTextView = itemView.findViewById(R.id.notePadTextView);
}
}

}

这是我的 SQlite 数据库:

public class DataBaseHelper extends SQLiteOpenHelper {
public static final String DATABSE_NAME = "AllWorkHours.db";
public static final String TABLE_NAME = "ALLWORKHOURS";
public static final String COL_0 = "ID";
public static final String COL_1 = "DATE";
public static final String COL_2 = "TIMESHIFTSTART";
public static final String COL_3 = "TIMESHIFTENDS";
public static final String COL_4 = "NOTES";
public static final String COL_5 = "NOTEMEMOS";
public static final int DATABASE_Version = 5;

public DataBaseHelper(Context context) {
super(context, DATABSE_NAME,null,DATABASE_Version);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, DATE TEXT, TIMESHIFTSTART INTEGER, TIMESHIFTENDS TEXT, NOTES TEXT, NOTEMEMOS TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS ALLWORKHOURS");
onCreate(db);
}

public boolean addHours(String Date, String TimeShiftStart, String TimeShiftEnds, String Notes){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, Date);
contentValues.put(COL_2, TimeShiftStart);
contentValues.put(COL_3, TimeShiftEnds);
contentValues.put(COL_4, Notes);
long inserted = db.insert(TABLE_NAME,null,contentValues);

if (inserted == -1){
return false;
}else{
return true;
}
}

public ArrayList<newShift> viewAllHours(){
ArrayList<newShift> arrayList = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);

while(cursor.moveToNext()){
int id = cursor.getInt(0);
String Date = cursor.getString(1);
String timestart = cursor.getString(2);
String timeEnds = cursor.getString(3);
String notes = cursor.getString(4);

newShift newShift = new newShift(id,Date,timestart,timeEnds,notes);

arrayList.add(newShift);
}
return arrayList;
}

public boolean addNotes(String NOTEMEMOS) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_5, NOTEMEMOS);
long inserted = db.insert(TABLE_NAME, null, contentValues);

if (inserted == -1){
return false;
}else{
return true;
}
}

public ArrayList<newNote> ViewAllNotes() {
ArrayList<newNote> arrayList = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT NOTEMEMOS FROM " + TABLE_NAME, null);


while(cursor.moveToNext()){
String notes = cursor.getString(0);
newNote newnote = new newNote(notes);
arrayList.add(newnote);
}
return arrayList;
}

}

最佳答案

你可以试试这个

db.delete(table, whereClause, whereArgs)

删除条目后,刷新 View ( ListView )

关于java - 如何通过ID从SQLITE数据库中删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60144831/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com