使用 SQLite 在 customAdapter 中删除一行扩展 BaseAdapter - Android

Delete A Row In customAdapter extends BaseAdapter using SQLite - Android

我试图在我的 SQLite 数据库中获取一行,以便在单击按钮时将其删除。我成功地在 ListView 中删除了一行,但每次我尝试从 sqlite 应用程序中删除时都会崩溃。

dba.removeData(position);

mModel.remove(position);
notifyDataSetChanged();

这只是删除 ListView 中的项目,而不是数据库中的行。

public class CustomAdapter extends BaseAdapter {
private int custom_adapter;
private Context context;
private ArrayList<Model> mModel;
DatabaseHelper dba = new DatabaseHelper(this.context);

public CustomAdapter(Context context, int custom_adapter, ArrayList<Model> mModel) {
    this.context = context;
    this.mModel = mModel;
    this.custom_adapter = custom_adapter;

}




@Override
public int getCount() {

    return mModel.size();
}

@Override
public Object getItem(int position) {

    return mModel.get(position);
}

@Override
public long getItemId(int position) {

    return mModel.get(position).getId();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v = View.inflate(context, R.layout.custom_adapter, null);
    final TextView ID = (TextView) v.findViewById(R.id.textViewID);
    TextView przejechane = (TextView) v.findViewById(R.id.textViewPrzejechane);
    TextView spalone = (TextView) v.findViewById(R.id.textViewSpalanie);
    TextView zuzytePaliwo = (TextView) v.findViewById(R.id.textViewUzytepaliwo);
    final TextView dataView = (TextView) v.findViewById(R.id.textViewData);
    final Button btnDelete = (Button) v.findViewById(R.id.btnDelete);
    final ListView listView = (ListView) v.findViewById(R.id.listview);
    ID.setText(String.valueOf( mModel.get(position).getId()));
    przejechane.setText(String.valueOf( mModel.get(position).getAmount_km()));
    spalone.setText(String.valueOf( mModel.get(position).getAvg()));
    zuzytePaliwo.setText(String.valueOf( mModel.get(position).getAmount_fuel()));
    dataView.setText(String.valueOf( mModel.get(position).getData()));



    btnDelete.setTag(position);


    btnDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dba.removeData(position);

            mModel.remove(position);
            notifyDataSetChanged();

            }
    });

    return v;
}

这是我的数据库助手

public class DatabaseHelper extends SQLiteOpenHelper {

private SQLiteDatabase mDatabase;
public static final String DATABASE_NAME = "AvgFuel.db";
public static final String TABLE_NAME = "avgFuel_table";

public static final String ID = "ID";
public static final String AMOUNT_FUEL = "AMOUNT_FUEL";
public static final String AMOUNT_KM = "AMOUNT_KM";
public static final String AVG = "AVG";
public static final String DATA = "DATA";
private static final String SELECT_PEOPLE = "SELECT * FROM " + TABLE_NAME;

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);

}



@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            AMOUNT_FUEL + " REAL, " + AMOUNT_KM + " REAL, " + AVG + " REAL, " + DATA + " TEXT" + ");");

}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String amount_fuel, String amount_km, double avg){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(AMOUNT_FUEL,amount_fuel);
    contentValues.put(AMOUNT_KM,amount_km);
    contentValues.put(AVG,avg);
    contentValues.put(DATA,getNow());
    long result = db.insert(TABLE_NAME, null, contentValues);
    if (result == -1)
        return false;
    else
        return true;
}
public void removeData(int id){
    SQLiteDatabase db = this.getReadableDatabase();
    db.delete(TABLE_NAME, ID + "=" + id , null);
    db.close();
}

public ArrayList<Model> getModels() {
    ArrayList<Model> model = new ArrayList<Model>();
    mDatabase = this.getReadableDatabase();
    Cursor cursor = mDatabase.rawQuery(SELECT_PEOPLE, null);
    cursor.moveToNext();
    for (int i = 0; i <cursor.getCount() ; i++) {
        model.add(new Model(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4)));
        cursor.moveToNext();
    }
    cursor.close();
    mDatabase.close();
    return model;
}
public Model getModel(int id){
    mDatabase = this.getReadableDatabase();
    String s = "SELECT * FROM" + TABLE_NAME + "WHERE " + ID + "=" + id;
    Cursor cursor = mDatabase.rawQuery(s,null);
    cursor.moveToFirst();
    Model model = new Model(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
    cursor.close () ;
    mDatabase.close () ;
    return model ;
}
private String getNow(){
    // set the format to sql date time
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date();
    return dateFormat.format(date);
}

尝试改变这个..mModel.get(position).getId()

 dba.removeData(mModel.get(position).getId());

我认为问题是 dba 创作。在这里你只是使用这个适配器上下文。这应该是您从 Activity.

获得的上下文
DatabaseHelper dba = new DatabaseHelper(this.context);

尝试使用这个

public CustomAdapter(Context context, int custom_adapter, ArrayList<Model> mModel) {
    this.context = context;
    this.mModel = mModel;
    this.custom_adapter = custom_adapter;
    dba = new DatabaseHelper(context);
}

要获取 recyclerView 的实际位置,您可以使用 getAdapterPosition()。像这样

dba.removeData(mModel.get(getAdapterPosition()).getId());

希望这会有所帮助。