EditText 在 ListView 中滚动时丢失内容 [英] EditText loses content on scroll in ListView

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

问题描述

我有一个带有 EditText 的列表项,我不知道会有多少项.当我在 EditText 中输入一些文本,然后向下滚动 ListView 时遇到问题,再次向上滚动后,我的第一个 EditText 中没有文本,或者 ListView 中的其他 EditText 中有一些文本.

I have list item with EditText in it, I dont know how many items there will be. I have a problem when I enter some text in EditText, and then scroll down a ListView, after I've scroll up again there is no text in my first EditText, or there is some text from other EditText from ListView.

我尝试过 TextWatcher,并将数据保存到数组,但问题是 ListView 中返回的视图位置并不总是正确的,所以我从数组中丢失了一些数据.-.-

I've tried TextWatcher, and saving data to array, but problems is that returned position of view in ListView isnt always right, so I lost some data from array. -.-

如何在ListView中检测视图的正确位置?

How to detect correct position of view in ListView?

例如:

如果我在 ListView 中有 10 个项目,而目前只有 5 个是可见的.适配器返回位置从 0 到 4...那没问题.当我向下滚动第 6 项的位置是 0...wtf?我从位置 0 的数组中丢失数据:)

If I have 10 items in ListView, and only 5 of them are currently visible. Adapter return position from 0 to 4...thats ok. When I scroll down position of item 6 is 0...wtf? and i lose data from array on position 0 :)

我正在使用 ArrayAdapter.

Im using ArrayAdapter.

请帮忙.

这是一些代码:

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

    tmp_position = position;

    if (convertView == null) {

        holder = new ViewHolder();

        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.element_in_game, null);

        holder.scoreToUpdate = (EditText) convertView
                .findViewById(R.id.elementUpdateScore);

        holder.scoreToUpdate.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
                scoresToUpdate[tmp_position] = s.toString();
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        initScoresToUpdateEditTexts(holder.scoreToUpdate, hint);

        convertView.setTag(holder);

    } else {

        holder = (ViewHolder) convertView.getTag();

        holder.scoreToUpdate.setText(scoresToUpdate[tmp_position]);

    }

    return convertView;
}

推荐答案

您应该尝试以下方式:

if (convertView == null) {

        holder = new ViewHolder();

        LayoutInflater vi = (LayoutInflater)    
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.element_in_game, null);

        holder.scoreToUpdate = (EditText) convertView
                .findViewById(R.id.elementUpdateScore);


        convertView.setTag(holder);

    } else {

        holder = (ViewHolder) convertView.getTag();

    }
   //binding data from array list
   holder.scoreToUpdate.setText(scoresToUpdate[position]);
   holder.scoreToUpdate.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
                //setting data to array, when changed
                scoresToUpdate[position] = s.toString();
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    return convertView;
}

说明:ListView 的回收行为,实际上是擦除所有与其 Row Item 绑定的数据,当它消失时.因此,您需要再次绑定数据,从任何方向进入视野中的每一行新行.此外,当 EditText 文本更改时,您还必须将值保留在某些数据结构中,以便稍后再次行数据绑定,您可以提取位置数据.可以使用数组ArrayList来保存edittext数据,也可以用来绑定数据.

Explanation: Recycling behavior of ListView, actually erase all data bind with its Row Item, when go out of vision. So every new row coming in in vision from whatever direction you need to bind data again. Also, when EditText text change you have to keep values also in some data structure, so later on again row data binding you can extract data for position. You can use, array ArrayList for keeping edittext data, and also can use to bind data.

在 Recyclerview Adapter 中使用类似的行为,背后的逻辑你需要了解适配器的行数据的数据绑定.

Similar behavior use in Recyclerview Adapter, behind logic you need to know about data binding of row data of adapters.

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

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