在的EditText的ListView是由onTextChanged滚动时更新 [英] EditText in ListView is updated by onTextChanged when scrolling

查看:242
本文介绍了在的EditText的ListView是由onTextChanged滚动时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个答案,但解决方案似乎并没有为我工作。我有一个TextView并以列表项目的EditText。我想,当用户编辑他们更新的EditTexts存储的值。

I have been searching for an answer to this, but the solutions don't seem to work for me. I have a TextView and an EditText in a list item. I am trying to update the stored values for the EditTexts when the user edits them.

@Override
public View getView(int index, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    final int pos = index;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.user_details_list_row, parent,false);
        holder = new ViewHolder();
        holder.mCaptionTextView  = (TextView)convertView.findViewById(id.user_detail_row_caption);
        holder.mDetailEditText = (EditText)convertView.findViewById(id.user_detail_row_value);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.mDetailEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {              
            mUserDetails.set(pos, s.toString());
        }
    });     

    holder.mCaptionTextView.setText(mUserCaptions.get(index));
    holder.mDetailEditText.setText(mUserDetails.get(index),BufferType.EDITABLE);

    return convertView;
}

public static class ViewHolder{
    public TextView mCaptionTextView;
    public EditText mDetailEditText;
}

当我这样做,滚动触发TextWatcher并更新值,与其他EditTexts的一个重写正确的文本复制的文本。

When I do this, scrolling triggers the TextWatcher and updates the values, overwriting correct text with duplicate text from one of the other EditTexts.

而不是一个TextWatcher的,我也试过这个code:

Instead of a TextWatcher, I've also tried this code:

holder.mDetailEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
    if (!hasFocus){
        EditText et =   (EditText)v.findViewById(id.user_detail_row_value);
        mUserDetails.set(index, et.getText().toString().trim());
    }
}
});

和它还更新了错误的EditTexts。我缺少的是在这里吗?

And it also updates the wrong EditTexts. What am I missing here?

编辑:也试过这样的:

        final ViewHolder testHolder = holder; 
    holder.mDetailEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus){
                EditText et =   (EditText)v.findViewById(id.user_detail_row_value);
                mUserDetails.set(testHolder.ref, et.getText().toString().trim());
            }
        }
    });

它纠正了滚动变化的问题​​,我所看到的,但现在编辑EditTexts之一后,它改变了一堆的人也是如此。

It corrects the scrolling change issue I was seeing, but now after editing one of the EditTexts, it changes a bunch of the others as well.

推荐答案

检查你的索引值,如果你正在使用的行位置相匹配。如果您使用的支架模式,也许该值不会改变从0到7-8这取决于你的屏幕尺寸。类似的事情发生在我身上一次,我想我通过实施 getCount将办法解决它。通过这种方式,行50给我的位置50,而不是7(例如)。

Check your index value if it matches with the row position you are working on. If you use the holder pattern maybe that value wont change from 0 to 7-8 depending in your screen size. Something like that happened to me once, I think I solved it by implementing the getCount method. This way the row 50 gave me the position 50 and not 7 (for example).

这篇关于在的EditText的ListView是由onTextChanged滚动时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆