布局文件
<? xml version="1.0" encoding="utf-8" ?> < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:orientation ="horizontal" > < RelativeLayout android:id ="@+id/RelativeLayout01" android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:gravity ="center_horizontal" > < ImageView android:id ="@+id/ImageView01_checklayout" android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:layout_alignParentLeft ="true" > </ ImageView > < TextView android:id ="@+id/TextView01_checklayout" android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:layout_marginTop ="20px" android:layout_marginLeft ="70px" android:textSize ="20px" android:gravity ="center" > </ TextView > < CheckBox android:id ="@+id/CheckBox01_checklayout" android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:layout_alignParentRight ="true" android:layout_marginTop ="14px" ></ CheckBox > </ RelativeLayout > </ LinearLayout >
-
Java code
-
package
com.jingoal.mobile.android.jingoal.adapter;
import
java.util.ArrayList;
import
java.util.List;
import
com.jingoal.mobile.android.jingoal.R;
import
com.jingoal.mobile.android.jingoal.holder.CheckBoxViewHolder;
import
android.content.Context;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.util.Log;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.CheckBox;
import
android.widget.CompoundButton;
import
android.widget.ImageView;
import
android.widget.TextView;
import
android.widget.CompoundButton.OnCheckedChangeListener;
/**
* * 带复选框的listView的适配器 *
@author
Administrator *
*/
public
class
MyCheckBoxAdapter
extends
BaseAdapter {
//
用来将布局文件变成列表中的文件
private
LayoutInflater mInflater
=
null
;
private
Bitmap mIcon
=
null
;
private
Context myContext
=
null
;
private
List
<
People
>
data
=
null
;
private
ArrayList
<
People
>
checkeddata
=
new
ArrayList
<
People
>
();
private
ArrayList
<
CheckBox
>
allCheck
=
new
ArrayList
<
CheckBox
>
();
//
是否全选
private
boolean
isSelectAll
=
false
;
public
MyCheckBoxAdapter(Context context, List
<
People
>
data) {
this
.myContext
=
context;
this
.mInflater
=
LayoutInflater.from(
this
.myContext);
this
.mIcon
=
BitmapFactory.decodeResource(
this
.myContext.getResources(), R.drawable.icon1);
this
.data
=
data; } @Override
public
int
getCount() {
return
data.size(); } @Override
public
Object getItem(
int
position) {
//
TODO Auto-generated method stub
return
data.get(position); } @Override
public
long
getItemId(
int
position) {
//
TODO Auto-generated method stub
return
position; } @Override
public
View getView(
final
int
position, View view, ViewGroup parent) {
final
CheckBoxViewHolder holder;
//
布局的结构类
if
(view
==
null
)
//
如果现在没有视图
{
//
初始化布局文件和布局的组建结构
view
=
this
.mInflater.inflate(R.layout.check_layout,
null
); holder
=
new
CheckBoxViewHolder(); holder.mImageView
=
(ImageView) view.findViewById(R.id.ImageView01_checklayout); holder.mTextView
=
(TextView) view.findViewById(R.id.TextView01_checklayout); holder.mCheckBox
=
(CheckBox) view.findViewById(R.id.CheckBox01_checklayout);
//
设置是否被全选
if
(
this
.isSelectAll
==
true
) { holder.mCheckBox.setChecked(
true
); }
else
{ holder.mCheckBox.setChecked(
false
); } view.setTag(holder);
//
将布局指向一个模式
}
else
{ holder
=
(CheckBoxViewHolder) view.getTag(); }
//
设置布局里的组建元素
holder.mTextView.setText(data.get(position).toString()); holder.mImageView.setImageBitmap(mIcon);
if
(holder.mCheckBox.isChecked()) {
if
(
this
.checkeddata.size()
<
0
) {
this
.checkeddata.add(
this
.data.get(position)); }
else
{
if
(
this
.checkeddata.indexOf(
this
.data.get(position))
<
0
) {
this
.checkeddata.add(
this
.data.get(position)); } } } holder.mCheckBox .setOnCheckedChangeListener(
new
OnCheckedChangeListener() { @Override
public
void
onCheckedChanged(CompoundButton button,
boolean
isSelected) { ArrayList
<
People
>
list
=
MyCheckBoxAdapter.
this
.checkeddata;
if
(isSelected
==
true
) {
if
(list.size()
>
0
) { People people
=
MyCheckBoxAdapter.
this
.data .get(position);
if
(list.indexOf(people)
<
0
) { list.add(people); } }
else
{ list.add(MyCheckBoxAdapter.
this
.data .get(position)); } }
else
{
if
(list.size()
>
0
) { People people
=
MyCheckBoxAdapter.
this
.data .get(position);
for
(
int
i
=
0
; i
<
list.size(); i
++
) {
if
(people
==
list.get(i)) { list.remove(i); } } } } } });
return
view; }
/**
* 得到已选择的条数 *
@return
*/
public
int
getCheckedSize() {
return
this
.checkeddata.size(); }
/**
* 获得选择的数据列表 *
@return
*/
public
ArrayList
<
People
>
getChecked() {
return
this
.checkeddata; }
/**
* 是否全选 *
@return
*/
public
boolean
isSelectAll() {
return
isSelectAll; }
/**
* 清除所有的选项
*/
public
void
removeAllChecked() {
int
size
=
getCheckedSize();
for
(
int
i
=
0
; i
<
size ; i
++
) {
this
.checkeddata.removeAll(checkeddata); } }
/**
* 设置全选 *
@param
isSelectAll
*/
public
void
setSelectAll(
boolean
isSelectAll) {
this
.isSelectAll
=
isSelectAll; } }