android - listview getting new instance when swiping the listview -


i have 1 listview in application,it contains 2 rows 1 task , 1 alarm,date,severity. first row of list item displayed list item , second 1 invisible. when click list item second row displayed item click list item @ time above list item closed second row. working fine me...my problem if open 1 list item , swipe listview @ click list item @ time above 1 cannot closed because above list item instance chnaged.please 1 me how solve problem...

int lastselectedposition == -1     @override    public void onitemclick(adapterview<?> arg0, view view, int position,          long id) {        textview textviewdate=(textview)view.findviewbyid(r.id.tasktimeiddaytoday);        selectedtaskdate=textviewdate.gettext().tostring().trim();       if (lastselectedposition == -1) {          log.i(tag,"loopif:"+lastselectedposition);          textview twtasktime = (textview) view                .findviewbyid(r.id.tasktimeiddaytoday);          textview twseverity = (textview) view                .findviewbyid(r.id.severityiddaytoday);          textview twalarm = (textview) view                .findviewbyid(r.id.alarmiddaytoday);          twalarm.setvisibility(view.visible);          twseverity.setvisibility(view.visible);          twtasktime.setvisibility(view.visible);          lastselectedposition = position;          lastselecteditem = arg0.getchildat(position);       } else {          // log.i(tag,"loopelse:"+lastselectedposition);          lastselecteditem.findviewbyid(r.id.tasktimeiddaytoday)                .setvisibility(lastselecteditem.gone);          lastselecteditem.findviewbyid(r.id.severityiddaytoday)                .setvisibility(lastselecteditem.gone);          lastselecteditem.findviewbyid(r.id.alarmiddaytoday).setvisibility(                  lastselecteditem.gone);          if (lastselectedposition != position) {             view.findviewbyid(r.id.tasktimeiddaytoday).setvisibility(                   view.visible);             view.findviewbyid(r.id.severityiddaytoday).setvisibility(                   view.visible);             view.findviewbyid(r.id.alarmiddaytoday).setvisibility(                   view.visible);             lastselectedposition = position;             lastselecteditem = arg0.getchildat(position);          } else {             lastselectedposition = -1;             lastselecteditem = null;          }       } 

getview():

@override public view getview(int position, view view, viewgroup parent) {     log.i("xxxx", "inside getview");      final daytodaytaskgettersetter objdaytodaygetset=getitem(position);      textview textviewtask;      textview txtviewalarm ,txtviewtasktime ,txtviewseverity;      log.i(tag,"insidegetview:"+position);      layoutinflater inflater=(layoutinflater)context.getsystemservice(activity.layout_inflater_service);      if(view==null)      {          view=inflater.inflate(r.layout.daytodaylistlayout,null);        }         log.i("xxxx", "before first test");      textviewtask=(textview)view.findviewbyid(r.id.tasknameiddaytoday);      txtviewalarm=(textview)view.findviewbyid(r.id.alarmiddaytoday);      txtviewseverity=(textview)view.findviewbyid(r.id.severityiddaytoday);      txtviewtasktime=(textview)view.findviewbyid(r.id.tasktimeiddaytoday);      return view; } 

enter image description here

enter image description here

in first click "gdfgdtet" list item show row , click second list item "dfgsdgsd" @ time above list item "gdfgdtet" closed second row.this normal output.suppose if open "gdfgdtet" list item , swipe listview @ time both of "gdfgdtet" "dfgsdgsd" opened , crashed...because above 1 list item reference changed when swiping please how solve problem...

you should pay attention tal kanel's answer , consider 1 extension it. advice in long run.

add boolean field daytodaytaskgettersetter class:

public class daytodaytaskgettersetter {  .... .... boolean open;      public daytodaytaskgettersetter (.., .., boolean o) {         ....         ....         open = o;     }      ....     ....      public boolean shouldopen() {         return open;     }      public void setopen(boolean o) {         open = o;     } } 

in getview(), check if object has open value set:

daytodaytaskgettersetter obj = (daytodaytaskgettersetter) getitem(position);  if (obj.shouldopen()) {     // set visibility true items } else {     // set visibility false items } 

on list item click, traverse list , set open list items false. use position retrieve daytodaytaskgettersetter , set open true:

@override public void onitemclick(adapterview<?> arg0, view view, int position, long id) {      (daytodaytaskgettersetter obj : listcontainingobjects) {         obj.setopen(false);     }      daytodaytaskgettersetter clickeditem = (daytodaytaskgettersetter)                                                  youradapter.getitem(position);     clickeditem.setopen(true);      youradapter.notifydatasetchanged(); } 

edit 1:

@override public void onitemclick(adapterview<?> arg0, view view, int position, long id) {      daytodaytaskgettersetter clickeditem = (daytodaytaskgettersetter)                                                  youradapter.getitem(position);      if (clickeditem.shouldopen()) {         clickeditem.setopen(false);     } else {          (daytodaytaskgettersetter obj : listcontainingobjects) {             obj.setopen(false);         }          clickeditem.setopen(true);     }      youradapter.notifydatasetchanged(); } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -