您好我想從列表視圖和database.Iam中使用菜單中刪除選中的項目。如果從菜單中選擇刪除,那麼我想從選擇的項目中刪除列表視圖和在數據庫中。如果選擇全部被單擊菜單中我想設置列表項的所有複選框檢查,然後刪除所有值從列表視圖,並刪除數據庫中的所有記錄。使用下面的代碼使用複選框在列表視圖中填充數據庫中的數據。如果有人知道,請幫助我。如何從列表視圖和數據庫中刪除選中的項目
代碼:
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());
}
});
}
}
我想刪除它通過選擇從menu.How刪除可我不喜歡that.Iam新android.Please幫助我以這樣的方式選擇的數據。 – Manikandan 2012-08-03 08:18:12
@ user1498488我在我的帖子中做了一些改變,請找到它。好運:) – Akshay 2012-08-03 08:25:55
對不起,說akshay,我不知道如何進一步刪除選擇的項目,通過選擇刪除菜單。如果你有空閒時間意味着請指導我這樣做。 – Manikandan 2012-08-03 08:33:09