2012-02-16 27 views
0

我的自定義列表視圖中的複選框偵聽器僅適用於第一個複選框。我認爲這與getView()中的位置有關。我附上我的代碼與這個問題,請建議我解決這個問題。OnCheckChanged偵聽器僅適用於自定義列表視圖中的第一個複選框

public View getView(final int position, View convertView, ViewGroup parent) { 
    final ViewHolder holder; 
    LayoutInflater inflater = context.getLayoutInflater(); 
    if(convertView==null) 
    { 
     convertView = inflater.inflate(R.layout.custom_list, null); 
     holder = new ViewHolder(); 
     holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title_text); 
     holder.txtViewDescription = (TextView)convertView.findViewById(R.id.description_text); 
     holder.cb=(CheckBox) convertView.findViewById(R.id.cb); 
     convertView.setTag(holder); 
     holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      String Channel=holder.txtViewTitle.getText().toString(); 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if(position==0) 
      { 
       //works 
      } 
      else 
      if(position==1) 
      { 
       //doesn't work 
      } 
     }); 
    } 
    else 
    { 
     holder=(ViewHolder)convertView.getTag(); 
    } 

    holder.txtViewTitle.setText(title[position]); 
    holder.txtViewDescription.setText(description[position]); 
    holder.txtViewDescription.setFocusable(false); 
    holder.txtViewTitle.setFocusable(false); 

    return convertView; 
} 
+0

使用下面的鏈接來解決烏爾問題.. .. http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkbox-through-list – himanshu 2012-02-16 06:49:16

回答

2

convertView是每一個項目,只有在第一次調用空一temlate,你必須添加的偵聽器這樣每個項目:

public View getView(final int position, View convertView, ViewGroup parent) { 
// TODO Auto-generated method stub 
final ViewHolder holder; 
LayoutInflater inflater = context.getLayoutInflater(); 
if(convertView==null) 
{ 
    convertView = inflater.inflate(R.layout.custom_list, null); 
    holder = new ViewHolder(); 
    holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title_text); 
    holder.txtViewDescription = (TextView)  
    convertView.findViewById(R.id.description_text); 
    holder.cb=(CheckBox) convertView.findViewById(R.id.cb); 
    convertView.setTag(holder); 

} else { 
    holder=(ViewHolder)convertView.getTag(); 
} 
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    String Channel=holder.txtViewTitle.getText().toString(); 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    // TODO Auto-generated method stub 
    if(position==0) 
     { 
     //works 
     }else if(position==1){ 
      //doesn't work 
     } 
    }); 
holder.txtViewTitle.setText(title[position]); 
holder.txtViewDescription.setText(description[position]); 
holder.txtViewDescription.setFocusable(false); 
holder.txtViewTitle.setFocusable(false); 
return convertView; 
} 
+0

我看不到你的代碼有任何改變。 – Dinesh 2012-02-16 07:03:42

+0

@Dinesh:chnage是他將「holder.cb.setOnCheckedChangeListener(n .....」)代碼從最初的if條件移動到了它之外,因爲只有在轉換視圖爲空的情況下才運行此代碼 – akkilis 2012-02-16 07:30:48

+0

不錯工作完成@ 2red13,upvote :) – akkilis 2012-02-16 07:31:12

相關問題