0

我有ListView一个自定义适配器。每行都有一个EditText. 每个EditText都有一个与该行相关的初始值,用户可以使用 更改此值EditTexts

我想存储这些更改的值。TextWatcher我为每个都定义了,EditText但它不能正常工作,尤其是当我滚动或过滤数据时。

我怎样才能做到这一点?

我是否使用了合适的方法?

4

1 回答 1

0

问题出在TextWatcher. 我使用以下方法解决了它 OnKeyListener

private boolean programmaticalTextChange = false;

private KeyEvent UnknownKey = new KeyEvent(KeyEvent.ACTION_DOWN,
            KeyEvent.KEYCODE_UNKNOWN);

    public class MyTextWatcher implements TextWatcher
    {
        private EditText editText;

        public MyTextWatcher(EditText editText)
        {
            this.editText = editText;
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }

        @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) {
            if (!programmaticalTextChange)
                editText.dispatchKeyEvent(UnknownKey);
        }
    }


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
       //...
       myEditText.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                updateItem(item, v, rowView);
                return false;
            }
        });
        myEditText.addTextChangedListener(new MyTextWatcher(myEditText));

}

protected void updateItem(MyClass item, View v) {
        try {
            item.Qty = Double.valueOf(((EditText) v).getText()
                    .toString());
        } catch (Exception e) {

        }
    }
于 2013-02-12T09:06:36.347 回答