Android OnCheckChanged侦听器仅适用于customlistview中的第一个复选框

Android OnCheckChanged侦听器仅适用于customlistview中的第一个复选框,android,view,checkbox,custom-adapter,Android,View,Checkbox,Custom Adapter,自定义listview中的listener for复选框仅适用于第一个复选框。我认为这与getView()中的位置有关。我附上我的代码和这个问题,请建议我解决这个问题 public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; LayoutInflater inflater = context.getLayoutInflater

自定义listview中的listener for复选框仅适用于第一个复选框。我认为这与getView()中的位置有关。我附上我的代码和这个问题,请建议我解决这个问题

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

convertView是每个项的temlate,仅在第一次调用null时,您必须为每个项添加侦听器,如下所示:

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

使用以下链接解决您的问题@迪内什:问题是他将“holder.cb.setOnCheckedChangeListener(n….)”代码从初始的if条件移到了它的外部。因为只有当convert视图为null时,该代码才运行。这只是一个小错误,但花费了我很多钱。@2red13谢谢你。