0

您好我正在使用菜单来删除列表视图项目。
我有两个菜单,一个是Delete,另一个是Select All。我正在从数据库中检索数据并将其显示listviewcheckbox.
如果我选择一些列表项并选择删除,则必须删除所选项目。如果我从菜单中选择全选,则必须选择所有列表项,如果我从菜单中选择删除,则必须删除所有数据。
问题是,如果我按菜单选择所有列表视图项目,Select All并且取消选中某些列表项目,则其余选定项目不会被删除。
我曾经ResourceCursorAdapter显示数据库中的列表项。

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();
                        System.out.println("CHECKEDIITTEEMSS-->>>"
                                + getStrinValue);
                    } else {                        
                    }                   
                }
            }
        }
        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.custom_sent_view, cur);
        }

        @Override
        public View newView(Context context, Cursor cur, ViewGroup parent) {
            LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return li.inflate(R.layout.custom_sent_view, 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);
        }
    }
4

1 回答 1

0

好的,最后我自己找到了答案。我将 setonclicklistener 添加到复选框以删除未选中的值。答案如下:

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();
                        System.out.println("CHECKEDIITTEEMSS-->>>"
                                + getStrinValue);
                    }
                    cb.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                            // TODO Auto-generated method stub
                            if (cb.isChecked() == false) {                              
                                getUnselected = cb.getTag().toString();
                                getStrinValue = getStrinValue.replace(","
                                        + getUnselected, "");
                                System.out.println("UNCHECKED--->>>"
                                        + getStrinValue);
                            } else if (cb.isChecked() == true) {
                                getStrinValue = getStrinValue + ","
                                        + cb.getTag().toString();
                                System.out.println("CHECKEDIITTEEMSS-->>>"
                                        + getStrinValue);
                            }
                        }
                    });
                }
            }
        }
        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.custom_sent_view, cur);
        }

        @Override
        public View newView(Context context, Cursor cur, ViewGroup parent) {
            LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return li.inflate(R.layout.custom_sent_view, 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);
        }
    }

非常感谢你。

于 2012-08-09T12:41:50.813 回答