OnCheckChanged 侦听器仅适用于 customlistview 中的第一个复选框 [英] The OnCheckChanged listener works only for the first checkbox in a customlistview

查看:17
本文介绍了OnCheckChanged 侦听器仅适用于 customlistview 中的第一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义列表视图中复选框的侦听器仅适用于第一个复选框.我认为这与 getView() 中的位置有关.我在此问题中附上了我的代码,请建议我解决此问题.

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

推荐答案

convertView 是每个项目的模板,只有在第一次调用 null 时,你必须为每个项目添加监听器,如下所示:

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

这篇关于OnCheckChanged 侦听器仅适用于 customlistview 中的第一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆