gpt4 book ai didi

java - 删除 recyclerView 和 SQLite 数据库上的两行列表项

转载 作者:行者123 更新时间:2023-11-29 23:18:21 24 4
gpt4 key购买 nike

我是编程初学者,我想在 recyclerView 的每个项目列表上制作一个删除按钮。我从堆栈溢出中获得了一些引用,它仅在 Activity (布局)中对已删除的项目起作用,但是当我再次运行该 Activity 时,所选项目再次显示。

在stackoverflow上找了一些相关的文章,做了从SQLite中删除的方法。但是每次我调用删除功能时,我的应用程序都会崩溃,“不幸的是应用程序已停止”。

希望有人能帮我弄清楚。

这是我的数据库助手类

public class DatabaseHelperClass extends SQLiteOpenHelper {
Log cat Database
public static String log = "DatabaseHelper";

//Databse version
public static final int DATABASE_VERSION = 1;

//Database name
public static final String DATABSE_NAME = "dbPig";

//Tables Name
public static final String TABLE_PIGINFO = "tb_pigInfo";

//Common and PigInfo Column Names
public static final String KEY_ID = "id";
public static final String KEY_NAMA = "nama";
public static final String KEY_TANGGAL_PENDAFTARAN = "tanggal_pendaftaran";


//table create statement
//table pig Info
public static final String CREATE_TABLE_PIGINFO = "CREATE TABLE "
+ TABLE_PIGINFO + "(" + KEY_ID + " INTEGER," + KEY_NAMA
+ " TEXT," + KEY_TANGGAL_PENDAFTARAN
+ " TEXT" + ")";


public DatabaseHelperClass(Context context) {
super(context, DATABSE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

//creating requaired table
db.execSQL(CREATE_TABLE_PIGINFO);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PIGINFO);

// create new tables
onCreate(db);
}

public void insertdata(String nama, String tanggal_pendaftaran) {
System.out.print("Tersimpan" + TABLE_PIGINFO);
SQLiteDatabase db = this.getWritableDatabase();

ContentValues contentValues = new ContentValues();
contentValues.put(KEY_NAMA, nama);
contentValues.put(KEY_TANGGAL_PENDAFTARAN, tanggal_pendaftaran);
db.insert(TABLE_PIGINFO, null, contentValues);

}


public List<PigInfoTable> getdata() {
List<PigInfoTable> data = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from " + TABLE_PIGINFO + " ;", null);
StringBuffer stringBuffer = new StringBuffer();
PigInfoTable pigInfoTable = null;
while (cursor.moveToNext()) {

pigInfoTable = new PigInfoTable();
String nama = cursor.getString(cursor.getColumnIndexOrThrow("nama"));
String tanggal_pendaftaran = cursor.getString(cursor.getColumnIndexOrThrow("tanggal_pendaftaran"));
pigInfoTable.setNama(nama);
pigInfoTable.setTanggal_pendaftaran(tanggal_pendaftaran);
stringBuffer.append(pigInfoTable);
data.add(0, pigInfoTable);
}

for (PigInfoTable mo : data) {

Log.i("Hellomo", "" + mo.getNama());
}

return data;
}

public void delete(int position) {

SQLiteDatabase db = this.getWritableDatabase();
String table = TABLE_PIGINFO;
String whereClause = KEY_ID;
String [] whereArgs = new String[] {String.valueOf(position)};
db.delete (table, whereClause, whereArgs);

}

这是我的适配器

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.Myholder> {

DatabaseHelperClass databaseHelper;

List <PigInfoTable> pigInfoTablesArrayList;


public RecycleAdapter(List <PigInfoTable> pigInfoTablesArrayList) {
this.pigInfoTablesArrayList = pigInfoTablesArrayList;

}

class Myholder extends RecyclerView.ViewHolder{
private TextView nama, tanggal_pendaftaran;
private Button delete;

public Myholder(View itemView) {
super(itemView);

nama = (TextView) itemView.findViewById(R.id.nama1);
tanggal_pendaftaran = (TextView) itemView.findViewById(R.id.tanggal1);
delete = (Button) itemView.findViewById(R.id.delete);


}
}

@Override
public Myholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_itempigview,null);
return new Myholder(view);

}

@Override
public void onBindViewHolder(Myholder holder, final int position) {

PigInfoTable pigInfoTable= pigInfoTablesArrayList.get(position);
holder.nama.setText(pigInfoTable.getNama());
holder.tanggal_pendaftaran.setText(pigInfoTable.getTanggal_pendaftaran());
holder.itemView.setClickable(true);





holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

databaseHelper.delete(position);
pigInfoTablesArrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, pigInfoTablesArrayList.size());
notifyDataSetChanged();


}
});



}



@Override
public int getItemCount() {

return pigInfoTablesArrayList.size();
}

我正在尝试其他一些解决方案,但发生了同样的错误

上的空对象引用

Position

错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.newbreedy.DatabaseHelperClass.delete(int)' on a null object reference at com.example.newbreedy.RecycleAdapter$1.onClick(RecycleAdapter.java:66)

谢谢

最佳答案

com.example.newbreedy.DatabaseHelperClass.delete(int)' 在空对象引用上因为你还没有初始化的引用DatabaseHelper类数据库助手;

所以添加。

databaseHelper =new DatabaseHelperClass (context);

在你的回收适配器中

在您的代码中,您正在发送适配器位置,因此代替位置发送 KEY_ID。

            databaseHelper.delete(position);
pigInfoTablesArrayList.remove(position);
notifyDataSetChanged();

关于java - 删除 recyclerView 和 SQLite 数据库上的两行列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54878094/

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