如何使用CustomListView中的复选框删除行

时间:2017-05-04 10:06:34

标签: android

我想在单击按钮时使用CustomListView中的复选框删除一行,然后删除该行。我在工作时浪费了几个小时。可以帮助我..

1 个答案:

答案 0 :(得分:0)

首先在listView项目布局中创建checkBox。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dip"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2">

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:buttonTint="#000"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        android:focusable="false"
        android:focusableInTouchMode="false"/>


  //OtherViews

</LinearLayout>

之后在你的自定义Adapter类中,创建一个成员变量及其setter方法,一旦我们长按任何listView项,它将被调用以使checkBox可见。

此外,您必须在自定义适配器类中使用ViewHolder来跟踪已检查的行,否则在选择行时,当您滚动listView时,一旦超出屏幕视图,就不会保存行状态..

 private int checkboxVisibility = View.GONE;

    public void setCheckboxVisibility(int visibility) {
        this.checkboxVisibility = visibility;
    }
 class ViewHolder {

        protected CheckBox checkBox;

        //othercode here

    }

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

        ViewHolder viewHolder = null;
        if (convertView == null) {

            convertView = inflator.inflate(R.layout.item_todo, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

            viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                    list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                }
            });
            convertView.setTag(viewHolder);

            convertView.setTag(R.id.checkbox, viewHolder.checkBox);

            //other views goes here


        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // This line is important.
        viewHolder.checkBox.setTag(position); 


        viewHolder.checkBox.setChecked(list.get(position).isSelected());
        viewHolder.checkBox.setVisibility(checkboxVisibility);

        return convertView;
    }

}
    @Override
    public void onBackPressed()
    {
        adapter.setCheckboxVisibility(View.GONE);

        adapter.notifyDataSetChanged();

    }

现在在OnItemLongClickListener中调用setter方法setCheckboxVisibility,这将使复选框可见,您可以选择单行或多行。

 public OnItemLongClickListener myClickListener = new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View v,
                                   int arg2, long arg3) {
        // Assigning the item position to our global variable
        // So we can access it
        listView.performLongClick();
        adapter.setCheckboxVisibility(View.VISIBLE);
        adapter.notifyDataSetChanged();
       checkBox= (CheckBox) v.findViewById(R.id.checkbox);
        checkBox.setFocusable(true);
        checkBox.setFocusableInTouchMode(true);



        return true;
    }
};

选择所有行/行后,您可以通过浏览保存复选框状态的列表并删除检查状态为真的列表来删除它们。

 public void deleteSelected(View view) {

    if (listItems != null && !listItems.isEmpty()) {

        // Creating a new alert dialog to confirm the delete
        new AlertDialog.Builder(view.getContext())
                .setTitle("Are you sure? ")
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int whichButton) {
                                int count=0;
                                ArrayList<Reflection> selectedItems = listItems;

                            for(int i=0;i<selectedItems.size();i++) {

                                if(selectedItems.get(i).isSelected()){

                                    DatabaseHelper.deleteNote(selectedItems.get(i).getId());
                                }




                            }
                                listItems = DatabaseHelper.getAllNotes("newones");
                                adapter.clear();
                                adapter.addAll(listItems);
                                if(checkBox!=null) {
                                    checkBox.setFocusable(false);
                                    checkBox.setFocusableInTouchMode(false);
                                }
                                adapter.setCheckboxVisibility(View.GONE);
                                adapter.notifyDataSetChanged();

                                }
                                dialog.dismiss();
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int whichButton) {
                                // When you press cancel, just close the
                                // dialog
                                dialog.cancel();
                            }
                        }).show();


    }