Android afterTextChanged get EditText tag

2019-08-03 15:16发布

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?

2条回答
祖国的老花朵
2楼-- · 2019-08-03 15:46

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);
}
查看更多
戒情不戒烟
3楼-- · 2019-08-03 15:59

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.

查看更多
登录 后发表回答