使用SQLITE删除自定义列表视图中的行

时间:2017-05-07 16:33:04

标签: android sqlite android-sqlite android-adapter

我正在我的应用中创建购物车。我正在使用sqlite将值存储在表中以检索该人员添加的内容。

我为每一行添加了一个按钮,这样当他们点击该按钮时,它将从列表中删除该行及其数据。

但是如何在单击按钮时调用我的删除功能?提前谢谢!

这是我的Adapter类:

public class ListCartAdapter extends BaseAdapter {

private Context context;

private ArrayList<String> orderid;
private ArrayList<String> orderName;
private ArrayList<String> orderSize;
private ArrayList<String> orderQuantity;

public ListCartAdapter(Context context, ArrayList<String> orderid, ArrayList<String> orderName, ArrayList<String> orderSize, ArrayList<String> orderQuantity){
  // public ListCartAdapter(Context context, ArrayList<String> orderName){
    this.context = context;
    this.orderid = orderid;
    this.orderName = orderName;
    this.orderSize = orderSize;
    this.orderQuantity = orderQuantity;


}

@Override
public int getCount() {
    return orderName.size();
}

@Override
public Object getItem(int position) {
    return orderName.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View listView;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listView = inflater.inflate(R.layout.cart_list_item, null);

    TextView number = (TextView) listView.findViewById(R.id.textID);
    TextView name = (TextView) listView.findViewById(R.id.textOrderName);
    TextView size = (TextView) listView.findViewById(R.id.textOrderSize);
    TextView quantity = (TextView) listView.findViewById(R.id.textOrderQuantity);

    ImageButton btnRemove = (ImageButton) listView.findViewById(R.id.btnRemove);

    number.setText(orderid.get(position));
    name.setText(orderName.get(position));
    size.setText(orderSize.get(position));
    quantity.setText(orderQuantity.get(position));

    //BUTTON TO REMOVE ROW
    btnRemove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           orderName.remove(position);
            notifyDataSetChanged();

        }
    });

    return listView;
}

这是我的SQLITEHELPER CLASS:

 public static final String DB_NAME = "CartDB";
public static final String TABLE_NAME = "Orders";
public static final String COLUMN_ID = "id";
public static final String NAME ="name";
public static final String SIZE ="size";
public static final String QUANTITY ="quantity";

private static final int DB_VERSION = 1;

public cartDatabaseHelper(Context context)
{
    super(context,DB_NAME,null,DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String sql = "CREATE TABLE " + TABLE_NAME
            + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + NAME + " VARCHAR, "
            + SIZE + " VARCHAR, "
            + QUANTITY + " VARCHAR);";

              db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String sql = "DROP TABLE IF EXIST Orders";
    db.execSQL(sql);
    onCreate(db);
}

public boolean addPerson(String name, String size, String quantity){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    contentValues.put(NAME,name);
    contentValues.put(SIZE,size);
    contentValues.put(QUANTITY,quantity);

    long result = db.insert(TABLE_NAME,null ,contentValues);
    if(result == -1)
        return false;
    else
        return true;
}


public Cursor getListContents(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    return data;
}


public void removeSingleContact(String id) {
    SQLiteDatabase database = this.getWritableDatabase();

   database.execSQL("DELETE FROM " + TABLE_NAME + " WHERE " + COLUMN_ID + "= '" + id + "'");

    //Close the database
    database.close();
}

1 个答案:

答案 0 :(得分:0)

嗯,我解决了我的问题:D我仍然不知道为什么我被投票,但是谁在乎。

这是在Adapter类中调用SqliteHelper的方法,添加此代码

final cartDatabaseHelper db = new cartDatabaseHelper(context);

我希望这可以帮助其他人解决这个问题:D