【问题标题】:Remove all my item in Listview删除我在 Listview 中的所有项目
【发布时间】:2017-05-22 12:32:10
【问题描述】:

我的行中有一个Listview,我有一个复选框来选择要删除的项目。 我想要一个按钮来选择我的所有项目并同时删除它们。 当我单击按钮选择所有项目时,只有最后一个项目被选中。

这是我的代码:

 all.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                for(int i=0;i<listview.getChildCount();i++){
                    Log.i("mhs",listview.getChildCount()+"");
                    checkBox.setChecked(true);
                }


            }
        });

这是我的Adaptor

  public class CustomArrayAdapter extends ArrayAdapter<String> {

    private LayoutInflater inflater;

    // This would hold the database objects i.e. Blacklist
    private List<Blacklist> records;

    @SuppressWarnings("unchecked")
    public CustomArrayAdapter(Context context, int resource, @SuppressWarnings("rawtypes") List objects) {
        super(context, resource, objects);

        this.records = objects;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        //Reuse the view to make the scroll effect smooth
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_item, parent, false);

        // Fetch phone number from the database object
        final Blacklist phoneNumber = records.get(position);

        // Set to screen component to display results
        ((TextView) convertView.findViewById(R.id.serial_tv)).setText("" + (position + 1));
        ((TextView) convertView.findViewById(R.id.phone_number_tv)).setText(phoneNumber.phoneNumber);
        checkBox = (CheckBox) convertView.findViewById(R.id.check);
        final LinearLayout me = (LinearLayout) convertView.findViewById(R.id.me);
        if (position + 1 >= 1) {
            me.setVisibility(View.VISIBLE);

        }
        checkBox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (((CheckBox) v).isChecked()) {



                    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

                    // Set the appropriate message into it.
                    alertDialogBuilder.setMessage(getResources().getString(R.string.block));

                    // Add a positive button and it's action. In our case action would be deletion of the data
                    alertDialogBuilder.setPositiveButton(getResources().getString(R.string.yes),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    try {

                                        blackListDao.delete(blockList.get(position));

                                        // Removing the same from the List to remove from display as well
                                        blockList.remove(position);
                                        listview.invalidateViews();

                                        // Reset the value of selectedRecordPosition
                                        selectedRecordPosition = -1;
                                        populateNoRecordMsg();
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });

                    // Add a negative button and it's action. In our case, just hide the dialog box
                    alertDialogBuilder.setNegativeButton(getResources().getString(R.string.no),
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    checkBox.setChecked(false);
                                }
                            });

                    // Now, create the Dialog and show it.
                    final AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();
                }

            }
        });
        all.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                for(int i=0;i<listview.getChildCount();i++){
                    Log.i("mhs",listview.getChildCount()+"");
                    checkBox.setChecked(true);
                    listview.removeView(i);
                }


            }
        });

        return convertView;
    }

}

如何同时选择所有项目?

【问题讨论】:

  • 只需像 YOUR_ARRAYLIST.clear() 一样清除您的列表;并确保在清除列表后使用notifyDatachange
  • 也发布你的适配器类
  • 我假设您想从某种持久性系统数据库、文件中删除项目...您只需删除所有这些项目,然后更新您的适配器
  • 我刚刚选择的最后一项。现在我什么都不清除。首先我必须让他们聚在一起,然后删除他们
  • 配合什么是“复选框”?您正在执行 for 循环,但没有使用增量来获取当前项目。

标签: java android


【解决方案1】:

列表视图的问题是视图被回收了!这意味着调用

            checkBox.setChecked(true);

只会编辑当前活动的项目。如果您想删除所有项目,最好的方法是清除您正在使用的列表和 notifyDataSetChanged()。

【讨论】:

  • 如何清除我的列表?如何勾选我所有的复选框然后删除它们?
  • 你的意思是 mylistview.clear?我不明白你的意思
  • 更好的方法可能是使用 listView.setAdapter(null);
  • 是的,但我想先选择我的项目然后删除它们
【解决方案2】:

在for循环中添加:

currentCheckBox = listView.getChildAt(i);
//now you should have the correct checkBox

编辑:如果您的需要只是删除数据和清除,我会为此制定一个方法。调用它,从 db 中删除项目,然后 .clear() 适配器。 (不记得此处是否自动调用了 notifyDatasetChanged(),如果没有,请执行)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-09
    • 2016-06-01
    • 1970-01-01
    • 2011-02-03
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多