how can i call itemclicklistener inside a switch onclicklistener android -


i creating android application. want when user clicked switch in row call listview's onitemclicklistener. before, tried call listview's itemclicklistener on switch's onclicklistener inside customadapter throws error. if there's no way me achieve desired result, please tell me alternative way. if wondering why need listview's itemclick called inside switch's onclick because itemclick way can rows id use updating rows.

thank in advance. :)

here's code of activity listview:

import java.text.decimalformat; import java.util.timer; import java.util.timertask;  import android.app.activity; import android.app.dialog; import android.app.dialogfragment; import android.content.intent; import android.database.cursor; import android.os.bundle; import android.os.handler; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.button; import android.widget.listview; import android.widget.switch; import android.widget.textview; import android.widget.toast;  public class listviewform extends activity {     private customcursoradapter customadapter;     private todoitemdatabase td;     private listview lv;     private switch swi;     int devid, devpos;     long devid2;     string devname;     int devwatt, devstat;     //for timer     timer timer;     timertask timertask;     handler handler, adapter;     //for bill compute     int totalwatt;     int totalhour = 1;     double watthourperday, kilowattperday, kilowattpermonth, billpermonth;      int timerfirstrun;     double costpermonth = 8.5568 ;     //int delay = 3600000;     int delay = 50000;     textview lblbillval;     //double costpermonth = .10 ;     onitemclicklistener itemclick;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_list_view_form);          lv = (listview)findviewbyid(r.id.list_data);         lv.setitemscanfocus(false);         lv.setchoicemode(listview.choice_mode_single);          lblbillval = (textview)findviewbyid(r.id.lblbillvalue);         lblbillval.settext("");          swi = (switch)findviewbyid(r.id.switch1);         //itemclick          itemclick = new onitemclicklistener(){              @override             public void onitemclick(adapterview<?> parent, view v, int pos, long id) {                 // todo auto-generated method stub                 log.d("batelec", "id: "+id+" pos: "+pos);                 todoitemdatabase td = new todoitemdatabase(getbasecontext());                 int x = integer.valueof(string.valueof(id));                 cursor cur = td.getonerow(x);                 if(cur != null){                     if(cur.movetofirst()){                         log.d("batelec", "inside onitemclick, cur count: "+cur.getcount());                         devstat = cur.getint(cur.getcolumnindex(cur.getcolumnname(3)));                         log.d("batelec", "1st devstat: "+devstat);                         if(devstat == 0){                             devstat = 1;                         }                         else{                             devstat = 0;                         }                          devid = cur.getint(cur.getcolumnindex(cur.getcolumnname(0)));                         devname = cur.getstring(cur.getcolumnindex(cur.getcolumnname(1)));                         devwatt = cur.getint(cur.getcolumnindex(cur.getcolumnname(2)));                         //devstat                         log.d("batelec", "2nd devstat: "+devstat);                         td.updatestat(new todoitem(devid, devname, devwatt, devstat));                          customcursoradapter cr = new customcursoradapter(listviewform.this, td.getalldata());                         cr.notifydatasetchanged();                         lv.setadapter(cr);                     }                  }             }          };         lv.setonitemclicklistener(itemclick);         //itemlongclick         lv.setonitemlongclicklistener(new adapterview.onitemlongclicklistener() {             @override             public boolean onitemlongclick(adapterview<?> parent, view v, int pos, long id){                 log.d("batelec", "longclick");                  return onlonglistitemclick(v, pos, id);             }         });          td = new todoitemdatabase(this);         new handler().post(new runnable(){             @override             public void run(){                 customadapter = new customcursoradapter(listviewform.this, td.getalldata());                 lv.setadapter(customadapter);             }         });          //usehandler();     }     //itemlongclick     protected boolean onlonglistitemclick(view v, final int pos, long id) {         log.d("batelec", "onlonglistitemclick id= " + id + " position= " + pos);          devid2 = id;         devpos = pos;         final dialog dialog = new dialog(listviewform.this);         dialog.setcontentview(r.layout.dialog_form_design);         dialog.settitle("option");         button btnedit = (button)dialog.findviewbyid(r.id.dialogbtnedit);         button btndelete = (button)dialog.findviewbyid(r.id.dialogbtndelete);          btnedit.setonclicklistener(new onclicklistener(){             public void onclick(view v){                 todoitemdatabase td = new todoitemdatabase(listviewform.this);                 cursor cur = td.getalldata();                  if(cur!=null){                     if(cur.movetofirst()){                         cur.movetoposition(pos);                         devid = cur.getint(cur.getcolumnindex("_id"));                         devname = cur.getstring(cur.getcolumnindex("devicename"));                         devwatt = cur.getint(cur.getcolumnindex("devicewattage"));                         devstat = cur.getint(cur.getcolumnindex("devicestatus"));                         log.d("batelec", "devid: "+devid+" devname: "+devname+" devwatt: "+devwatt+" devstat: "+devstat);                     }                 }                 intent = new intent(getbasecontext(),adddeviceform.class);                 bundle b = new bundle();                 log.d("batelec", "sent id: "+devid);                 b.putint("xtradevid", devid);                 b.putstring("xtradevname", devname);                 b.putint("xtrawatt", devwatt);                 b.putint("xtrastat", devstat);                 log.d("batelec", "sent devstat: "+devstat);                 i.putextras(b);                 startactivity(i);                 finish();             }         });          btndelete = (button)dialog.findviewbyid(r.id.dialogbtndelete);         btndelete.setonclicklistener(new onclicklistener(){             public void onclick(view v){                  todoitemdatabase td = new todoitemdatabase(listviewform.this);                 cursor cur = td.getalldata();                  if(cur!=null){                     if(cur.movetofirst()){                         cur.movetoposition(pos);                         devid = cur.getint(cur.getcolumnindex("_id"));                         devname = cur.getstring(cur.getcolumnindex("devicename"));                         devwatt = cur.getint(cur.getcolumnindex("devicewattage"));                         devstat = cur.getint(cur.getcolumnindex("devicestatus"));                         devpos = pos;                         log.d("batelec", "devid: "+devid+" devname: "+devname+" devwatt: "+devwatt+" devstat: "+devstat+" devpos: "+devpos);                     }                 }                 bundle b = new bundle();                 b.putint("xtraid", devid);                 b.putint("xtrapos", devpos);                 dialogfragment deletediag = new deletedialog();                 deletediag.setarguments(b);                 deletediag.show(getfragmentmanager(), "deletedevice");             }         });          dialog.show();         log.d("batelec", "showing dialog");         return true;     }      public void usehandler(){         handler = new handler();         handler.postdelayed(runnable, delay);     }     public void stoprunnable(view v){         handler.removecallbacks(runnable);     }      private runnable runnable = new runnable(){         @override         public void run(){             todoitemdatabase td = new todoitemdatabase(getbasecontext());             cursor cur = td.getactivedevice();              if(timerfirstrun == 0){                 timerfirstrun++;                 log.d("batelec", "timer = 0");             }             else{                 try{                     if(cur != null){                         toast.maketext(getbasecontext(), "1 hour elapsed", toast.length_long).show();                          /*cur.movetofirst();                         for(int x = 1; x <= cur.getcount(); x++){                             int id = cur.getint(cur.getcolumnindex("_id"));                             string name = cur.getstring(cur.getcolumnindex("devicename"));                             int watt = cur.getint(cur.getcolumnindex("devicewattage"));                             int stat = cur.getint(cur.getcolumnindex("devicestatus"));                             log.d("batelec", "id: " + id + " name: " + name + " watt: " + watt + " status: " + stat);                             totalwatt = totalwatt + watt;                             cur.movetonext();                         }*/                         totalwatt = 125;                         log.d("batelec", "total hour: "+totalhour);                         watthourperday = totalwatt * totalhour;//all active device wattage * hours active                          log.d("batelec", "wattperhour: "+watthourperday+" (totalwatt * totalhour)");                         kilowattperday = watthourperday / 1000;//all device watts divided 1000 watts = 1 kw                          log.d("batelec", "kilowatt per day: "+kilowattperday+" (wattperhour / 1000)");                         kilowattpermonth = (watthourperday * 30) / 1000;//watts per month                          log.d("batelec", "kilowatt per month: "+kilowattpermonth+" ((wattperhour * 30) / 1000)");                         billpermonth = kilowattpermonth * costpermonth;//estimated bill per month                          log.d("batelec", "bill per month: "+billpermonth+" (kilowattpermonth * costpermonth)");                          double res;                         decimalformat df = new decimalformat("#.##");                         res = double.valueof(df.format(billpermonth));                          log.d("batelec", "new bill: "+res);                          lblbillval.settext(string.valueof(res));                         totalhour++;                     }                 }catch(exception e){                     log.d("batelec", ""+e);                 }             }             handler.postdelayed(runnable, delay);         }     };  } 

here's custom adapter code:

import android.content.context; import android.database.cursor; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.cursoradapter; import android.widget.switch; import android.widget.textview;  public class customcursoradapter extends cursoradapter {     int watttotal = 0;     int pos, i;     int[] tags;     //components     textview txtdevname, txtdevwatt;     switch swistatus;     //viewholder viewholder;     @suppresswarnings("deprecation")     public customcursoradapter(context context, cursor c) {         super(context, c);     }      @override     public view newview(context context, cursor cursor, viewgroup parent) {         layoutinflater inflater = layoutinflater.from(parent.getcontext());         view retview = inflater.inflate(r.layout.list_view_design, parent, false);          return retview;     }      @override     public void bindview(view view, context context, cursor cursor) {         txtdevname = (textview) view.findviewbyid(r.id.textdevicename);         txtdevname.settext(cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(1))));         string name = cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(1)));          txtdevwatt = (textview) view.findviewbyid(r.id.textdevicewatt);         txtdevwatt.settext(cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(2))));          swistatus = (switch) view.findviewbyid(r.id.switch1);         int stat = cursor.getint(cursor.getcolumnindex(cursor.getcolumnname(3)));          log.d("batelec", "name: "+ name + " stat: "+stat);         //log.d("batelec", "tags: "+i);        /* swistatus.setonclicklistener(new onclicklistener(){              @override             public void onclick(view v) {                 // todo auto-generated method stub                 log.d("batelec", "switch click: "+i);                 listviewform lvf = new listviewform();                 lvf.itemclick();             }          });*/          if (stat == 0){             swistatus.setchecked(false);         }         else{             swistatus.setchecked(true);         }         //i++;       } } 

here's xml, don't understand yshahak meant @ first

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical" >      <tablelayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_weight="0.03" >          <tablerow             android:id="@+id/tablerow1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_weight="0.03" >              <textview                 android:id="@+id/textview1"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_weight="0.03"                 android:text="@string/txtdevicename"                 android:textappearance="?android:attr/textappearancelarge" />              <textview                 android:id="@+id/textview2"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_weight="0.03"                 android:text="@string/txtdevicewatt"                 android:textappearance="?android:attr/textappearancelarge" />              <textview                 android:id="@+id/textview3"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_weight="0.03"                 android:gravity="top|end"                 android:text="@string/txtstatus"                 android:textappearance="?android:attr/textappearancelarge" />         </tablerow>      </tablelayout>      <listview         android:id="@+id/list_data"         android:layout_width="match_parent"         android:layout_height="fill_parent"         android:layout_weight="1"         android:clickable="true"         android:focusable="false"         android:longclickable="true"         android:descendantfocusability="blocksdescendants"/>      <tablelayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_weight="0.03" >          <tablerow             android:id="@+id/tablerow2"             android:layout_width="wrap_content"             android:layout_height="wrap_content" >              <textview                 android:id="@+id/lblapproxbill"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="@string/txtestimatebill"                 android:textappearance="?android:attr/textappearancelarge" />              <textview                 android:id="@+id/lblbillvalue"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_gravity="end"                 android:text="@string/txtnull"                 android:textappearance="?android:attr/textappearancelarge"                 android:width="@dimen/listnamefieldsize" />          </tablerow>     </tablelayout>  </linearlayout> 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -