我在自定义列表视图中的每一行旁边都有一个复选框。用户选择要删除的行。我想要完成的是使用复选框仅删除选定的行。以下是我尝试过的:< / p>
关于我的活动:
-fno-exceptions
在我的DBHelper中:
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_delete_selected) {
CheckBox cb = (CheckBox)findViewById(R.id.checkBox);
if (cb.isChecked()) {
myDb.deleteSingleContact(id);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
showTable();
}
然而,代码不会删除行,仍然显示吐司。我错过了什么?任何帮助将不胜感激。 的修改 我的完整活动代码:
public void deleteSingleContact(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(CONTACTS_TABLE_NAME, CONTACTS_COLUMN_ID+"="+id,null);
}
完成DBHelper:
public class MyBasket extends ActionBarActivity {
ListView obj;
DBHelper myDb;
int numRows;
int id_To_Update = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_basket);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
myDb = new DBHelper(this);
obj = (ListView) findViewById(R.id.listView1);
Button btn = (Button) findViewById(R.id.checkout);
if (myDb.checkForTables()) {
showTable();
btn.setVisibility(View.VISIBLE);
} else {
showAlert();
btn.setVisibility(View.GONE);
}
TextView txt = (TextView) findViewById(R.id.numRows);
int profile_counts = myDb.numberOfRows();
myDb.close();
txt.setText(String.valueOf(profile_counts));
TextView txt2 = (TextView) findViewById(R.id.Amount_textView);
int i = myDb.getTotalOfAmount();
txt2.setText("" + i);
Button basketButton = (Button) findViewById(R.id.checkout);
basketButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MyBasket.this, CheckOut.class);
startActivity(intent);
}
});
}
public void showAlert() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.empty_basket, null);
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setView(layout);
adb.setCancelable(false);
adb.setPositiveButton("Add items to basket", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(MyBasket.this, MainActivity.class);
startActivity(i);
}
});
adb.show();
}
private void showTable() {
ArrayList<ContactListItems> contactList = myDb.getAllContacts();
ContactListAdapter contactListAdapter = new ContactListAdapter(
MyBasket.this, contactList);
//adding it to the list view.
obj = (ListView) findViewById(R.id.listView1);
obj.setAdapter(contactListAdapter);
obj.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
deleteDialog();
}
});
obj.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
myDb.deleteSingleContact(arg2);
showTable();
return true;
}
});
}
private void deleteDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder (MyBasket.this);
alertDialog.setCancelable(false);
alertDialog.setMessage("Delete item?");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myDb.deleteSingleContact(which);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_my_basket, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(MyBasket.this, Settings.class);
startActivity(intent);
return true;
}
if (id == R.id.action_delete) {
myDb.deleteContact();
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MyBasket.this, MyBasket.class);
startActivity(intent);
return true;
}
if (id == R.id.action_delete_selected) {
CheckBox cb = (CheckBox)findViewById(R.id.checkBox);
if (cb.isChecked()) {
myDb.deleteSingleContact(id);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
showTable();
}
return true;
}
if (id == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
您是否检查了返回值,它应该是整数1
int rowsDeleted = db.delete(CONTACTS_TABLE_NAME, CONTACTS_COLUMN_ID+"="+id,null);
if(rowsDeleted>0){
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
}