【问题标题】:How to count the number of checkboxes ticked which is inside a listview如何计算列表视图中勾选的复选框数量
【发布时间】:2016-02-28 23:30:01
【问题描述】:

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

所以基本上项目Layout是这样的

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

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

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

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




}

谢谢大家

【问题讨论】:

标签: java android list listview checkbox


【解决方案1】:

我在您的 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. 添加了 int checkAccumulator 以跟踪点击了多少项目。
  2. 添加了两个 CheckBox 视图用于跟踪检查。
  3. 添加了 CompoundButton.OnCheckedChangeListener,每次选中/取消选中任何复选框时都会调用一个新的 countCheck 方法。我们通过 isChecked 布尔值来跟踪我们是否需要从 checkAccumulator 中添加或减去。
  4. 添加了 countCheck 函数,根据布尔值加减 1。对于混淆的代码,对于所有不习惯 ?有条件的基本上是说如果为真加1,否则加-1。
  5. 不确定您想对检查计数做什么,所以我添加了一个 Log.i("MAIN", checkAccumulator + ""); 以便您可以观看 Android监控以确保它确实在工作。

应该适合你。

【讨论】:

  • 很高兴为您提供帮助。还有几点:确保您的 .xml 文件始终都是小写的。此外,关于 TextView TextView CustomView ... TextCustomView 应该是小写的。大写首字母保留给类定义,而不是类实例。甚至 StackOverflow 也被大写混淆了,并将它们像类一样着色。祝你好运。
  • 作为一个额外的问题,你能告诉我如何在stand1.xml 中将Score 的textview 设置为checkAccumulator 吗?感谢命名,对这个很新,所以会在一段时间后尝试修复它
  • 一种方法是使用界面。这是一个稍微复杂的过程,您可能想提出一个新问题。也许你可以从这里弄清楚:developer.android.com/training/basics/fragments/…
  • 干杯。应该可以从那里找出来。非常感谢您的帮助
  • @Selvin 我不同意,这只是解决方案的一部分。你的有效批评已经在这个线程上被指出了两次。我不认为任何提问者期望响应者重写整个应用程序。你怎么知道提问者没有为我们提供简化的代码库以便更容易回答问题?
【解决方案2】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2013-06-14
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 2012-06-10
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多