recyclerview sqlite 数据库,recyclerview 上删除的数据但数据库中没有删除

Posted

技术标签:

【中文标题】recyclerview sqlite 数据库,recyclerview 上删除的数据但数据库中没有删除【英文标题】:recyclerview sqlite database, data deleted on recyclerview but not deleting in database 【发布时间】:2021-10-27 05:02:03 【问题描述】:

我想同时删除 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();
    


【问题讨论】:

【参考方案1】:

为此检查此解决方案

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

【讨论】:

以上是关于recyclerview sqlite 数据库,recyclerview 上删除的数据但数据库中没有删除的主要内容,如果未能解决你的问题,请参考以下文章

如何在基于 SQLite 的 recyclerview 中实现 searchview? - 安卓

如何从 recyclerview 和 sqlite 中删除项目? [复制]

使用从 SQLite 概括的数据填充 RecyclerView/ListView

SQLite 数据库通过适配器获取所有数据到 Recyclerview。(寻找首选项)

更新 SQLite 数据库 Android RecyclerView 迭代

使用 SQLite、imageView 和可回收的 RecyclerView 性能 = false