sqlite - recyclerview sqlite 数据库,recyclerview 上删除的数据但数据库中没有删除
问题描述
我想删除 recyclerview 和 sqlite 数据库,但唯一的 recyclerview 被删除。提交新记录时,已删除的记录可见。SQLite 数据库中的数据不会被删除。如何在 android 中使用 recyclerview 删除 SQLite 数据库中的数据。
这是我的代码。适配器类
package com.example.recyclerviewsqlite;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHoder> {
private ArrayList<Model> modelArrayList;
private Context context;
DBmain dBmain;
public MyAdapter(ArrayList<Model> modelArrayList, Context context) {
this.modelArrayList = modelArrayList;
this.context = context;
}
@NonNull
@Override
public MyAdapter.ViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.singledata, parent, false);
return new ViewHoder(view);
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.ViewHoder holder, int position) {
Model model = modelArrayList.get(position);
holder.txtname.setText(model.getSname());
holder.txtsub.setText(model.getSsubject());
//delete data
holder.txtimag.setOnClickListener(new View.OnClickListener() {
int newPosition = holder.getAdapterPosition();
@Override
public void onClick(View v) {
dBmain = new DBmain(context);
dBmain.delete(newPosition);
modelArrayList.remove(newPosition);
notifyItemRemoved(newPosition);
notifyItemRangeChanged(newPosition, modelArrayList.size());
}
});
}
@Override
public int getItemCount() {
return modelArrayList.size();
}
public class ViewHoder extends RecyclerView.ViewHolder {
private TextView txtname, txtsub;
private ImageView icon, txtimag;
public ViewHoder(@NonNull View itemView) {
super(itemView);
txtname = (TextView) itemView.findViewById(R.id.txtname);
txtsub = (TextView) itemView.findViewById(R.id.txtsub);
icon = (ImageView) itemView.findViewById(R.id.icon);
txtimag = (ImageView) itemView.findViewById(R.id.txtimg);
}
}
}
数据库类
package com.example.recyclerviewsqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class SqliteHelper extends SQLiteOpenHelper {
public static final String DBNAME="student";
public static final String TABLENAME="college";
public static final int VER=1;
public SqliteHelper(@Nullable Context context) {
super(context, DBNAME, null, VER);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query="create table "+TABLENAME+"(id integer primary key, name text, subject text)";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query="drop table if exists "+TABLENAME+"";
db.execSQL(query);
}
public void delete(int id) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
//deleting row
sqLiteDatabase.delete(TABLENAME, "id=" + id, null);
sqLiteDatabase.close();
}
}
解决方案
为此检查此解决方案
public void remove(String Id){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(PE.TABLE_NAME, PE.COLUMN_PLACE_ID + "=\"" + Id+"\"", null) ;
}
并在您要删除时调用此方法
private void delete(int position){
Dbhelper dbHelper = new Dbhelper(MainActivity.this);
dbHelper.removePlace(arraylist.get(position).getPlaceId());
arraylist.remove(position);
mAdapter.notifyDataSetChanged();
}
推荐阅读
- java - 这个 Java 方法的变量类型应该是什么?
- javascript - 清理臃肿的 IF 语句,使其采用更简洁的行业标准格式
- swift - 无法在真机上安装“项目名称”
- java - 检查谁在使用 Thread.interrupted() 而没有抛出 InterruptedException 的好方法?
- hibernate - 我真的需要两次列出@Entity 吗?
- symfony - 使用 Symfony 3 进行路由翻译
- reactjs - Reactable 不能在生产模式下工作,但在开发模式下工作正常
- typescript - 你将如何获得 TypeScript 中某个类型的方法的名称?
- c# - 如何编写风格化但独立于操作系统的 WPF 控件。使用 Microsoft.Windows.Themes
- imagej - 调整宏 ImageJ 中的阈值