gpt4 book ai didi

java - TextWatcher.onTextChanged 被多次调用

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:57 25 4
gpt4 key购买 nike

我已经阅读了一些有关此问题的帖子,但找不到完整的答案。我有一个包含 3 行的 ListView,每行包含一个 TextView 和一个 EditText,以及一个扩展 BaseAdapter 的自定义适配器。

这是适配器的 getView 函数:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = mActivity.getLayoutInflater();
convertView = inflater.inflate(R.layout.settings_column, null);

mTxtValue = (EditText) convertView.findViewById(R.id.settings_value);

mTxtValue.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (tryParseInt(s.toString())) {
RowsList.get(position).setDuration(Integer.parseInt(s.toString()));
System.out.println("position: " + position + ", value: " + s.toString());
}
}

@Override
public void afterTextChanged(Editable s) {

}
});
}

ColorColumn colorColumn = RowsList.get(position);
mTxtValue.setText(String.valueOf(colorColumn.getDuration()));

return convertView;
}

正如您所看到的,每次值更改时,我都会尝试使用 EditText 值更新名为 RowsList 的 ColorColumn 列表。由于某种原因,onTextChanged 方法被调用太多次,因此将错误数据放入列表中。 EditText 的 inputType 是 android:inputType="number|textNoSuggestions",如另一个线程中的建议。

这是启动 Activity 并填充 ListView 时显示的日志:

10-27 19:30:15.238 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 1
10-27 19:30:15.242 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 2
10-27 19:30:15.244 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 3
10-27 19:30:15.317 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 3
10-27 19:30:15.325 19845-19845/com.busalert.www.busalert I/System.out: position: 1, value: 2
10-27 19:30:15.333 19845-19845/com.busalert.www.busalert I/System.out: position: 2, value: 3
10-27 19:30:15.346 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 3
10-27 19:30:15.350 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 2
10-27 19:30:15.353 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 3
10-27 19:30:15.388 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 3
10-27 19:30:15.394 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 2
10-27 19:30:15.398 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 3

尽你所能,每个位置的第一次出现都是正确的,但有 9 次额外的调用。

这是用户第一次输入 EditText 时出现的日志:

10-27 19:30:21.226 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 3
10-27 19:30:21.230 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 2
10-27 19:30:21.231 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 3
10-27 19:30:21.356 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 3
10-27 19:30:21.357 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 2
10-27 19:30:21.363 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 3
10-27 19:30:21.369 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 3
10-27 19:30:21.370 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 2
10-27 19:30:21.376 19845-19845/com.busalert.www.busalert I/System.out: position: 0, value: 3

再次,9 个额外调用(应该有 0 个调用,因为没有真正改变)。

从现在开始,每项更改都会根据需要产生一次调用。

更新:我创建了一个 boolean 数组来指示 TextWatcher 是否已添加到每个 EditText,因此我确保每个 EditText 都只有一个。添加此操作后, Activity 开始时有 6 个调用(额外 3 个),而当我第一次单击 EditText 时没有调用。这是新日志(2、3 和 4 是多余的):

10-27 20:02:49.765 29637-29637/com.busalert.www.busalert I/System.out: position: 0, value: 1
10-27 20:02:49.767 29637-29637/com.busalert.www.busalert I/System.out: position: 0, value: 2
10-27 20:02:49.769 29637-29637/com.busalert.www.busalert I/System.out: position: 0, value: 3
10-27 20:02:49.827 29637-29637/com.busalert.www.busalert I/System.out: position: 0, value: 3
10-27 20:02:49.834 29637-29637/com.busalert.www.busalert I/System.out: position: 1, value: 2
10-27 20:02:49.840 29637-29637/com.busalert.www.busalert I/System.out: position: 2, value: 3

最佳答案

问题是 LisView 元素正在被回收,因此旧的 TextWatcher 在回收行后仍然附加。因此,每次调用 getView 时,都会添加一个新的 TextWatcher,而旧的 TextWatcher 仍附加到 EditText。不幸的是,没有函数可以删除所有附加的旧 TextWatcher,因此唯一的解决方案是创建一个自定义 EditText,在其中保留对所有 TextWatcher 的引用,然后创建一个自定义函数以在回收 getView 中的 View 时将它们全部删除。

关于java - TextWatcher.onTextChanged 被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33375484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com