Skip to content Skip to sidebar Skip to footer

Custom Listview Adapter. Textchangedlistener Calls For Wrong Edittext

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 button c

Solution 1:

in getView method, the parameter position gives the position of the newly created childView, not the clicked childView's position.

use this to get the correct position:

finalint actual_position = myList.getPositionForView((View) v.getParent());

in onClick(View v); of the onClickListener of any View. In you case, you must implement onTextChangedListener for that EditText.

here:

myList is the ListView

v is the View you clicked, in this case the childView of the parent(myList).

Solution 2:

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

Solution 3:

Create one more EditText in the screen that is invisible with name invivisbleEt. And do the following thing in the addTextChangedListener

firstNameView.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable editable) {
        if(!firstNameView.isFocused())
            currentItem.setFirstName(editable.toString());
    }

});

Also add this code in the onCreate method for ListView object.

lv.setOnScrollListener(newAbsListView.OnScrollListener() {

    //public boolean scrolling;@OverridepublicvoidonScrollStateChanged(AbsListView absListView, int scrollState) {
        invivisbleEt.requestFocus();
    }

    @OverridepublicvoidonScroll(AbsListView absListView, int i, int i1, int i2) {
    }
});

Post a Comment for "Custom Listview Adapter. Textchangedlistener Calls For Wrong Edittext"