ListView嵌套CheckBox实现单选

ListView嵌套CheckBox实现单选_第1张图片

布局文件:



     
    
 
    
        
    
        
    
     

代码:

package com.gd.handphoto.adpter;
 
import java.util.List;
 
import android.app.Activity;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
 
import com.gd.handphoto.main.R;
import com.gd.handphoto.model.AddressModel;
 
public class AddressAdapter extends MyBaseAdapter {
    private List list;
    private LayoutInflater inflater;
    private SparseBooleanArray selectArray;
     
    public AddressAdapter(Activity context) {
        inflater = LayoutInflater.from(context);
        selectArray = new SparseBooleanArray();
    }
 
    public void setData(List list) {
        this.list = list;
    }
 
    @Override
    public int getCount() {
        return list.size();
    }
 
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
     
    public SparseBooleanArray getSelectArray() {
        return selectArray;
    }
 
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
 
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.address_list_item, parent ,false);
 
            holder.address = (TextView) convertView.findViewById(R.id.address);
            holder.address_name = (TextView) convertView.findViewById(R.id.address_name);
            holder.cityAndProvince = (TextView) convertView.findViewById(R.id.address_provinceAndCity);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.address_select);
 
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
 
        holder.address_name.setText(list.get(position).getAddressee());
        holder.address.setText(list.get(position).getAddress());
        holder.cityAndProvince.setText(list.get(position).getProvince() + " " + list.get(position).getCity());
 
        holder.checkBox.setTag(position);
         
        holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
 
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int tag = Integer.parseInt(buttonView.getTag().toString());
         
                        if (isChecked) {
                            for (int i = 0; i < getCount(); i++) {
                                if (tag == i) {
                                    selectArray.put(i, true);
                                }else {
                                    selectArray.put(i, false);
                                }
                            }
                             
                        } else {
                            selectArray.put(tag, false);
                        }
                 
                notifyDataSetChanged();
            }
        });
         
        holder.checkBox.setChecked(selectArray.get(position)); 
         
        return convertView;
    }
 
    static class ViewHolder {
        TextView address, address_name, cityAndProvince;
        CheckBox checkBox;
    }
 
}


转载于:https://my.oschina.net/wvliang/blog/360870

你可能感兴趣的:(ListView嵌套CheckBox实现单选)