如何限制自定义适配器中已选中复选框的数量?

时间:2011-03-09 09:08:25

标签: android listview checkbox

我想限制listview中已选中复选框的数量。我有一个带有选中Checkbox的全局数组,当它的大小超过给定限制时,我想禁用所有未经检查的Checkbox。

由于我希望在选中最后一个允许的复选框后立即禁用未选中的复选框,我认为必须在Checkbox onclick处理程序中完成,否则在再次调用getview之前不会刷新视图。是吗?

现在,为了能够禁用未经检查的,我必须访问列表视图的所有复选框。我该怎么做并从Checkbox onclick处理程序刷新视图?

这可能不是实现这一目标的最佳方式,因此任何建议都值得赞赏。

感谢。

public class FriendAdapter extends SimpleCursorAdapter implements OnClickListener {

    private Context mContext;
    private int mLayout;
    private Cursor mCursor;
    private int mNameIndex;
    private int mIdIndex;
    private LayoutInflater mLayoutInflater; 
    private final ImageDownloader imageDownloader = new ImageDownloader();  

    private final class ViewHolder {
        public TextView name;
        public ImageView image;
        public CheckBox checkBox;
    }

    public FriendAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);

        this.mContext = context;
        this.mLayout = layout;
        this.mCursor = c;
        this.mNameIndex = mCursor.getColumnIndex(WhipemDBAdapter.KEY_NAME);
        this.mIdIndex = mCursor.getColumnIndex(WhipemDBAdapter.KEY_FB_ID);
        this.mLayoutInflater = LayoutInflater.from(context);        
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (mCursor.moveToPosition(position)) {
            ViewHolder viewHolder;

            if (convertView == null) {
                convertView = mLayoutInflater.inflate(mLayout, null);

                viewHolder = new ViewHolder();
                viewHolder.name = (TextView) convertView.findViewById(R.id.contact_name);
                viewHolder.image = (ImageView) convertView.findViewById(R.id.contact_pic);
                viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
                viewHolder.checkBox.setOnClickListener(this);

                convertView.setTag(viewHolder);
            }
            else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            String name = mCursor.getString(mNameIndex);
            String fb_id = mCursor.getString(mIdIndex);         
            boolean isChecked = ((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id);

            viewHolder.name.setText(name);
            imageDownloader.download("http://graph.facebook.com/"+fb_id+"/picture", viewHolder.image);

            viewHolder.checkBox.setTag(fb_id);
            viewHolder.checkBox.setChecked(isChecked);
        }

        return convertView;
    }

    @Override
    public void onClick(View v) {

        CheckBox cBox = (CheckBox) v;
        String fb_id = (String) cBox.getTag();

        if (cBox.isChecked()) {
            if (!((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id))
                ((GlobalVars) mContext.getApplicationContext()).addSelectedFriend(fb_id);
        } else {
            if (((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id))
                ((GlobalVars) mContext.getApplicationContext()).removeSelectedFriend(fb_id);
        }

        int numSelectedCheckboxes = ((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id);

        if(numSelectedCheckBoxes > 6)
        {
        /* 
            How can I access all the checkboxes here to disable the unckecked ones?
        */
        }


    }
}

1 个答案:

答案 0 :(得分:0)

跟踪在单独的数组中检查哪些项目,不要跟踪视图元素,因为它们被回收。

public class FriendAdapter extends SimpleCursorAdapter implements OnClickListener {
  // Map item position to its check state (true/false)
  private Map<Integer, Boolean> checkStates;

  // ...

  public View getView(int position, View convertView, ViewGroup parent) {
        if (mCursor.moveToPosition(position)) {
            ViewHolder viewHolder;

            if (convertView == null) {
                // ...
            }
            else {
                // ...
            }

            // ...      
            Boolean isChecked = checkStates.get(position);

            viewHolder.checkBox.setTag(position);
            viewHolder.checkBox.setChecked(isChecked==null ? false : isChecked);
        }

        return convertView;
    }

    @Override
    public void onClick(View v) {
        CheckBox cBox = (CheckBox) v;
        int position = (Integer) cBox.getTag();

        checkStates.put(position, cBox.isChecked());

        // Here you iterate on checkStates to disable/enable according to the number 
        // of checked ones.


        // Update the UI with the new check states
        notifyDataSetChanged();
    }
}