2011-03-08 46 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 
     } 
     } 
    } 

希望這有助於