失去的EditText内容上滚动的ListView控件 [英] EditText loses content on scroll in ListView

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

问题描述

我有一个EditText上的列表项的话,我不知道有多少项目会出现。 我有一个问题,当我进入的EditText一些文本,然后向下滚动一个ListView后,我已经向上滚动再次出现在我的第一个EditText上没有文字,或者有一些文字来自其他的EditText从ListView控件。

我已经试过TextWatcher,并且将数据保存到数组,但问题是,鉴于ListView的心不是返回的位置永远是对的,所以我从阵列丢失一些数据。 -.-

如何检测的ListView的观点正确位置?

例如:

如果我有10个项目在ListView中,只有5人是当前可见。 适配器返回位置,从0到4 ...那好吧。 当我向下滚动项目的第6位为0 ...跆拳道?我失去的位置,从0阵列数据:)

即时通讯使用ArrayAdapter。

请帮忙。

下面是一些code:

 公开查看getView(INT位置,查看convertView,ViewGroup中父){

    tmp_position =位置;

    如果(convertView == NULL){

        持有人=新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(新TextWatcher(){

            @覆盖
            公共无效onTextChanged(CharSequence中,诠释开始,
                    INT之前,诠释计数){
                scoresToUpdate [tmp_position] = s.toString();
            }

            @覆盖
            公共无效beforeTextChanged(CharSequence中,诠释开始,
                    诠释计数,之后INT){

            }

            @覆盖
            公共无效afterTextChanged(编辑S){

            }
        });

        initScoresToUpdateEditTexts(holder.scoreToUpdate,提示);

        convertView.setTag(保持器);

    } 其他 {

        支架=(ViewHolder)convertView.getTag();

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

    }

    返回convertView;
}
 

解决方案

你应该尝试在如下这样:

 如果(convertView == NULL){

        持有人=新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(保持器);

    } 其他 {

        支架=(ViewHolder)convertView.getTag();

    }
   //从数组列表绑定数据
   holder.scoreToUpdate.setText(scoresToUpdate [位置]);
   holder.scoreToUpdate.addTextChangedListener(新TextWatcher(){

            @覆盖
            公共无效onTextChanged(CharSequence中,诠释开始,
                    INT之前,诠释计数){
                //改变时设置的数据阵列,
                scoresToUpdate [位置] = s.toString();
            }

            @覆盖
            公共无效beforeTextChanged(CharSequence中,诠释开始,
                    诠释计数,之后INT){

            }

            @覆盖
            公共无效afterTextChanged(编辑S){

            }
        });

    返回convertView;
}
 

说明: ListView控件的回收行为,实际上擦除所有数据绑定其行项目,当走出视野。 所以,每一个新的行即将在无论从任何方向,你需要重新绑定数据愿景。 此外,当文字的EditText改变你必须保持价值也在某些数据结构,所以后来再行数据绑定可以提取数据的位置。 您可以使用,数组ArrayList中保持的EditText数据,也可以使用数据绑定。

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

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.

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. -.-

How to detect correct position of view in ListView?

For example:

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 :)

Im using ArrayAdapter.

Please help.

Here's some code:

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;
}

解决方案

you should try in this way as below:

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;
}

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.

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天全站免登陆