-2
私は自分のアプリでショッピングカートを作成しています。私はsqliteを使用して、テーブルに値を格納して、人が何を追加したかを取得しています。SQLiteを使用してカスタムリストビューの行を削除
各行にボタンを追加しました。そのボタンをクリックすると、その行がリストから削除され、そのデータがテーブルから削除されます。
ボタンをクリックしたときに削除機能を呼び出すにはどうすればよいですか?前もって感謝します!
は、これは私のアダプタクラスです:
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クラスです:
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();
}
'「」+ COLUMN_ID +「= '"+ id +"' ");' idは** INTEGER **です。 ** STRING **のように扱いません。 –
@Rotwang大丈夫です。私はそれを念頭に置いておきます。私はこの問題にどのように取り組むと思いますか? –
また、SQLインジェクションは悪いです。 'database.delete'メソッドについて学ぶ –