The OnCheckChanged listener works only for the first checkbox in a customlistview

北城余情 提交于 2019-12-29 09:35:13

问题


The listener for checkbox in my custom listview works only for the first checkbox. I think this has something to do with the position in getView(). I'm attaching my code with this question please suggest me a work around for this problem.

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

回答1:


convertView is a temlate for every item and only at the first call null, you have to add the Listener for each item like this:

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


来源:https://stackoverflow.com/questions/9306726/the-oncheckchanged-listener-works-only-for-the-first-checkbox-in-a-customlistvie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!