android - OnItemSelectedListener, ListView and item recycling -


i have bunch of items in listview. each item contains several editable views. want save changes user makes these. event save occur when item loses focus.

i see 2 ways:

    view.onfocuschangelistener()  onfocuschange(view v, boolean hasfocus) 

this works messy since each item contains several editable views. rather save when whole item defocused. attempts simplify far have complicated things.

if there onitemdeselectedlistener perfect, there opposite:

    adapterview.onitemselectedlistener() onitemselected(adapterview<?> adapterview, view view, int i, long l) 

on surface looks good. create variable lastselectedview know last selected (deselected) item was. problem how listview recycles views. believe there no guarantee adapter has not recycled lastselectedview , changed data contains. reasonable assumption?

is there reasonable way around recycling view problem onitemselectedlistener?

if you're using single selection, selecting item "deselect" current item. @ time, apply changes locally stored copy of item in adapter. don't believe should use view being passed in onitemselected() or onitemdeselected() model edited data. said, it's best create item of adapter data type being edited, , in onselectedlistener(), apply changes edited item corresponding item in adapter. this:

    listview mylistview;     object objectcurrentlyediting;     int positioncurrentlyediting;     mylistview.setonitemselectedlistener(new onitemselectedlistener(){          @override         public void onitemselected(adapterview<?> arg0, view arg1,                 int position, long arg3)          {             mylistview.getadapter().getitem(positioncurrentlyediting).update(objectcurrentlyediting);              positioncurrentlyediting = position;             objectcurrentlyediting = mylistview.getadapter().getitem(position);         }          @override         public void onnothingselected(adapterview<?> arg0) {             // todo auto-generated method stub          }     }); 

the update function how list item saved after no longer selected. method assumes you're using single choice mode though.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -