解决办法:
设置一个Map集合,用于保存第一个listview中数据的选中与否状态,然后根据这个状态设置给checkbox的状态,这样就不会混乱了。
第一步:Map
public Map<Integer,Boolean> isCheck = null;
第二步:初始化Map,把当前listview中数据给map的key,把value全部设置成false;
private void initMap(){
for (int i = 0; i < list.size(); i++) {
isCheck.put(i, false);
}
}
在点击checkbox的时候改变map的选中状态(在代码中把cb的点击事件给layout的整个item了)
vh.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(vh.cb.isChecked()){
vh.cb.setChecked(false);
}else{
vh.cb.setChecked(true);
}
}
});
vh.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
isCheck.put(position, true);
} else {
isCheck.put(position, false);
}
}
});
最后,才能设置checkbox的选状态,这个状态从map中取出
vh.cb.setChecked(isCheck.get(position));
全部代码如下
在Activity中
public class MainActivity extends AppCompatActivity {
private MyAdapter adapter;
private ListView listView;
private ArrayList list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=new ArrayList();
listView=(ListView)findViewById(R.id.listview);
initData();
adapter=new MyAdapter(this,list);
listView.setAdapter(adapter);
}
private void initData() {
for(int i=0;i<20;i++){
list.add("this is item"+i);
}
}
}
在Adapter中
public class MyAdapter extends BaseAdapter {
public Map isCheck = null;
private Context context;
private ArrayList list;
public MyAdapter(Context context, ArrayList list) {
this.context = context;
this.list = list;
isCheck = new HashMap();
initMap();
}
private void initMap(){
for (int i = 0; i < list.size(); i++) {
isCheck.put(i, false);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder vh;
if (convertView == null) {
vh = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
vh.tv = (TextView) convertView.findViewById(R.id.tv_text);
vh.cb = (CheckBox) convertView.findViewById(R.id.cb);
vh.layout= (LinearLayout) convertView.findViewById(R.id.layout);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(vh.cb.isChecked()){
vh.cb.setChecked(false);
}else{
vh.cb.setChecked(true);
}
}
});
vh.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
isCheck.put(position, true);
} else {
isCheck.put(position, false);
}
}
});
vh.tv.setText("this is item" + position);
vh.cb.setChecked(isCheck.get(position));
return convertView;
}
public static final class ViewHolder {
TextView tv;
CheckBox cb;
LinearLayout layout;
}
}
原因在listview的getView中没有else, 在listview的getView中如果写了if,就必须有else,要不就会出现各种问题(下面是显示聊天界面中有未读消息数和红点的显示getView)
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LinearLayout.inflate(context, R.layout.item_chatlist,
null);
holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
holder.tv_content = (TextView) convertView
.findViewById(R.id.tv_content);
holder.tv_count = (TextView) convertView
.findViewById(R.id.tv_count);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ChatListBean.Data chatList = chatData.get(position);
System.out.println("++++" + chatData.size());
holder.tv_name.setText("与 " + chatList.getPatName() + " 的回答");
holder.tv_time.setText(chatList.getLateTime());
holder.tv_content.setText(chatList.getContent());
if (Integer.parseInt(chatList.getNewCount()) == 0) {
holder.tv_count.setVisibility(View.INVISIBLE);
holder.tv_count.setBackgroundColor(Color.parseColor("#ffffff"));
} else {
holder.tv_count.setVisibility(View.VISIBLE);
holder.tv_count.setText(chatList.getNewCount());
holder.tv_count.setBackgroundResource(R.drawable.count_round);
}
return convertView;
}
checkbox的as代码下载地址
http://download.csdn.net/detail/androidxiaogang/9514909