【问题标题】:Get array of both checked and unchecked state of checkboxes within adapter获取适配器中复选框的选中和未选中状态的数组
【发布时间】:2014-05-12 17:44:39
【问题描述】:

我会在每个列表中创建包含复选框和文本视图的列表视图。我面临的问题是,我无法从适配器类中检索已检查和未检查值的数组。我在这里附上了某种代码。 这是我的适配器类

public class MyFacilityAdapter<T> extends BaseAdapter {

    ArrayList<T> mFList;
    LayoutInflater mInflate;
    SparseBooleanArray mySaprseBooleanArray;
    ArrayList<Integer> items = new ArrayList<Integer>();

    public MyFacilityAdapter(Context mContext, ArrayList<T> facilityList) {
        // TODO Auto-generated constructor stub
        mInflate = LayoutInflater.from(mContext);

        mySaprseBooleanArray = new SparseBooleanArray();

        mFList = new ArrayList<T>();
        this.mFList = facilityList;
    }

    /*public ArrayList<T> getCheckeditems() {

        ArrayList<T> tempArray = new ArrayList<T>();

        for (int i = 0; i < mFList.size(); i++) {
            if (mySaprseBooleanArray.get(i)) {
                tempArray.add(mFList.get(i));
            }
        }

        return tempArray;
    }*/
    ArrayList<Integer> getAdapterItems() {
        return items;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mFList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mFList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int postion, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            convertView = mInflate.inflate(R.layout.row_facility, null);
        }

        TextView tvFName = (TextView) convertView
                .findViewById(R.id.tv_facilityName);
        tvFName.setText(mFList.get(postion).toString());
        CheckBox chkBox = (CheckBox) convertView.findViewById(R.id.chkBox);
        chkBox.setTag(postion);
        /*chkBox.setChecked(mySaprseBooleanArray.get(postion));*/
        chkBox.setOnCheckedChangeListener(mChkChangedListener);

        return convertView;
    }

    OnCheckedChangeListener mChkChangedListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            CheckBox chk = (CheckBox) buttonView;
            // TODO Auto-generated method stub
            if (chk.isChecked()) {
                items.set(chk.getTag(), 1);
            } else {
                items.set(chk.getTag(), 0);
            }
            /*mySaprseBooleanArray.put((Integer) buttonView.getTag(), isChecked);*/
        }
    };

}

我将代码修改为 [item.set(chk.getTag(), 1)] 那样。但是一旦我选中复选框,它就会触发 Indexoutofbounds 异常。我在这里附上了logcat。

04-02 01:21:53.002: E/AndroidRuntime(1019): FATAL EXCEPTION: main
04-02 01:21:53.002: E/AndroidRuntime(1019): Process: com.example.homeyapp, PID: 1019
04-02 01:21:53.002: E/AndroidRuntime(1019): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-02 01:21:53.002: E/AndroidRuntime(1019):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
04-02 01:21:53.002: E/AndroidRuntime(1019):     at java.util.ArrayList.set(ArrayList.java:481)
04-02 01:21:53.002: E/AndroidRuntime(1019):     at com.example.homeyapp.MyFacilityAdapter$1.onCheckedChanged(MyFacilityAdapter.java:95)
04-02 01:21:53.002: E/AndroidRuntime(1019):     at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
04-02 01:21:53.002: E/AndroidRuntime(1019):     at android.widget.CompoundButton.toggle(CompoundButton.java:87)

【问题讨论】:

    标签: android arrays checkbox


    【解决方案1】:

    想通了。 首先将整数 1 或 0 放入 SparseBoolean 数组中,如:

    if (chk.isChecked()) {
                    mySaprseBooleanArray.put((Integer) chk.getTag(), isChecked);
                } else {
                    mySaprseBooleanArray.put((Integer) chk.getTag(), isChecked);
                }
    

    那么, 为了得到检查和未检查的项目数组:

    public ArrayList<Integer> getCheckeditems() {
    
            ArrayList<Integer> items = new ArrayList<Integer>();
            for (int i = 0; i < mFList.size(); i++) {
                Boolean temp = mySaprseBooleanArray.get(i);
                if (temp) {
                    items.add(1);
                } else {
                    items.add(0);
                }
            }
    
            return items;
        }
    

    这里的项目返回为 100101010; 1 为选中,0 为未选中。

    【讨论】:

      【解决方案2】:

      'items' ArrayList 需要预先填充与列表中的行数相同的元素。 (现在,每次用户选中或取消选中任何行时,您都会在 ArrayList 中添加一个 new 整数元素——这没有任何意义。)

      如果您执行上述操作,则 onCheckChanged() 可能如下所示:

          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              CheckBox chk = (CheckBox) buttonView;
              if (chk.isChecked()) {
                  items.set(chk.getTag(), 1);
              } else {
                  items.set(chk.getTag(), 0);
              }
              /*mySaprseBooleanArray.put((Integer) buttonView.getTag(), isChecked);*/
          }
      

      【讨论】:

      • 我按照你说的修改了代码。但它似乎仍然不是 gud。以及如何准确地预填充“项目”??
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      相关资源
      最近更新 更多