java - ExpendableListview Failed to read row 0, column -1 from a CursorWindow -


i tried show data in expendablelistview, error failed read row 0, column -1 cursorwindow.i debug , give me error in bindchildview on adapter. i've checked query there nothing wrong query, tried check column had in table nothing wrong it.

this fragment :

public class c_agentlistspajfragment extends fragment implements loadermanager.loadercallbacks<cursor> {     private static final string tag = c_agentlistspajfragment.class.getsimplename();      private string kodeagen;      private c_listspajdraftadapter adapter;      private expandablelistview explist_spaj;      private list<string> list_group;      private cursor cursor = null;      private boolean isexpand = true;      public c_agentlistspajfragment() {     }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         sethasoptionsmenu(true);          sharedpreferences sharedpreferences = getactivity().getsharedpreferences(getstring(r.string.app_preferences), context.mode_private);         kodeagen = sharedpreferences.getstring("kode_agen", "");     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         view view = inflater.inflate(r.layout.e_fragment_list_spaj, container, false);         explist_spaj = (expandablelistview) view.findviewbyid(r.id.explist_spaj);         cursor header = new e_select(getcontext()).getfetchgroupspaj(getcontext(), kodeagen, "", 0);          adapter = new c_listspajdraftadapter(                 header, getactivity(), getcontext(), this, kodeagen,                 r.layout.c_list_group_layout,                 r.layout.e_adapter_child_spaj,                 new string[]{getstring(r.string.count)},                 new int[]{r.id.tv_header_list},                 new string[]{getstring(r.string.spaj_id), getstring(r.string.nama_pp), getstring(r.string.nama_tt),                         getstring(r.string.premi_pokok_di), getstring(r.string.up_ua), getstring(r.string.va)},                 new int[]{r.id.tv_no_proposal, r.id.tv_pp, r.id.tv_tt, r.id.tv_premi, r.id.tv_up, r.id.tv_no_va}         );          explist_spaj.setadapter(adapter);         loader loader = getloadermanager().getloader(0);         if (loader != null && !loader.isreset()) {             getloadermanager().restartloader(0, null, this);         } else {             getloadermanager().initloader(0, null, this);         }         return view;     }      @override     public loader<cursor> oncreateloader(int id, bundle args) {          if (id != -1) {             if (id == 0) { // draft                 cursor = new e_select(getcontext()).getchildlistspaj(getcontext(), kodeagen, "", 0, 0);             } else { //submit                 cursor = new e_select(getcontext()).getchildlistspaj(getcontext(), kodeagen, "", 1, 0);             }         } else {             cursor = new e_select(getcontext()).getfetchgroupspaj(getcontext(), kodeagen, "", 0);         }         return new cursorloader(getcontext()) {             @override             public cursor loadinbackground() {                 return cursor;             }         };     }      @override     public void onloadfinished(loader<cursor> loader, cursor data) {         int id = loader.getid();         if (id != -1) {             if (!data.isclosed()) {                 log.d(tag, "data.getcount() " + data.getcount());                  sparseintarray groupmap = adapter.getgroupmap();                 try {                     int grouppos = groupmap.get(id);                     adapter.setchildrencursor(grouppos, data);                 } catch (nullpointerexception e) {                     e.getmessage();                 }             }         } else {             adapter.changecursor(data);         }         if (isexpand) {             isexpand = false;             explist_spaj.expandgroup(0);             explist_spaj.expandgroup(1);         }     }      @override     public void onloaderreset(loader<cursor> loader) {         int id = loader.getid();         if (id != -1) {             try {                 adapter.setchildrencursor(id, null);             } catch (nullpointerexception e) {                 e.getmessage();             }         } else {             adapter.setgroupcursor(null);         }     } } 

and adapter :

public class c_listspajdraftadapter extends simplecursortreeadapter {     private context context;     private string kodeagen;     private fragmentactivity mactivity;     private c_agentlistspajfragment c_agentlistspajfragment;     private  sparseintarray groupmap;      public c_listspajdraftadapter(cursor cursor, fragmentactivity activity, context context, c_agentlistspajfragment c_agentlistspajfragment, string kodeagen, int grouplayout,                                   int childlayout, string[] groupfrom, int[] groupto, string[] childrenfrom,                                   int[] childrento) {         super(context, cursor, grouplayout, groupfrom, groupto,                 childlayout, childrenfrom, childrento);         this.mactivity = activity;         this.context = context;         this.kodeagen = kodeagen;         this.c_agentlistspajfragment = c_agentlistspajfragment;         this.groupmap = new sparseintarray();     }      @override     protected cursor getchildrencursor(cursor cursor) {         int position = cursor.getposition();         int groupid = cursor.getint(cursor.getcolumnindex(mactivity.getstring(r.string._id)));         groupmap.put(groupid, position);         loader<object> loader = mactivity.getsupportloadermanager().getloader(groupid);         if (loader != null && !loader.isreset()) {             mactivity.getsupportloadermanager().restartloader(groupid, null, c_agentlistspajfragment);         } else {             mactivity.getsupportloadermanager().initloader(groupid, null, c_agentlistspajfragment);         }          return null;     }       @override     protected void bindgroupview(view view, context context, cursor cursor, boolean isexpanded) {         super.bindgroupview(view, context, cursor, isexpanded);          int groupposition = cursor.getposition();         int childrencount = getchildrencount(groupposition);          imageview iv_header_list = (imageview) view                 .findviewbyid(r.id.iv_header_list);          textview tv_empty_notif = (textview) view.findviewbyid(r.id.tv_empty_notif);          switch (groupposition) {             case 0:                 tv_empty_notif.settext("tidak ada draft spaj");                 break;             case 1:                 tv_empty_notif.settext("tidak ada spaj yang disubmit");                 break;             default:                 break;         }          if (isexpanded) {             iv_header_list.setimageresource(r.drawable.ic_expand_less_light_grey);             if (childrencount == 0) {                 tv_empty_notif.setvisibility(view.visible);             } else {                 tv_empty_notif.setvisibility(view.gone);             }          } else {             iv_header_list.setimageresource(r.drawable.ic_expand_more_light_grey);             tv_empty_notif.setvisibility(view.gone);         }      }      @override     protected void bindchildview(view view, context context, cursor cursor, boolean islastchild) {         super.bindchildview(view, context, cursor, islastchild);         textview tv_no_proposal = (textview) view.findviewbyid(r.id.tv_no_proposal);         textview tv_pp = (textview) view.findviewbyid(r.id.tv_pp);         textview tv_tt = (textview) view.findviewbyid(r.id.tv_tt);         textview tv_premi = (textview) view.findviewbyid(r.id.tv_premi);         textview tv_up = (textview) view.findviewbyid(r.id.tv_up);         textview tv_no_va = (textview) view.findviewbyid(r.id.tv_no_va);         string nama = cursor.getstring(cursor.getcolumnindexorthrow("spaj_id"));         tv_no_proposal.settext(nama);      }      @override     public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) {         view v = super.getgroupview(groupposition, isexpanded, convertview, parent);         expandablelistview mexpandablelistview = (expandablelistview) parent;         mexpandablelistview.expandgroup(groupposition, false);         mexpandablelistview.setselectedgroup(0);         return v;     }      public sparseintarray getgroupmap() {         return groupmap;     } } 

and how call cursor group :

        public cursor getfetchgroupspaj(context context, string kode_agen, string idlead, int flag_view) {         cursor cursor = null;         int count = 0;         matrixcursor matrixcursor = new matrixcursor(new string[]{context.getstring(r.string._id), "count"});   cursor = dbhelper.getreadabledatabase().rawquery("select count(*) " + context.getstring(r.string.table_e_mst_spaj) +                     " kode_agen = '" + kode_agen + "' , flag_aktif = 0", null);             cursor.movetofirst();             count = cursor.getint(0);             matrixcursor.addrow(new object[]{0, "draft "+count});              cursor = dbhelper.getreadabledatabase().rawquery("select count(*) " + context.getstring(r.string.table_e_mst_spaj) +                     " kode_agen = '" + kode_agen + "' , flag_aktif = 1", null);             cursor.movetofirst();             count = cursor.getint(0);             matrixcursor.addrow(new object[]{1, "submit " +count});    cursor.close();         return matrixcursor;     } 

cursor childview :

      public cursor getchildlistspaj(context context, string kode_agen, string idlead, int flag_spaj, int flag_view) {             list<string> list = new arraylist<>();             cursor cursor = null;             int count = 0;       cursor = dbhelper.getreadabledatabase().rawquery("select a._id _id, a.spaj_id spaj_id, b.nama_pp nama_pp, c.nama_tt nama_tt," +                 " d.premi_pokok_di premi_pokok_di, e.up_ua up_ua, a.virtual_acc virtual_acc" +                 " e_mst_spaj a, e_pemegang_polis b, e_tertanggung c, e_detil_investasi d, e_usulan_asuransi e" +                 " a.spaj_id = b.spaj_id" +                 " , a.spaj_id = c.spaj_id" +                 " , a.spaj_id = d.spaj_id" +                 " , a.spaj_id = e.spaj_id" +                     " , a.kode_agen = '" + kode_agen + "' , a.flag_aktif = " + flag_spaj, null);             cursor.movetofirst();    return cursor;     } 

and logcat said :

    e/androidruntime: fatal exception: main         process: id.co.ajsmsig.nb, pid: 9120         java.lang.illegalstateexception: couldn't read row 0, col -1 cursorwindow.  make sure cursor initialized correctly before accessing data it.             @ android.database.cursorwindow.nativegetlong(native method)             @ android.database.cursorwindow.getlong(cursorwindow.java:524)             @ android.database.abstractwindowedcursor.getlong(abstractwindowedcursor.java:75)             @ android.widget.cursortreeadapter$mycursorhelper.getid(cursortreeadapter.java:436)             @ android.widget.cursortreeadapter.getchildid(cursortreeadapter.java:173)             @ android.widget.expandablelistconnector.getitemid(expandablelistconnector.java:428)             @ android.widget.abslistview.setitemviewlayoutparams(abslistview.java:3306)             @ android.widget.abslistview.obtainview(abslistview.java:3275)             @ android.widget.listview.measureheightofchildren(listview.java:1396)             @ android.widget.listview.onmeasure(listview.java:1303)             @ android.view.view.measure(view.java:21103)             @ android.support.constraint.constraintlayout.internalmeasurechildren(constraintlayout.java:934)             @ android.support.constraint.constraintlayout.onmeasure(constraintlayout.java:973)             @ android.view.view.measure(view.java:21103)             @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1658)             @ android.view.view.measure(view.java:21103)             @ android.support.constraint.constraintlayout.onmeasure(constraintlayout.java:1028)             @ android.view.view.measure(view.java:21103)             @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6462)             @ android.support.design.widget.coordinatorlayout.onmeasurechild(coordinatorlayout.java:714)             @ android.support.design.widget.headerscrollingviewbehavior.onmeasurechild(headerscrollingviewbehavior.java:90)             @ android.support.design.widget.appbarlayout$scrollingviewbehavior.onmeasurechild(appbarlayout.java:1391)             @ android.support.design.widget.coordinatorlayout.onmeasure(coordinatorlayout.java:784)             @ android.view.view.measure(view.java:21103)             @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6462)             @ android.widget.framelayout.onmeasure(framelayout.java:185)             @ android.support.v7.widget.contentframelayout.onmeasure(contentframelayout.java:139)             @ android.view.view.measure(view.java:21103)             @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6462)             @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1464)             @ android.widget.linearlayout.measurevertical(linearlayout.java:758)             @ android.widget.linearlayout.onmeasure(linearlayout.java:640)             @ android.view.view.measure(view.java:21103)             @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6462)             @ android.widget.framelayout.onmeasure(framelayout.java:185)             @ android.view.view.measure(view.java:21103)             @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6462)             @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1464)             @ android.widget.linearlayout.measurevertical(linearlayout.java:758)             @ android.widget.linearlayout.onmeasure(linearlayout.java:640)             @ android.view.view.measure(view.java:21103)             @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6462)             @ android.widget.framelayout.onmeasure(framelayout.java:185)             @ com.android.internal.policy.decorview.onmeasure(decorview.java:873)             @ android.view.view.measure(view.java:21103)             @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:2580)             @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1639)             @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1890)             @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1519)             @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:7113)             @ android.view.choreographer$callbackrecord.run(choreographer.java:927)             @ android.view.choreographer.docallbacks(choreographer.java:702)             @ android.view.choreographer.doframe(choreographer.java:638) 09-12 15:42:14.140 9120-9120/id.co.ajsmsig.nb e/androidruntime:                  @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:913)             @ android.os.handler.handlecallback(handler.java:751)             @ android.os.handler.dispatchmessage(handler.java:95)             @ android.os.looper.loop(looper.java:154)             @ android.app.activitythread.main(activitythread.java:6780)             @ java.lang.reflect.method.invoke(native method)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1496)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1386) 

is there wrong code? hope can me solve problem. thank much.

basically you're trying column doesn't exist in cursor. either line

int groupid = cursor.getint(cursor.getcolumnindex(mactivity.getstring(r.string._id))); 

or

string nama = cursor.getstring(cursor.getcolumnindexorthrow("spaj_id")); 

because you're doing unions in cursor query, believe it's second line. you're specifying a.spaj_id in query (which common column name across tables), recommend using alias using as keyword instead e.g.

select a._id, a.spaj_id res_spaj_id, ... 

and using res_spaj_id in getstring:

string nama = cursor.getstring(cursor.getcolumnindexorthrow("res_spaj_id")); 

alternatively (and i've not tried might wrong), use table qualifier:

string nama = cursor.getstring(cursor.getcolumnindexorthrow("a.spaj_id")); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -