1.选中前面的checkbox导致后的checkbox被选中
2.选中后下滑listview,再上滑时被选中的checkbox又变成未选中状态。
问题大都是因为对listview进行代码优化重用convertView导致的。
解决方法:
package se08.edu.com.example.hxdn.softwareengineeringdate;
import android.content.Context;
import android.support.annotation.NonNull;
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.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Created by hxdn on 2015/9/27.
*/
class MyAdapter extends BaseAdapter {
private Context mcontext;
private int pagesize=15;
private int page=1;
private LayoutInflater inflater=null;
private Person person[];
// private static ArrayList<String> selected;
private Map<Integer,String> selected;
public MyAdapter(Context c,Person p[],int page,int pagesize)
{
this.mcontext=c;
this.person=p;
this.page=page;
this.pagesize=pagesize;
this.inflater=LayoutInflater.from(c);
selected = new HashMap<Integer,String>();
}
public Map<Integer,String> getSelected()
{
return selected;
}
@Override
public int getCount() {
int c=(page-1)*pagesize;
if (person.length-c<pagesize)
{
return person.length-c;
}
else
{
return pagesize;
}
}
@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) {
ViewHolder viewHolder=null;
selected = new HashMap<Integer,String>();
if (convertView==null)
{
viewHolder=new ViewHolder();
convertView=inflater.inflate(R.layout.simple_item,parent,false);
viewHolder.iv=(ImageView)convertView.findViewById(R.id.iv);
viewHolder.name=(TextView)convertView.findViewById(R.id.name);
viewHolder.majorId=(TextView)convertView.findViewById(R.id.majorId);
viewHolder.tel=(TextView)convertView.findViewById(R.id.tel);
viewHolder.cb=(CheckBox)convertView.findViewById(R.id.check);
convertView.setTag(viewHolder);
}
else
{
viewHolder=(ViewHolder)convertView.getTag();
}
viewHolder.cb.setTag(position + pagesize * (page - 1));//设置标签
if (selected.containsKey(position + pagesize * (page - 1)))
//判断listview的item上的checkbox是否被选中,用position+pagesize*(page-1)作为唯一标识
{
viewHolder.cb.setChecked(true);
}
else
{
viewHolder.cb.setChecked(false);
}
if (MainActivity.editModel)
{
viewHolder.cb.setVisibility(View.VISIBLE);
}
else
{
viewHolder.cb.setVisibility(View.GONE);
}
viewHolder.iv.setImageResource(person[position + pagesize * (page - 1)].getImageId());
viewHolder.name.setText(person[position + pagesize * (page - 1)].getName());
viewHolder.tel.setText(person[position + pagesize * (page - 1)].getTel());
viewHolder.majorId.setText(person[position + pagesize * (page - 1)].getMajorId());
viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//OnCheckChangedListener监听CheckBox的状态,无论来自你的onClick事件还是其他。
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
if(!selected.containsKey(buttonView.getTag()))
//前面代码进行优化,会对converView进行复用,所以会导致当前面一个checkbox被选中后面的checkbox也被选中
//于是进行判断,如果selectd没有这个key值,则加入到selectd中
//之前出现的问题是当我选中第一个checkbox,第十二个也会被选中
// 加了这个判断后,即使第十二checkbox是因为复用converView导致被选中的,但selectd的key值已经包含了
//这个信息,selectd便不会将其加入。
//在前面用selectd进行判断的时候便会把第十二checkbox设置为false;
selected.put((Integer) buttonView.getTag(),person[position + pagesize * (page - 1)].getMajorId());
MainActivity.txtcount.setText("共选中:" + selected.size());
}
else
{
selected.remove((Integer) buttonView.getTag());
MainActivity.txtcount.setText("共选中:" + selected.size());
}
}
});
return convertView;
}
public class ViewHolder
{
private ImageView iv;
private TextView name;
private TextView majorId;
private TextView tel;
CheckBox cb;
}
}