2011-03-08 53 views
0

如何从给定复选框的onclick事件访问我的listview中的所有复选框。我想达到的目的是当选中的复选框的数量大于给定数量时,禁用所有未复位的复选框。Android:访问自定义适配器中复选框的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(); 

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

    } 
} 

回答

0

首先用onCheckChangedListener代替onClickListener。保持一个全局计数器,检查选中的朋友数量,并在构造函数中设置它的值。接下来在onCheckedChanged方法中,根据复选框是否已被选中来增加或减少计数器。如果计数器变得超过你的计数,那么使列表视图无效,并在getview方法中设置复选框的可见性为不可见(或消失)

+0

如果我想在达到限制时立即禁用未经检查的复选框,我必须在onClick(或onCheckChangedListener)中执行此操作,对吧?如何从onClick事件访问所有复选框,如何获取ListView中的所有子项。 – jul 2011-03-09 08:08:40

+0

是的。至于访问设置一些全球性的标志,表明未检查的框应该是不可见的,请致电listview.invalidate() – pankajagarwal 2011-03-09 09:55:26

+0

如何获得我的适配器中的列表视图? – jul 2011-03-09 10:15:16

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

希望这有助于