嗨,我想检查列表视图的所有项目。为此,我正在使用菜单。
如果选择全选菜单,则必须选择所有数据。同样,如果我从菜单中选择删除,则必须删除列表视图的所有值。
我正在正确选择列表视图项目。但是,如果我取消选中任何复选框并选择删除它,请说“请选择一个项目”。这是一个错误。
这意味着如果我选择所有项目然后取消选中列表视图中的任何一项,则必须删除选中的值。
我不知道该怎么做。
如果有人知道,请帮助我。
代码:
private void CreateMenu(Menu menu) {
menu.setQwertyMode(true);
MenuItem mnu1 = menu.add(0, 0, 0, "Delete");
{
mnu1.setAlphabeticShortcut('a');
}
MenuItem mnu2 = menu.add(1, 1, 1, "Select All");
{
mnu2.setAlphabeticShortcut('s');
}
}
private boolean MenuChoice(MenuItem item) throws Exception {
switch (item.getItemId()) {
case 0:
if (getStrinValue != null) {
delhistory(getStrinValue);
} else {
Toast.makeText(getApplicationContext(),
"Please select an Item", Toast.LENGTH_SHORT).show();
}
case 1:
if(item.getTitle().equals("Select All")){
for(int i=0; i < lvhistory.getChildCount(); i++){
RelativeLayout itemLayout = (RelativeLayout)lvhistory.getChildAt(i);
final CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.check);
cb.setChecked(true);
if (cb.isChecked() == true) {
getStrinValue = getStrinValue + ","
+ cb.getTag().toString();
}else{
getStrinValue = getStrinValue + ","
+ cb.getTag().toString();
}
}
}
}
return true;
}
@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);
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.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
if (cb.isChecked() == true) {
getStrinValue = getStrinValue + ","
+ cb.getTag().toString();
} else {
getStrinValue = null;
}
}
});
}
}
public void delhistory(String getStrinValue) {
int pos1 = getStrinValue.indexOf(",");
if (pos1 > 0) {
String rowId = getStrinValue.substring(pos1 + 1);
String delimiter = "\\,";
String[] sentID = rowId.split(delimiter);
for (int i = 0; i < sentID.length; i++) {
String temp0 = sentID[i];
int id = Integer.parseInt(temp0);
MainscreenActivity.JEEMAAndroSMSDB
.delete(MainscreenActivity.Table_SentHistory, "_id="
+ id, null);
}
Toast.makeText(getApplicationContext(),
"History deleted successfully", Toast.LENGTH_SHORT)
.show();
finish();
Intent intent = new Intent(getApplicationContext(),
SentHistoryActivity.class);
startActivity(intent);
}
}