您好我想要从列表视图和数据库中删除已检查的项目。我使用菜单。如果从菜单中选择删除,那么我想从列表视图和数据库中删除所选项目。如果选择全部单击菜单我要设置所有列表项的复选框,然后从列表视图中删除所有值并删除数据库中的所有记录。使用以下代码填充listview中数据库中的数据如果有人知道,请帮我。
代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.senthistory);
lvhistory = (ListView) findViewById(android.R.id.list);
PopulateSentList();
}
public void PopulateSentList() {
String strquery = "SELECT * FROM sent_history";
Cursor Cursor = (MainscreenActivity.JEEMAAndroSMSDB).rawQuery(
strquery, null);
MyAdapter adapter = new MyAdapter(SentHistoryActivity.this, Cursor);
setListAdapter(adapter);
lvhistory.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
SQLiteCursor selectedValue = (SQLiteCursor) getListAdapter()
.getItem(position);
String id1 = selectedValue.getString(0);
System.out.println("DATA-->>>" + id1);
Intent intent = new Intent(getApplicationContext(),
Historydisplay.class);
intent.putExtra("Id", id1);
final int result = 1;
startActivityForResult(intent, result);
}
});
}
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(SentHistoryActivity.this,
MainscreenActivity.class);
startActivity(intent);
finish();
}
private void CreateMenu(Menu menu) {
menu.setQwertyMode(true);
MenuItem mnu1 = menu.add(0, 0, 0, "Delete");
{
mnu1.setAlphabeticShortcut('D');
}
MenuItem mnu2 = menu.add(0, 0, 0, "Select All");
{
mnu2.setAlphabeticShortcut('S');
}
}
private boolean MenuChoice(MenuItem item) throws Exception {
switch (item.getItemId()) {
case 0:
int count = (int) getListAdapter().getCount();
for (int i = 1; i <= count; i++) {
if (this.lvhistory.isItemChecked(i)) {
listItems.remove(i);
adapter.notifyDataSetChanged();
MainscreenActivity.JEEMAAndroSMSDB.delete(
MainscreenActivity.Table_SentHistory, "_id=" +i, null);
finish();
Intent intent = new Intent(getApplicationContext(),
SentHistoryActivity.class);
startActivity(intent);
}
}
return true;
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
try {
return MenuChoice(item);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private class MyAdapter extends ResourceCursorAdapter {
public MyAdapter(Context context, Cursor cur) {
super(context, R.layout.dummy, cur);
}
@Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return li.inflate(R.layout.dummy, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cur) {
TextView tvListText = (TextView)view.findViewById(R.id.Mobile);
final CheckBox chkBox = (CheckBox)view.findViewById(R.id.check);
tvListText.setText(cur.getString(cur.getColumnIndex(MainscreenActivity.COL_Mobile)));
chkBox.setTag(cur.getString(cur.getColumnIndex(MainscreenActivity.COL_Sent_id)));
chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Log.v("Checked", chkBox.getTag().toString());
}
});
}
}
答案 0 :(得分:2)
要删除选中的项目,可以使用CheckBox的isChecked()方法。在您的代码中,您可以按以下方式使用。
chkBox.setOnClickListener(new new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox)v;
if(cb.isChecked() == true)
{
String getStrinValue = cb.getTExt().toString(); // here you will get the value of selected CheckBox
// And now you have to perform your deletion operation as usual.
}
}
答案 1 :(得分:0)
好的,我将概述如何完成。这就是我为我的申请所做的。
首先,您需要为列表项创建一个模型类,该类具有维护状态的属性(已选中),然后每当您从数据库中获取项目时,都会创建项目列表,例如arraylist。
然后,只要选中复选框,就会更改特定列表项的state属性。最后,当您从菜单中单击删除时,将有三个步骤
我认为这是有道理的
修改强>
你有没有试过this以前的SO问题?顺便说一句,这只会帮助从适配器中删除项目。您必须找到一种从数据库中删除项目的方法。
答案 2 :(得分:0)
这就是我的表现。我发现这是始终匹配正确ID的最简单的解决方案。因为适配器和光标将始终具有相同的计数和位置。两者的起始指数均为0.
这就是我的菜单项内容。 我还添加了一个警告对话框作为安全步骤。
if (mListView.getCount() == 0 || mListView.getCheckedItemCount() == 0) {
Toast.makeText(this, "Select an item by holding", Toast.LENGTH_SHORT).show();
return false;
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Are you sure you want to delete these items?");
builder.setCancelable(true);
builder.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SparseBooleanArray selection = mListView.getCheckedItemPositions();
Cursor data = mDBHandler.getAllIDs(); // "SELECT " + COLUMN_ID + " FROM " + TABLE_NAME;
int itemID;
int itemCount = arrayAdapter.getCount();
for (int i=itemCount-1; i >= 0; i--) {
if (selection.get(i)) {
data.moveToPosition(i);
itemID = data.getInt(data.getColumnIndexOrThrow("id")); // COLUMN_ID = "id"
mDBHandler.deleteItem(itemID);
}
}
selection.clear();
updateListView();
dialog.cancel();
}
});
builder.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDelete = builder.create();
alertDelete.show();
return true;
public void updateListView() {
mListView.setAdapter(arrayAdapter);
Cursor data = mDBHandler.getItems();
arrayList.clear();
while (data.moveToNext()) {
arrayList.add(data.getString(1));
}
arrayAdapter.notifyDataSetChanged();
}