我正在尝试创建一个我使用过自定义列表的应用。该列表包含文本视图和复选框。列表中包含少量30-40个数据。现在我想要的是当用户检查时特别的复选框,应该将与该复选框相关联的数据添加到arrayList.I尝试了但是当我滚动列表时,列表中的数据会消失。为什么会发生这种情况
码
public class EmailListAdapter extends BaseAdapter {
private Context context;
private ArrayList<EmailModel> data;
DbHandler dbHandler;
int[] emails;
ArrayList<String> emailSeperated;
public EmailListAdapter(Context context, ArrayList<EmailModel> data) {
this.context = context;
this.data = data;
emails = new int[data.size()];
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
dbHandler = new DbHandler(context);
emailSeperated = new ArrayList<String>();
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.email_custom_list, viewGroup, false);
holder.tvContact = (TextView) view.findViewById(R.id.tv_email_name);
holder.checkBox = (CheckBox) view.findViewById(R.id.cb_email_checkbox);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// if (emails[i] == 0) {
// holder.checkBox.setChecked(false);
// } else {
// holder.checkBox.setChecked(true);
// }
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton == holder.checkBox) {
if (b) {
emails[i] = 1;
//dbHandler.updateContactList(data.get(i).getUserID(), 1);
// emailSeperated.add(data.get(i).getEmail());
// Log.e("Email values", emailSeperated.toString());
// ShareWithinpocketDocs.etEmailLists.setText(emailSeperated.toString());
//
} else {
emails[i] = 0;
// emailSeperated.remove(data.get(i).getEmail());
// Log.e("Email values", emailSeperated.toString());
}
}
}
}
);
if (emails[i] == 0) {
holder.checkBox.setChecked(false);
emailSeperated.remove(data.get(i).getEmail());
Log.e("Email values", emailSeperated.toString());
ShareWithinpocketDocs.etEmailLists.setText(emailSeperated.toString());
} else {
holder.checkBox.setChecked(true);
emailSeperated.add(data.get(i).getEmail());
Log.e("Email values", emailSeperated.toString());
ShareWithinpocketDocs.etEmailLists.setText(emailSeperated.toString());
}
holder.tvContact.setText(data.get(i).getEmail());
return view;
}
private class ViewHolder {
TextView tvContact;
CheckBox checkBox;
}
}
当用户滚动列表时,数组中的数据将被删除。是否会发生这种情况?