[英]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 ;
}
}
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.