I have a DialogFragment
containing a ListView
, with a custom adapter hooked up to the ListView
. The list displays a bunch of items with an EditText
for each record to allow the user to enter a quantity.
When any of these quantities change I need to update my array within the adapter, which means linking an EditText
to a specific element in the array. I do this using the getTag / setTag methods of the EditText
. Items in the array are unique by two properties:
LocationID
and
RefCode
These are stored in my TagData
object and set at the point of getView(). I'm attempting to use EditText.getTag()
once a value has changed, sadly to no avail.
The problem is I can't access the EditText
in the afterTextChanged
method.
Here's the getView()
method of my Adapter:
@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;
}
Above I create a TagData
object and tie it to the EditText
using setTag()
. I also hook up an addTextChangedListener
in the getView()
. For which the afterTextChanged
method looks like this:
@Override
public void afterTextChanged(Editable editable) {
EditText editText = (EditText)context.getCurrentFocus(); // This returns the WRONG EditText!?
// I need this
TagData locAndRefcode = (TagData) editText.getTag();
}
According to this post, Activity.getCurrentFocus()
should return the EditText
in question, it doesn't. Instead it returns an EditText
from the View behind the DialogFragment.
Which leaves me stuck. How can I get access to an EditText
's tag from within my afterTextChanged method?
you can use this code
If you would declare your txtQuantity as final and then pass an anonymous new TextWatcher() { ... } into the addTextChangedListener, then you could directly use txtQuantity inside the afterTextChanged(Editable s) method. Hope this helps.