在自定义列表视图编辑文本丢失的滚动值 [英] Edit Text in custom List View Loses Value on Scroll

查看:85
本文介绍了在自定义列表视图编辑文本丢失的滚动值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是重复的问题,但我没有找到合适的答案。
在具有我的自定义列表视图两个TextView的一Editext,当我插在编辑文本值,然后滚动我亏编辑的文本价值。
当我通过列表适配器传递字符串值适配器,然后它会显示空白的编辑文本。

I know it's duplicate question, but I didn't find proper answer. In my custom list-view having a two textview and one Editext, when I inserted a value in edit text and scroll then I loss value of edited text. And when I pass string value for an adapter through list adapter then it will show blank edit text.

下面我适配器code:

Here my Adapter code:

class CreateAdapter extends ArrayAdapter<String> {
        String[] strItecode=null;
        String[] strItem;
        String[] strQuantity,text;
        Context context;
        int temp;
        CreateAdapter(Context context, String[] strItemcode, String[] strItem,
                String[] strQauntity) {
            super(context, R.layout.create_list_item, R.id.txtItemcode, strItemcode);
            this.context = context;
            this.strItecode = strItemcode;
            this.strItem = strItem;
            this.strQuantity = strQauntity;
            text= new String[strItem.length];
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            temp=position;
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {

                holder = new ViewHolder();
                convertView = mInflater
                        .inflate(R.layout.create_list_item, parent, false);

                holder.txtItecode = (TextView) convertView
                        .findViewById(R.id.txtItemcode);
                holder.txtItem = (TextView) convertView
                        .findViewById(R.id.txtItem);
                holder.editQuantity = (EditText) convertView
                        .findViewById(R.id.editcreateQuantity);
                holder.editQuantity.setTag(position);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
          holder.editQuantity.addTextChangedListener(new TextWatcher() {

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

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

              }

              @Override
              public void afterTextChanged(Editable s) {

              }
          });
            holder.txtItecode.setText(strItecode[position]);
            holder.txtItem.setText(strItem[position]);
            holder.editQuantity.setText(text[temp]);

            return convertView;
        }

        class ViewHolder {
            TextView txtItecode;
            TextView txtItem;
            EditText editQuantity;
        }
    }

我的工作就可以了过去3天,我怎么能解决这个问题?

I'm working on it past 3 days, how I can fix this problem?

推荐答案

在除了一些有关可能使用滚动型,与code是如何ListView的适配器重用联的意见,真正的问题的意见你TextWatcher(S)。

In addition to some of the comments about possibly using a ScrollView, the real issue with your code is how the ListView's Adapter is reusing the Views in conjunction with your TextWatcher(s).

要显示的每一个时间连续的需要,在该位置上的getView()方法将被调用。适配器可以,并且会重用实际查看你在充气previous遍历以节省内存。

Every single time a row needs to be displayed, the getView() method on that position will be called. The adapter can, and will, reuse the actual Views you've inflated in previous traversals to save memory.

这通常是一个好东西,但都将有后果,当你与EditTexts和TextWatchers工作。

That is usually a good thing, but is going to have consequences when you're working with EditTexts and TextWatchers.

首先,holder.editQuantity.addTextChangeListener()仅仅是要保持在每个布局传递添加新TextWatcher。这是不好的,是你的问题的部分原因。

First, holder.editQuantity.addTextChangeListener() is just going to keep adding a new TextWatcher on every layout pass. That's bad and is partly responsible for your issue.

其次,一旦视图被重用,你实际上设置TextWatcher,后更改它的文本将触发这两个老的(如果有)和新TextWatchers'onTextChanged()方法,改变你保存的值,而这又是搞乱你的观点。

Secondly, once a view is being reused, you're actually changing the text on it after setting the TextWatcher, which will trigger both the old (if there is one) and the new TextWatchers' onTextChanged() methods and change your stored values, which in turn is messing up your Views.

如果你想要去与此有关的ListView /适配器的路线,您的解决方案将是有一个全局变量TextWatcher,设置在getView(文本之前从视图中删除TextWatcher)(使其不火的事件当你不希望它),然后重新添加你设置了文本之后。但是,因为你不能保持创造新TextWatchers(因为这将prevent你取出旧的以及经验搞砸值同样的问题),您还需要一种方式来链接到相应的在你的文本位置[],因为你将无法从getView()方法指向它。但是,您可以设置的EditText的FocusChangeListener确定何时查看具有焦点(即它正在编辑),并设置从适当的编辑位置)

If you want to go the ListView/Adapter route with this, your solution will be to have a global TextWatcher variable, remove the TextWatcher from the View before setting the text in getView() (so that it doesn't fire its events when you don't want it to), and then re-adding it after you've set the text. However, since you can't keep creating new TextWatchers (as that will prevent you from removing the old ones as well as experience the same issue with messed up values), you'll also need a way to link to the appropriate position in your text[], since you won't be able to point to it from the getView() method. However, you can set the EditText's FocusChangeListener to determine when the View has focus (ie. it's being edited) and set the appropriate editing position from that)

例如

private int editingPosition = 0;
private TextWatcher watcher = new TextWatcher() {
          public void onTextChanged(CharSequence s, int start, int before, int count) {
              text[editingPosition] = s.toString();
          }
          public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
          public void afterTextChanged(Editable s) { }
      };

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

       // Before this is fine

      holder.editQuantity.removeTextChangedListener(watcher);

      holder.editQuantity.setText(text[temp]);

      holder.editQuantity.setOnFocusChangeListener(new OnFocusChangeListener() {       
          public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus) editingPosition = position;
          }
      });

      holder.editQuantity.addTextChangedListener(watcher);

      // The rest is good, just make sure to remove the call to setting the EditText's text at the end

      return convertView;
}

这篇关于在自定义列表视图编辑文本丢失的滚动值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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