Android:按下后退按钮时,Textedit输入丢失

时间:2016-07-26 18:50:04

标签: android

我在listview中有一个EditText,我通过列表适配器填充,我的问题是,每当我在EditText中输入任何文本并按下后退按钮时,我的所有更改都会丢失,但在另一个位置的另一个EditText中(其中有没有列表视图)一切正常(所有更改都保留),为什么会发生这种情况以及如何摆脱这个问题?

edittext code>>

<EditText
    android:id="@+id/edit_card_list_editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/editbox_cursor_margin"
    android:layout_marginRight="@dimen/editbox_cursor_margin"
    android:layout_marginTop="@dimen/edit_card_list_mid_gap"
    android:background="@color/white"
    android:ems="10"
    android:enabled="true"
    android:fontFamily="@string/roboto_light_font"
    android:inputType="textNoSuggestions"
    android:textSize="@dimen/forty_eight_font" >

</EditText>

Listadapter代码&gt;&gt;

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View rowView = convertView;
    boolean convertViewWasNull = false;

    if(rowView == null){

        rowView = inflator.inflate(R.layout.edit_card_list_layout, parent, false);
        convertViewWasNull = true;

    }

    TextView titleview = (TextView) rowView.findViewById(R.id.edit_card_list_title);
    EditText detailview = (EditText) rowView.findViewById(R.id.edit_card_list_editText);

    if(title.get(position) != null){
        titleview.setText(title.get(position).toString());

        }
    if(detail.get(position) != null){
        detailview.setText(detail.get(position).toString());
        }else{
            detailview.setText("");
        }

    detailview.requestFocus();
    //InputMethodManager inputManager = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    //inputManager.restartInput(detailview);

    if(convertViewWasNull){

        detailview.addTextChangedListener(new GenericTextWatcher(detailview));

    }

    detailview.setTag(title.get(position).toString());
    return rowView;
}

private class GenericTextWatcher implements TextWatcher{

       private EditText view;
       private GenericTextWatcher(EditText view) {
            this.view = view;
        }

        public void afterTextChanged(Editable editable) {

            text = editable.toString();
            //save the value for the given tag :
            EditCardListAdapter.this.editTextValues.put((String) view.getTag(),text);

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }
    }

1 个答案:

答案 0 :(得分:0)

我看到您正在跟踪对该字段的修改,但在创建列表项视图时您没有使用这些编辑:

        if (detail.get(position) != null) {
            String orig = detail.get(position).toString();
            String edited = editTextValues.get(title.get(position).toString());
            detailview.setText(edited != null ? edited : orig);
        } else {
            detailview.setText("");
        }