【问题标题】:recyclerview sqlite database, data deleted on recyclerview but not deleting in databaserecyclerview sqlite 数据库,recyclerview 上删除的数据但数据库中没有删除
【发布时间】: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();
    }

}

【问题讨论】:

    标签: sqlite android-studio android-recyclerview android-sqlite delete-row


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

    【讨论】:

      猜你喜欢
      • 2019-01-15
      • 2016-10-05
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多