自定义ListView 适配器中实现监听控件

public View getView(int position, View convertView, ViewGroup parent) {  
        convertView = mInflater.inflate(layoutID, null);  
        for (int i = 0; i < flag.length; i++) {//备注1   
            if (convertView.findViewById(ItemIDs[i]) instanceof ImageView) {  
                ImageView iv = (ImageView) convertView.findViewById(ItemIDs[i]);  
                iv.setBackgroundResource((Integer) list.get(position).get(  
                        flag[i]));  
            } else if (convertView.findViewById(ItemIDs[i]) instanceof TextView) {  
                TextView tv = (TextView) convertView.findViewById(ItemIDs[i]);  
                tv.setText((String) list.get(position).get(flag[i]));  
            }else{  
                //...备注2   
            }  
        }  
        addListener(convertView);  
        return convertView;  
    }  
/** 
 * 童鞋们只需要将需要设置监听事件的组件写在下面这方法里就可以啦! 
 * 别的不需要修改! 
 * 备注3 
 */  
    public void addListener(View convertView) {  
        ((Button)convertView.findViewById(R.id.btn)).setOnClickListener(  
                new View.OnClickListener() {  
                    @Override  
                    public void onClick(View v) {  
                        new AlertDialog.Builder(MainActivity.ma)  
                        .setTitle("自定义通用SimpleAdapter")  
                        .setMessage("按钮成功触发监听事件!")  
                        .show();  
                    }  
                });  
        ((CheckBox)convertView.findViewById(R.id.cb)).  
        setOnCheckedChangeListener(new OnCheckedChangeListener() {  
            @Override  
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
                new AlertDialog.Builder(MainActivity.ma)  
                .setTitle("自定义通用SimpleAdapter")  
                .setMessage("CheckBox成功触发状态改变监听事件!")  
                .show();  
            }  
        });  
    }  

 

你可能感兴趣的:(ListView)