我正在开发一个应用程序,用户可以在其中填写报告和民意调查。在一个活动中,我有一个ListView,其中的子级包含EditTexts,Checkbox和TextView。
EditTexts和Checkbox的问题在于,滚动时会丢失它们具有的内容。我可以通过将检查状态保存在数组中来解决Checkbox问题,但是我无法使用EditTexts解决该问题。我知道有关此特定问题存在一些问题,但他们提供的解决方案似乎无效。所以这是我的适配器:
public class DiveProfileAdapter extends ArrayAdapter<DiveProfile> {
private Context mContext;
private List<DiveProfile> mTasks;
ViewHolder holder;
private ArrayList<Boolean> itemChecked = new ArrayList<>(); //for saving checkbox values
public DiveProfileAdapter(Context context, List<DiveProfile> objects) {
super(context, R.layout.row_dive_profile_divers_list_item, objects);
this.mContext = context;
this.mTasks = objects;
for (int i = 0; i < this.getCount(); i++) {
itemChecked.add(i, false); // initializes all items value with false
}
}
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
convertView = mLayoutInflater.inflate(R.layout.row_dive_profile_divers_list_item, null);
holder = new ViewHolder();
holder.diverAttended = (CheckBox) convertView.findViewById(R.id.diver_attended_checkbox);
holder.diveTime = (EditText) convertView.findViewById(R.id.dive_time_edit_text);
holder.depth = (EditText) convertView.findViewById(R.id.depth_edit_text);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
final DiveProfile diverObjectTask = mTasks.get(position);
holder.diverAttended.setChecked(diverObjectTask.isDiverAttended());
holder.diveTime.setText(diverObjectTask.getDiveTime());
holder.diverAttended.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v.findViewById(R.id.diver_attended_checkbox);
if (cb.isChecked()) {
itemChecked.set(position, true);
// do some operations here
} else if (!cb.isChecked()) {
itemChecked.set(position, false);
// do some operations here
}
}
});
holder.diverAttended.setChecked(itemChecked.get(position)); // this will Check or Uncheck the
holder.diveTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String txt = setDiveTimeDialog();
holder.diveTime.setText(txt);
mTasks.get(position).setCurrentText(txt); //add the setter to the current item's position
}
});
holder.diveTime.setText(diverObjectTask.getCurrentText());
return convertView;
}
static class ViewHolder {
CheckBox diverAttended;
EditText diveTime;
EditText depth;
}
private String setDiveTimeDialog() {
//final String[] items = finItems;
final String[] items = getContext().getResources().getStringArray(R.array.certification_level_items);
// Create list dialog with required parameters - context, title, and our array of items to fill the list.
CustomListDialog.Builder builder = new CustomListDialog.Builder(getContext(), "Dive Time", items);
// Now again we can use some extra methods on the builder to customise it more.
builder.titleAlignment(BaseDialog.Alignment.LEFT);
builder.itemAlignment(BaseDialog.Alignment.LEFT);
builder.titleColor(getContext().getResources().getColor(R.color.primary_color));
// Now we can build our dialog.
CustomListDialog customListDialog = builder.build();
// Finally we can show it.
customListDialog.show();
customListDialog.setListClickListener(new CustomListDialog.ListClickListener() {
@Override
public void onListItemSelected(int i, String[] strings, String s) {
result = s;
}
});
return result;
}
}
最佳答案
您应该执行以下操作:
final DiveProfile diverObjectTask = mTasks.get(position);
holder.diveTime.setText(diverObjectTask.getDiveTie());
//also set your CheckBox value
这些值变为空,因为您没有在
getView
中设置它(列表滚动时会自动调用getView
)更新
看着您的问题,我认为最简单的方法是在
DiveProfile
类中创建新变量,例如boolean isSelected
和String currentText
。之后,无需使用
itemChecked
这样的单独列表。您可以get
和set
当前项目的值。使用
getter
的示例:holder.diverAttended.setChecked(mTasks.get(position).getSelected()); // getSelected is isSelected getter method
并在您的
setSelected
setter
中调用isSelected
(holder.diverAttended
的onClick
)EditText
setter
的示例: holder.diveTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String txt = setDiveTimeDialog();
holder.diveTime.setText(txt);
mTasks.get(position).setcurrentText(txt); //add the setter to the current item's position
}
});
然后在
getCurrentText()
中调用getView