有很多类似的问题,但没有一个涵盖我的情况。 我有一个带有EditText的ListView,它处于编辑阶段。 如果我滚动列表以使我的编辑行不在视图中,则会丢失输入值。
当用户从一行移到另一行时,我已经使用OnFocusChangeListener来“存储”更改的值,但是当编辑行不在视图范围内时,不会触发此事件。
这是我的实际代码:
public enum FieldType {
UNDEFINED, TITLE, EDIT, CHECK;
}
public enum EditType {
INTEGER, FLOAT
}
private abstract class BaseField {
public FieldType type = FieldType.UNDEFINED;
}
private class TitleField extends BaseField {
public int titleID;
public TitleField(int titleID) {
this.type = FieldType.TITLE;
this.titleID = titleID;
}
}
private class EditField extends BaseField {
public String code;
public int captionID;
public String value;
public String unit;
public String decimal;
public String minimum;
public String maximum;
public EditField(String code, int captionID, String value, String unit, String decimal, String minimum, String maximum) {
this.type = FieldType.EDIT;
this.code = code;
this.captionID = captionID;
this.value = value;
this.unit = unit;
this.decimal = decimal;
this.minimum = minimum;
this.maximum = maximum;
}
}
private class CheckField extends BaseField {
public String code;
public int captionID;
public boolean value;
public String unit;
public CheckField(String code, int captionID, boolean value, String unit) {
this.type = FieldType.CHECK;
this.code = code;
this.captionID = captionID;
this.value = value;
this.unit = unit;
}
}
private class TitleFieldViewHolder {
public TextView textTitle = null;
}
private class EditFieldViewHolder {
public TextView textCode = null;
public TextView textCaption = null;
public EditText editValue = null;
public TextView textUnit = null;
}
private class CheckFieldViewHolder {
public TextView textCode = null;
public TextView textCaption = null;
public CheckBox checkValue = null;
public TextView textUnit = null;
}
private BaseField Fields[] = {
new TitleField(R.string.general_settings),
new EditField("PG-01", R.string.legs_number, "4", "", "", "", ""),
new EditField("PG-02", R.string.operating_mode, "0", "", "", "", ""),
new EditField("PG-03", R.string._500mm_position_mode, "3", "", "", "", ""),
new EditField("PG-04", R.string.enable_additional_lift, "0", "", "", "", ""),
new EditField("PG-05", R.string.mechanical_release_mode, "0", "", "", "", ""),
new EditField("PG-06", R.string.delta_count_mechanical_release, "50", "bit", "", "", ""),
new EditField("PG-07", R.string.max_misalignment_bridge, "150", "bit", "", "", ""),
new EditField("PG-08", R.string.min_delta_inclinometers, "10", "bit", "", "", ""),
new EditField("PG-09", R.string.max_bridge_height, "3000", "bit", "", "", ""),
new EditField("PG-10", R.string.min_bridge_height, "0", "bit", "", "", ""),
new EditField("PG-11", R.string.range_limit_approaching_bridge, "100", "bit", "", "", ""),
new EditField("PG-12", R.string.enable_input_sa, "0", "", "", "", ""),
new EditField("PG-13", R.string.enable_working_positions, "0", "", "", "", ""),
new TitleField(R.string.time_settings),
new EditField("PG-14", R.string.mechanical_release_time, "2.000", "s", "", "", ""),
new EditField("PG-15", R.string.bridge_stabilization_time, "2.000", "s", "", "", ""),
new EditField("PG-16", R.string.waiting_time_c1_start, "0.100", "s", "", "", ""),
new EditField("PG-17", R.string.waiting_time_ev2_start, "0.100", "s", "", "", ""),
new EditField("PG-18", R.string.waiting_time_ev3_start, "0.100", "s", "", "", ""),
new EditField("PG-19", R.string.waiting_time_c1_on, "0.100", "s", "", "", ""),
new EditField("PG-20", R.string.waiting_time_ev1_on, "0.100", "s", "", "", ""),
new EditField("PG-21", R.string.waiting_time_ev2_on, "0.100", "s", "", "", ""),
new EditField("PG-22", R.string.waiting_time_ev3_on, "0.100", "s", "", "", ""),
new EditField("PG-23", R.string.waiting_time_c1_off, "0.100", "s", "", "", ""),
new EditField("PG-24", R.string.waiting_time_ev1_off, "0.100", "s", "", "", ""),
new EditField("PG-25", R.string.waiting_time_ev2_off, "0.100", "s", "", "", ""),
new EditField("PG-26", R.string.waiting_time_ev3_off, "0.100", "s", "", "", ""),
};
private ListView mListView;
private ListViewAdapter mListViewAdapter;
void init() {
mListView = (ListView) findViewById(R.id.listView);
mListView.setItemsCanFocus(true);
mListViewAdapter = new ListViewAdapter();
mListView.setAdapter(mListViewAdapter);
}
public class ListViewAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ListViewAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return Fields.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
switch (Fields[position].type) {
case TITLE:
return 0;
case EDIT:
return 1;
case CHECK:
return 2;
}
return 1;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
switch (Fields[position].type) {
case TITLE:
TitleFieldViewHolder titleHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.title_field_item, null);
titleHolder = new TitleFieldViewHolder();
titleHolder.textTitle = (TextView) convertView.findViewById(R.id.textTitle);
convertView.setTag(titleHolder);
} else {
titleHolder = (TitleFieldViewHolder) convertView.getTag();
}
TitleField titleField = (TitleField) Fields[position];
titleHolder.textTitle.setText(getResources().getString(titleField.titleID));
break;
case EDIT:
EditFieldViewHolder editHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.edit_field_item, null);
editHolder = new EditFieldViewHolder();
editHolder.textCode = (TextView) convertView.findViewById(R.id.textCode);
editHolder.textCaption = (TextView) convertView.findViewById(R.id.textCaption);
editHolder.editValue = (EditText) convertView.findViewById(R.id.editValue);
editHolder.textUnit = (TextView) convertView.findViewById(R.id.textUnit);
convertView.setTag(editHolder);
} else {
editHolder = (EditFieldViewHolder) convertView.getTag();
}
final EditField editField = (EditField) Fields[position];
editHolder.textCode.setText(editField.code);
editHolder.textCaption.setText(getResources().getString(editField.captionID));
editHolder.editValue.setText(editField.value);
editHolder.textUnit.setText(editField.unit);
//we need to update adapter once we finish with editing
editHolder.editValue.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
EditText editValue = (EditText) v;
editField.value = editValue.getText().toString();
}
}
});
break;
case CHECK:
CheckFieldViewHolder checkHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.check_field_item, null);
checkHolder = new CheckFieldViewHolder();
checkHolder.textCode = (TextView) convertView.findViewById(R.id.textCode);
checkHolder.textCaption = (TextView) convertView.findViewById(R.id.textCaption);
checkHolder.checkValue = (CheckBox) convertView.findViewById(R.id.checkValue);
checkHolder.textUnit = (TextView) convertView.findViewById(R.id.textUnit);
convertView.setTag(checkHolder);
} else {
checkHolder = (CheckFieldViewHolder) convertView.getTag();
}
CheckField checkField = (CheckField) Fields[position];
checkHolder.textCode.setText(checkField.code);
checkHolder.textCaption.setText(getResources().getString(checkField.captionID));
checkHolder.checkValue.setChecked(checkField.value);
checkHolder.textUnit.setText(checkField.unit);
break;
}
return convertView;
}
}