布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/address_select"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="@+id/address_name"
android:layout_toRightOf="@id/address_select"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="@dimen/hao_15dp"
android:text="DASHDK"
android:padding="5dp"/>
<TextView
android:id="@+id/address_provinceAndCity"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="@dimen/hao_13dp"
android:layout_alignParentRight="true"
android:textColor="@color/deep_grey"
android:padding="5dp"
android:text="上海市 虹口区"/>
<TextView
android:id="@+id/address"
android:layout_toRightOf="@id/address_select"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="@color/deep_grey"
android:textSize="@dimen/hao_13dp"
android:layout_below="@id/address_name"
android:text="dasdhaidhaid"
android:padding="5dp"/>
</RelativeLayout>
代码:
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<AddressModel> list;
private LayoutInflater inflater;
private SparseBooleanArray selectArray;
public AddressAdapter(Activity context) {
inflater = LayoutInflater.from(context);
selectArray = new SparseBooleanArray();
}
public void setData(List<AddressModel> 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;
}
}