【问题标题】:How can I limit the number of checked checkboxes in my custom adapter?如何限制自定义适配器中选中复选框的数量?
【发布时间】:2011-03-09 09:08:24
【问题描述】:

我想限制列表视图中选中复选框的数量。我有一个带有选中复选框的全局数组,当它的大小大于给定限制时,我想禁用所有未选中的复选框。

由于我希望在选中最后一个允许的 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?
        */
        }


    }
}

【问题讨论】:

    标签: android listview checkbox


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 2013-09-30
    • 1970-01-01
    相关资源
    最近更新 更多