2

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:

LocationIDand 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?

Community
  • 1
  • 1
Leigh
  • 1,495
  • 2
  • 20
  • 42

3 Answers3

4

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.

dev.bmax
  • 8,998
  • 3
  • 30
  • 41
  • Being new to Java this pattern didn't even occur to me! Solved my problem perfectly. Thank you +1 – Leigh Nov 23 '14 at 21:37
2

you can use this code

private Activity activity;
private TextWatcher textWatcher = new TextWatcher() {

      @Override
      public void afterTextChanged(Editable s) {
          View focView=activity.getCurrentFocus();
          /* if t use EditText.settxt to change text  and the user has no 
           * CurrentFocus  the focView will be null
           */
          if(focView!=null)
          {
         EditText edit= (EditText) focView.findViewById(R.id.item_edit);
         if(edit!=null&&edit.getText().toString().equals(s.toString())){    
         edit.getTag() 
         }
        }
      }

      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      }

      public void onTextChanged(CharSequence s, int start, int before,
              int count) {

      }     

    };
public EditAdapter(ArrayList<HashMap<String, String>> list, Activity activity){
    this.activity = activity;
    this.list = list;
    inflater = LayoutInflater.from(activity);
}
maxwell
  • 21
  • 3
  • I've since solved this problem using @dev.bmax's solution above, this does also look like it would work however. Thanks for the suggestion I'm sure it might help someone else :) – Leigh Dec 31 '14 at 14:09
-1

You can use the EditText#getEditableText method:

@Override
public void afterTextChanged(Editable s) {

    if(editText.getEditableText() == s){
        //
        // Your code
        //
    }

}
alex
  • 8,904
  • 6
  • 49
  • 75