我的程序中有一个listview,我有三个按钮。
如下图所示:
我希望当我点击删除按钮从列表视图和数据库中删除任何项目时。
我试图删除但已清除所有项目。
请帮帮我。谢谢
我的数据库助手:
public class DatabaseHelper1 extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "mylist.db";
public static final String TABLE_NAME = "mylist_data";
public static final String COL_1 = "ID";
public static final String COL_2 = "billID";
public static final String COL_3 = "payID";
public static final String COL_4 = "txtLOCATION";
public static final String COL_5 = "txtTYPE";
public DatabaseHelper1(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,\n" +
" billID INTEGER, payID INTEGER, txtLOCATION TEXT, txtTYPE TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
public boolean addData(String billid, String payid, String txtlocation,
String txttype) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, billid);
contentValues.put(COL_3, payid);
contentValues.put(COL_4, txtlocation);
contentValues.put(COL_5, txttype);
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 deleteAll()
{
SQLiteDatabase db = this.getWritableDatabase();
db.delete(DatabaseHelper1.TABLE_NAME, null, null);
}
}
我的列表适配器:
public class FourColumn_ListAdapter extends ArrayAdapter<User> {
DatabaseHelper1 myDB;
private LayoutInflater mInflater;
private ArrayList<User> users;
private int mViewResourceId;
public FourColumn_ListAdapter(Context context, int textViewResourcesId, ArrayList<User> users){
super(context,textViewResourcesId, users);
this.users = users;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewResourceId = textViewResourcesId;
}
@NonNull
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
final User user = users.get(position);
if (user != null){
TextView billId = (TextView) convertView.findViewById(R.id.bilid);
TextView payId = (TextView) convertView.findViewById(R.id.pyid);
TextView location = (TextView) convertView.findViewById(R.id.lc);
TextView types = (TextView) convertView.findViewById(R.id.noe);
if(billId != null){
billId.setText((user.getBillID()));
}
if(payId != null){
payId.setText((user.getPayID()));
}
if(location != null){
location.setText((user.getLocation()));
}
if(types != null){
types.setText((user.getTypes()));
}
final Button button = (Button)convertView.findViewById(R.id.btnDelete);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(getContext())
.setTitle("حذف قبض")
.setMessage("آیا از حذف این قبض اطمینان دارید؟ ")
.setPositiveButton("بله",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
myDB = new DatabaseHelper1(getContext());
myDB.deleteAll();
users.remove(position);
notifyDataSetChanged();
}
})
.setNegativeButton("بیخیال", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
}
});
}
return convertView;
}
答案 0 :(得分:2)
当我点击删除按钮删除列表中的任何项目时 查看和数据库
创建 DELETE 方法
public void deleteItem(String getID) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE from mylist_data WHERE billID= '" +getID+ "'");
}
然后
public void onClick(DialogInterface dialog, int id)
{
myDB = new DatabaseHelper1(getContext());
myDB.deleteItem(user.getBillID());
users.remove(position);
notifyDataSetChanged();
}