简体   繁体   English

如何计算列表视图中选中的复选框的数量

[英]How to count the number of checkboxes ticked which is inside a listview

I have an custom array adaptor which is handling a list view. 我有一个自定义阵列适配器,正在处理列表视图。 Each row has two checkboxes. 每行都有两个复选框。 There are five rows in total. 总共有五行。 I want to be able to count the number of checkboxes ticked and then display this number in Stand1 我希望能够计算已勾选的复选框的数量,然后在Stand1中显示此数字

So basically the project Layout is like this 所以基本上项目布局是这样的

Inside stand1 there is a list view and a text view. 在stand1内部有一个列表视图和一个文本视图。 The stand1.java calls an array adapter Called CustomArrayAdaptor. stand1.java调用称为CustomArrayAdaptor的阵列适配器。 I want to be able to count the number of checkboxes clicked and send that data back to the stand1 我希望能够计算单击的复选框的数量并将该数据发送回stand1

I am quite new to android development so if somebody can be able to push me in the right direction, it would be great. 我对android开发非常陌生,所以如果有人能够将我推向正确的方向,那就太好了。

Here is my code 这是我的代码

Stand1.xml Stand1.xml

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

<ListView
    android:layout_width="fill_parent"
    android:layout_height="446dp"
    android:id="@+id/Stand1list"
    android:scrollIndicators="right"
    android:smoothScrollbar="true">


</ListView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Score To Databse"
        android:id="@+id/sendtodb"
        android:textSize="12dp"
        android:clickable="true"
        android:layout_below="@+id/Stand1list"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="8/10"
        android:id="@+id/Score"
        android:layout_marginLeft="33dp"
        android:layout_marginStart="33dp"
        android:layout_alignBottom="@+id/sendtodb"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="10dp" />

Row_Layout1.xml Row_Layout1.xml

<?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="match_parent"
    android:padding="20dp">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textelement"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:textSize="30dp"
        android:textStyle="bold"
        android:longClickable="false" />

    <CheckBox
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/Checkbox1"
        android:button="@drawable/checkboxcustom"
        android:layout_marginLeft="50dp" />
    <CheckBox

        android:button="@drawable/checkboxcustom"
        android:id="@+id/Checkbox2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="50dp" />

</LinearLayout>

Stand1.java Stand1.java

public class Stand1 extends Fragment {
    ListView mList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.stand1,container,false);
        mList = (ListView) root.findViewById(R.id.Stand1list);
        return root;
    }

    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        populateListView();
    }

    private void populateListView() {
        String[] pair = {"Pair 1","Pair 2","Pair 3","Pair 4","Pair 5"};

        //build adapter
        ArrayAdapter<String> adapter = new CustomArrayAdaptor(getActivity(),pair);

        mList.setAdapter(adapter);
    }
}

CustomArrayAdaptor.java CustomArrayAdaptor.java

public class CustomArrayAdaptor extends ArrayAdapter<String>{


    public CustomArrayAdaptor(Context context, String [] Pairs) {
        super(context, R.layout.row_layout1, Pairs);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View CustomView = inflater.inflate(R.layout.row_layout1, parent, false);
        String stringelement = getItem(position);
        TextView Text= (TextView)CustomView.findViewById(R.id.textelement);

        Text.setText(stringelement);
        return CustomView;
    }




}

Thanks Folks 谢谢大家

I added a few things to your CustomArrayAdaptor and tested it, see below: 我向您的CustomArrayAdaptor添加了一些内容并对其进行了测试,如下所示:

    public class CustomArrayAdapter extends ArrayAdapter<String> {

    int checkAccumulator;

    public CustomArrayAdapter(Context context, String [] Pairs) {
        super(context, R.layout.row_layout1, Pairs);
        checkAccumulator = 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View customView = inflater.inflate(R.layout.row_layout1, parent, false);
        String stringelement = getItem(position);
        TextView Text = (TextView) customView.findViewById(R.id.textelement);

        CheckBox checkBox1 = (CheckBox) customView.findViewById(R.id.Checkbox1);
        CheckBox checkBox2 = (CheckBox) customView.findViewById(R.id.Checkbox2);

        CompoundButton.OnCheckedChangeListener checkListener = new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        countCheck(isChecked);
                        Log.i("MAIN", checkAccumulator + "");
                    }
                };

        checkBox1.setOnCheckedChangeListener(checkListener);
        checkBox2.setOnCheckedChangeListener(checkListener);

        Text.setText(stringelement);
        return customView;
    }

   private void countCheck(boolean isChecked) {

        checkAccumulator += isChecked ? 1 : -1 ;
    }
}
  1. Added int checkAccumulator to keep track of how many items have been clicked. 添加了int checkAccumulator来跟踪单击了多少个项目。
  2. Added two CheckBox Views for the purpose of tracking checks. 添加了两个CheckBox视图,用于跟踪检查。
  3. Added CompoundButton.OnCheckedChangeListener that calls a new countCheck method everytime any checkBox is checked / unchecked. 添加了CompoundButton.OnCheckedChangeListener ,它在每次选中/取消选中任何复选框时都调用新的countCheck方法。 We pass the isChecked boolean to track whether we need to add or subtract from our checkAccumulator . 我们传递isChecked布尔值来跟踪是否需要在checkAccumulator中添加还是减去。
  4. Added countCheck function that adds or subtracts 1 dependent on the boolean. 添加了countCheck函数,该函数根据布尔值加减1 Sorry for the obfuscated code, for all those who aren't used to the ? 对不起,对于所有不习惯使用? conditional it is basically saying if true add 1, else add -1. 有条件的基本上是说如果为true,则加1,否则为-1。
  5. Not sure what you wanted to do with the check count, so I added a Log.i("MAIN", checkAccumulator + ""); 不知道要对检查计数做什么,因此我添加了Log.i(“ MAIN”,checkAccumulator +“”); just so you can watch the Android Monitor to ensure that it is actually working. 以便您可以观看Android Monitor来确保它确实在工作。

Should work for you. 应该为您工作。

First of all, you should recycle your views in the adapter: 首先,您应该在适配器中回收视图:

    LayoutInflater inflater = LayoutInflater.from(getContext());

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.row_layout1, parent, false);
    }

After that, I think you should save the state of each checkbox in your data source or just keep a list, the same size of your pair array, and save it there using the position you get from getView. 之后,我认为您应该将每个复选框的状态保存在数据源中,或者只是保留一个与对数组大小相同的列表,然后使用从getView获得的位置将其保存在该列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM