1

嗨,我在列表视图中使用复选框,但是当我滚动列表视图时,复选框会自动取消选中或选中。

public class MyContactsAdapter extends BaseAdapter {

private LayoutInflater inflater;
private List<Friend> mAllFriends;
public MyContactsAdapter(Context context,List<Friend> allFriends) {

    inflater = LayoutInflater.from(context);
    this.mAllFriends =allFriends; 
}

@Override
public int getCount() {

    if(mAllFriends==null)
    {
        return 0;
    }else{
        return mAllFriends.size();
    }
}

@Override
public Object getItem(int position) {
    return mAllFriends.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder mHolder;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.contact_list_item, null);
        mHolder = new ViewHolder();
        mHolder.mProFileImage = (ImageView) convertView
                .findViewById(R.id.profleImage);
        mHolder.mPersonName = (TextView) convertView
                .findViewById(R.id.person_Name);
        mHolder.mEmailId = (TextView) convertView
                .findViewById(R.id.person_emailId);
        mHolder.mSelectPerson = (CheckBox)convertView.findViewById(R.id.personSelect);
        mHolder.mSelectAllLabel = (TextView)convertView.findViewById(R.id.personSelectAllLabel);
        convertView.setTag(mHolder);

    } else {

        mHolder = (ViewHolder) convertView.getTag();
    }
    if(position==0)
    {
        mHolder.mProFileImage.setVisibility(View.GONE);
        mHolder.mPersonName.setVisibility(View.GONE);
        mHolder.mEmailId.setVisibility(View.GONE);  
        mHolder.mSelectAllLabel.setVisibility(View.VISIBLE);
    }else{
        mHolder.mProFileImage.setVisibility(View.VISIBLE);
        mHolder.mPersonName.setVisibility(View.VISIBLE);
        mHolder.mEmailId.setVisibility(View.VISIBLE);
        mHolder.mSelectAllLabel.setVisibility(View.GONE);
    }
    mHolder.mProFileImage.setImageResource(R.drawable.ic_launcher);
    mHolder.mPersonName.setText(""+mAllFriends.get(position).getName());
    mHolder.mEmailId.setText(""+mAllFriends.get(position).getEmail());
    mHolder.mSelectPerson.setTag(position);
    mHolder.mSelectPerson.setChecked(mAllFriends.get(position).isSelected());
    mHolder.mSelectPerson.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            mAllFriends.get((Integer) buttonView.getTag()).setSelected(isChecked);
        }
    });


    return convertView;
}





private static class ViewHolder {

    ImageView mProFileImage;
    TextView mPersonName;
    TextView mEmailId;
    CheckBox mSelectPerson;
    TextView mSelectAllLabel;
}

}

4

0 回答 0