【发布时间】:2019-06-11 13:58:51
【问题描述】:
我正在尝试创建一个 RecyclerView,它在每一行中都包含一个 EditText。 更改文本后,我希望它使用 println 显示位置。 它运行良好,直到我在我的主要方法中运行 .notifyDataSetChanged() 。 之后,即使只更改了一个 EditText,它也会打印多个位置。
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.etName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
System.out.println(position);
}
});
}
之前:et0 更改 = I/System.out: 0
之后:et0 更改 = I/System.out: 2 AND I/System.out: 0
【问题讨论】:
-
不要使用位置变量,使用
holder.getAdapterPosition();方法获取位置。 -
您要打印上次修改的
EditText的数据,我理解正确吗? -
@JeelVankhede 它确实将位置固定为正确的值,但现在它总是打印两次相同的位置。
-
@DawidJ 最终是的,但现在我只需要 EditText 的位置。
标签: java android android-recyclerview notifydatasetchanged addtextchangedlistener