在行回收时使用空值调用EditText的onTextChanged [英] EditText's onTextChanged being called with empty value on row recycle

查看:59
本文介绍了在行回收时使用空值调用EditText的onTextChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每行都有一个带有EditText的RecyclerView.每个EditText都附加有一个TextWatcher和一个数组中的对象,用于存储有关行和EditText值的信息.

I have a RecyclerView with an EditText in each row. Each EditText has a TextWatcher attached to it and an object in an array to store information about the row and the value of the EditText.

我面临的问题是,当行离开屏幕时,它将被回收,但是在该过程中,使用空字符串调用onTextChanged,这意味着将空字符串保存到行的信息对象中.再次加载该行时,它仅加载一个空白值,有效删除了EditText之前的内容.这似乎是非常无用的功能,因为(据我所知),无法区分回收清除和用户实际清除EditText中的值,从而使RecyclerView中的EditText完全无用.

The problem I'm facing is that when the row goes offscreen, it gets recycled, but in that process, onTextChanged is called with an empty string, meaning an empty string is being saved to the row's information object. When that row is loaded again, it just loads a blank value, effectively deleting whatever content was in the EditText before. This seems like very useless functionality, because (as far as I can tell) there's no way to differentiate between a recycle clear and the user actually clearing the value in the EditText, making EditTexts in a RecyclerView completely useless.

我的 onBindViewHolder 方法是这样的:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    int value = indicators.get(position).getValue();

    System.out.println("VALUE: " + indicator.getValue());

    if (value == -1) {
        viewHolder.inputBox.setText("");
    } else {
        viewHolder.inputBox.setText(Integer.toString(value));
    }

    viewHolder.editListener.updatePosition(position);

}

我的TextWatcher是这样的:

My TextWatcher is this:

private class EditBoxListener implements TextWatcher  {

    private int position;
    private CharSequence sequence;
    private boolean initialCall = true;

    public void updatePosition(int _position) {
        position = _position;
    }

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

    @Override
    public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

        System.out.println("TEXT CHANGED TO: " + charSequence.toString());

        if (!initialCall) {
            int value = -1;

            try {
                value = Integer.parseInt(charSequence.toString());
            } catch (Exception e) {}

            if (indicators.size() > 0) {

                indicators.get(position).setValue(value);

            }
        } else {
            initialCall = false;
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {}

}

推荐答案

一种解决方法是在执行任何操作之前先检查文本是否为空白,也许是这样的(原谅我的格式不正确)):

Well one way to combat this would be to simply check if the text is blank or not before doing anything with it, perhaps something like this (forgive my poor formatting):

@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

if (!charSequence.equals("")) {

    System.out.println("TEXT CHANGED TO: " + charSequence.toString());

    if (!initialCall) {
        int value = -1;

        try {
            value = Integer.parseInt(charSequence.toString());
        } catch (Exception e) {}

        if (indicators.size() > 0) {

            indicators.get(position).setValue(value);

        }
    } else {
        initialCall = false;
    }
 }

}

这篇关于在行回收时使用空值调用EditText的onTextChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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