最近自己在编写有关SIM卡管理的软件做练习,其中使用到了ListView与CheckBox的的组合,遇到了和大家有同样的问题:

1.选中一个checkbox对应位置的其他checkbox也会被选中

2.选中一个checkbox之后,滑动滚动条,之前选中的checkbox会莫名其妙的取消选中

于是网上搜之,具体的问题所在,网上答得也是很含糊,由于接触android时间不长,

其中的原因我也不是很清楚,不过总的来说代码还是搞定了

写好的代码供大家参考下:

import java.util.HashMap;

import java.util.List;

import com.rice.activity.R;

import com.rice.domain.ViewHolder;

import android.content.Context;

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.TextView;

import android.widget.CompoundButton.OnCheckedChangeListener;

public class MyAdapter extends BaseAdapter

{

private Context context;

private

List

Object>> data;

//用来记录所有ListView记录对应checkbox的状态

public HashMap

Boolean> isSelected;

private int resource;

private int to[];

private String from[];

private LayoutInflater inflater = null;

private ViewHolder holder = null;

//构造函数

public MyAdapter(Context context,

List

Object>> data, int resource, String

from[], int to[])

{

this.context = context;

this.data = data;

this.resource = resource;

this.from = new

String[from.length];

this.to = new

int[to.length];

System.arraycopy(from, 0,

this.from, 0, from.length);

System.arraycopy(to, 0,

this.to, 0, to.length);

init();

}

public HashMap

Boolean> getIsSelected()

{

return isSelected;

}

//初始化设置所有checkbox都为未选择状态

private void init()

{

isSelected = new

HashMap();

for (int i = 0; i

< data.size(); i++)

{

isSelected.put(i,

false);

}

}

@Override

public int getCount()

{

return data.size();

}

@Override

public Object getItem(int arg0)

{

return data.get(arg0);

}

@Override

public long getItemId(int arg0)

{

return 0;

}

@Override

public View getView(final int position, View

view, ViewGroup arg2)

{

holder = null;

if(null == holder)

{

//总是新建一个ViewHolder对象,用来保存每一个listview条目的信息

holder = new

ViewHolder();

if(null ==

view)

{

inflater

= LayoutInflater.from(context);

view

= inflater.inflate(resource, null);

}

holder.name =

(TextView) view.findViewById(R.id.name);

holder.mobile

= (TextView) view.findViewById(R.id.mobile);

holder.checkBox

= (CheckBox) view.findViewById(R.id.item_checkBox);

}

HashMap

Object> map = data.get(position);

if(null != map)

{

String name =

(String) map.get("name");

String mobile

= (String) map.get("mobile");

holder.name.setText(name);

holder.mobile.setText(mobile);

}

//添加checkBox监听

holder.checkBox.setOnCheckedChangeListener(new

OnCheckedChangeListener()

{

@Override

public void

onCheckedChanged(CompoundButton arg0, boolean isCheck)

{

if(isCheck)

{

isSelected.put(position,

true);

//System.out.println("add

checked=" + position);

}

else

if(!isCheck)

{

isSelected.put(position,

false);

//System.out.println("remove

checked=" + position);

}

}

});

//根据isSelected中记录的信息,设置checkbox的状态

holder.checkBox.setChecked(isSelected.get(position));

return view;

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐