(android studio)删除带有复选框的listview项目

时间:2016-09-17 14:01:01

标签: android listview checkbox

我已经花了至少几个小时在网上搜索所有代码,但这个问题是用Google搜索的,但仍然不明白如何实现这个问题。

我有一个由数据库填充的listview和一个游标适配器。 每个listview项旁边都有一个复选框。

我想从列表视图中删除所有选中复选框的listview项目。

我的删除按钮需要哪些代码才能实现此功能?

谢谢和亲切的问候。

之前有人提到过这个问题;每个问题在问题中都有自己的实现和代码,我无法找到分步教程。

1 个答案:

答案 0 :(得分:0)

我的解决方案:

Example

这是我的适配器:

public class TextCheckBoxAdapter extends ArrayAdapter {

        private List<String> names;
        private List<Boolean> checked;

        public TextCheckBoxAdapter(Context context, List<String> names) {
            super(context, R.layout.custom_row_text_checkbox, names);
            this.names = names;
            checked = new ArrayList<>();
            for(String name : names) checked.add(false);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            View customView = inflater.inflate(R.layout.custom_row_text_checkbox, parent, false);

            TextView tv = (TextView) customView.findViewById(R.id.nameTv);
            tv.setText(names.get(position));

            final CheckBox checkBox = (CheckBox) customView.findViewById(R.id.checkbox);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    checked.set(position, checkBox.isChecked());
                }
            });


            return customView;
        }

        public String getName(int position){
            return names.get(position);
        }

        public List<Boolean> getCheckList(){
            return checked;
        }

        /**Anzahl selektierter Checkboxen*/
        public int getCountSelectedCheckBoxes(){
            int toReturn = 0;
            for(boolean b : checked) if(b) toReturn++;
            return toReturn;
        }

        public void delete(int i){
            names.remove(i);
            checked.remove(i);
            notifyDataSetChanged();
        }

        public boolean isEmpty(){
            return names.isEmpty();
        }
    }

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_marginLeft="@dimen/custom_margin_left_and_right"
    android:layout_marginStart="@dimen/custom_margin_left_and_right"
    android:layout_marginRight="@dimen/custom_margin_left_and_right"
    android:layout_marginEnd="@dimen/custom_margin_left_and_right">

    <TextView
        android:id="@+id/nameTv"
        android:layout_weight="0.9"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/unbenannt"
        android:textColor="@color/grey"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_weight="0.1"
        android:layout_gravity="center"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

</LinearLayout>

//通过工具栏中的图标删除

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == android.R.id.home){
            onBackPressed();
        }else if(id == R.id.action_loeschen){
            if(adapter.getCountSelectedCheckBoxes() > 0){
                List<Boolean> checked = adapter.getCheckList();
                for(int i = checked.size() - 1; i >= 0; i--){   //Rueckwaerts ausfuehren!
                    if(checked.get(i)){
                        String name = adapter.getName(i);

                        //Loesche aus der Liste
                        adapter.delete(i);

                        //TODO: Delete from DB or whatever

                        //Checken ob Liste leer
                        checkIsListEmpty();
                    }
                }
            }else{
                MyToast.showShort(this, getString(R.string.hinweis_keine_selektion));
            }
        }

        return super.onOptionsItemSelected(item);
    }