首页 > 解决方案 > 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();
    }

}

标签: sqliteandroid-studioandroid-recyclerviewandroid-sqlitedelete-row

解决方案


为此检查此解决方案

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();
}

推荐阅读