【发布时间】:2014-11-23 20:52:44
【问题描述】:
我有一个包含ListView 的DialogFragment,并带有一个连接到ListView 的自定义适配器。该列表显示一堆项目,每条记录都有一个EditText,以允许用户输入数量。
当这些数量中的任何一个发生变化时,我需要在适配器中更新我的数组,这意味着将EditText 链接到数组中的特定元素。我使用EditText 的getTag / setTag 方法来做到这一点。数组中的项目通过两个属性是唯一的:
LocationID和
RefCode
这些存储在我的TagData 对象中并在getView() 处设置。一旦值发生更改,我正在尝试使用EditText.getTag(),遗憾的是无济于事。
问题是我无法在afterTextChanged 方法中访问EditText。
这是我的适配器的getView() 方法:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ItemModel item = (ItemModel) getItem(i);
TagData tagData = new TagData();
tagData.setLocationID(item.getLocationID());
tagData.setRefCode(item.getRefCode());
EditText txtQuantity = ((EditText) view.findViewById(R.id.txtQuantity));
txtQuantity.setTag(tagData);
txtQuantity.setText(String.valueOf(item.getQtySelected()));
txtQuantity.addTextChangedListener(this);
...
return view;
}
在上面我创建了一个TagData 对象并使用setTag() 将它绑定到EditText。我还在getView() 中连接了addTextChangedListener。其中afterTextChanged 方法如下所示:
@Override
public void afterTextChanged(Editable editable) {
EditText editText = (EditText)context.getCurrentFocus(); // This returns the WRONG EditText!?
// I need this
TagData locAndRefcode = (TagData) editText.getTag();
}
根据this 的帖子,Activity.getCurrentFocus() 应该返回有问题的EditText,它没有。相反,它会从 DialogFragment 后面的视图返回 EditText。
这让我陷入困境。如何从我的 afterTextChanged 方法中访问 EditText 的标签?
【问题讨论】: