Custom ListView adapter. TextChangedListener calls for wrong EditText

前端 未结 3 1292
清歌不尽
清歌不尽 2021-01-23 13:20

I have the list of travelers with custom adapter what consist two EditText - edtFirstName and edtLastName. I want when user enters text save changes to List, and when next butto

3条回答
  •  佛祖请我去吃肉
    2021-01-23 14:14

    The issue happens because views are reusable (that is by design in Android API). So eventually you may assign more than 1 text watcher to the same text view. And all of the assigned watchers are fired when text inside of the text view is changed.

    A quick fix (and non-optimal if the list is really long, say, of 1000+ items) would be to have a map of Traweller -> TextWatcher.

    Then inside of getView() you can do this (pseudo-code):

    • check the map if there is a TextWatcher for this Traweller
    • if map does not have any, then create a new TextWatcher, put in the map and assign to EditText
    • otherwise detach the TextWatcher from the EditText and remove from the map
    • create a new TextWatcher, put in the map and assign to EditText

提交回复
热议问题