Android:访问自定义适配器中复选框的onclick事件中的所有复选框

时间:2011-03-08 18:58:53

标签: android listview checkbox onclick

如何从给定复选框的onclick事件访问listview中的所有复选框。我想要实现的是当复选的复选框数大于给定数量时禁用所有未签名的复选框。

谢谢!

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

        /* 
            How can I access all the checkboxes here?
        */

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

    }
}

2 个答案:

答案 0 :(得分:0)

首先使用onCheckChangedListener代替onClickListener。为所检查的朋友数量保留一个全局计数器,并在构造函数本身中设置其值。 onCheckedChanged方法中的下一个方法是根据是否已选中复选框来递增或递减计数器。如果计数器超过您的计数,则使列表视图无效,并在getview方法中将复选框的可见性设置为不可见(或已消失)

答案 1 :(得分:0)

我认为你可以通过在getView方法中获取视图的全局引用来实现这一目的。

所以它应该是这样的:

View myRow;
public View getView(int position, View convertView, ViewGroup parent) {
myRow=convertView;

//all other stuff
}

之后,假设ListView行的根视图是RelativeLayout,您可以尝试在onClick方法中使用此代码:

RelativeLayout rowRootView=(RelativeLayout)myRow.getRootView();
for(int i=0;i<rowRootView.getChildCount();i++){
    if(rowRootView.getChildAt(i).getClass()==CheckBox.class){
        //do your stuff
    }
}

也许它应该适用于此代码

 @Override
    public void onClick(View v) {
        RelativeLayout rowRootView=(RelativeLayout)v.getRootView();
        for(int i=0;i<rowRootView.getChildCount();i++){
        if(rowRootView.getChildAt(i).getClass()==CheckBox.class){
        //do your stuff
        }
         }
    }

希望这个帮助