Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何计算listview中勾选的复选框数_Java_Android_List_Listview_Checkbox - Fatal编程技术网

Java 如何计算listview中勾选的复选框数

Java 如何计算listview中勾选的复选框数,java,android,list,listview,checkbox,Java,Android,List,Listview,Checkbox,我有一个自定义的数组适配器,它正在处理列表视图。 每行有两个复选框。总共有五行。 我希望能够计算勾选的复选框数量,然后在Stand1中显示该数字 所以基本上项目布局是这样的 stand1内部有一个列表视图和一个文本视图。 java调用一个名为CustomArrayAdaptor的数组适配器。 我希望能够计算点击的复选框数量,并将数据发送回stand1 我对android的开发还很陌生,所以如果有人能把我推向正确的方向,那就太好了 这是我的密码 Stand1.xml <?xml versio

我有一个自定义的数组适配器,它正在处理列表视图。 每行有两个复选框。总共有五行。 我希望能够计算勾选的复选框数量,然后在Stand1中显示该数字

所以基本上项目布局是这样的

stand1内部有一个列表视图和一个文本视图。 java调用一个名为CustomArrayAdaptor的数组适配器。 我希望能够计算点击的复选框数量,并将数据发送回stand1

我对android的开发还很陌生,所以如果有人能把我推向正确的方向,那就太好了

这是我的密码

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" />
<?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>

行布局1.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" />
<?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

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);
    }
}
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;
    }




}
公共类Stand1扩展了片段{
ListView-mList;
@可空
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图根=充气机。充气(右侧布局。支架1,容器,错误);
mList=(ListView)root.findViewById(R.id.Stand1list);
返回根;
}
已创建ActivityState上的公共无效(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
populateListView();
}
私有void populateListView(){
字符串[]对={“对1”、“对2”、“对3”、“对4”、“对5”};
//构建适配器
ArrayAdapter=新的CustomArrayAdapter(getActivity(),pair);
mList.setAdapter(适配器);
}
}
CustomArrayAdaptor.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);
    }
}
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;
    }




}
公共类CustomArrayAdapter扩展了ArrayAdapter{
公共CustomArrayAdaptor(上下文,字符串[]对){
super(上下文,R.layout.row\u layout1,成对);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=LayoutInflater.from(getContext());
视图CustomView=充气机。充气(R.layout.row\u layout1,父级,false);
String stringelement=getItem(位置);
TextView Text=(TextView)CustomView.findViewById(R.id.textelement);
setText(stringelement);
返回自定义视图;
}
}

谢谢各位

首先,您应该在适配器中回收视图:

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

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

在此之后,我认为您应该在数据源中保存每个复选框的状态,或者只保留一个与配对数组大小相同的列表,并使用从getView获得的位置将其保存在那里。

首先,您应该在适配器中循环视图:

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

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

在此之后,我认为您应该在数据源中保存每个复选框的状态,或者只保留一个与配对数组大小相同的列表,并使用从getView获得的位置将其保存在那里。

我向您的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 ;
    }
}
公共类CustomArrayAdapter扩展了ArrayAdapter{
整数校验累加器;
公共CustomArrayAdapter(上下文,字符串[]对){
super(上下文,R.layout.row\u layout1,成对);
检查累加器=0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=LayoutInflater.from(getContext());
视图customView=充气机。充气(R.layout.row\u layout1,父级,false);
String stringelement=getItem(位置);
TextView Text=(TextView)customView.findViewById(R.id.textelement);
复选框checkBox1=(复选框)customView.findviewbyd(R.id.checkBox1);
复选框checkBox2=(复选框)customView.findviewbyd(R.id.checkBox2);
CompoundButton.OnCheckedChangeListener checkListener=新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
计数检查(已检查);
Log.i(“MAIN”,checkacculator+”);
}
};
checkBox1.setOnCheckedChangeListener(checkListener);
checkBox2.setOnCheckedChangeListener(checkListener);
setText(stringelement);
返回自定义视图;
}
私有void countCheck(布尔值已检查){
检查累加器+=已检查?1:-1;
}
}
  • 添加了int checkacculator,以跟踪单击了多少项
  • 添加了两个复选框视图,用于跟踪检查
  • 添加了CompoundButton.OnCheckedChangeListener,每次选中/取消选中任何复选框时,它都会调用一个新的countCheck方法。我们传递isChecked布尔值,以跟踪是否需要从检查累加器中进行加法或减法
  • 添加了取决于布尔值的加减1的countCheck函数。为那些不习惯使用这种语言的人,为那些模糊的代码感到抱歉?它基本上是说如果真加1,否则加-1
  • 不确定要对检查计数执行什么操作,因此我添加了一个Log.I(“MAIN”,checkacculator+”)这样你就可以观看Android监视器,确保它能正常工作

  • 应该适合您。

    我在您的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 ;
        }
    }
    
    公共类CustomArrayAdapter扩展了ArrayAdapter{
    整数校验累加器;
    公共CustomArrayAdapter(上下文,字符串[]对){
    super(上下文,R.layout.row\u layout1,成对);
    检查累加器=0;
    }
    @凌驾
    公共视图getView(int位置、视图转换视图、视图组父视图){
    LayoutInflater充气器=LayoutInflater.from(getContext());
    视图自定义视图=充气机。充气(右)