android - SimpleCursorAdapater with Filtered Listview - Error Attempting to access a closed CursorWindow -
i have app 3 tabs. in tab1 have listview cursor adapter, in tab2 , tab3 have textviews.
the problems happens when put code filter listview , next, click randomly in tabs. when come tab1 listview error.
i tracking problem , @ setfilterqueryprovider. if rid off filter works well. doing wrong ?
the error is:
d/androidruntime: shutting down vm e/androidruntime: fatal exception: main process: net.techabout.medappointment, pid: 10716 android.database.staledataexception: attempting access closed cursorwindow.most probable cause: cursor deactivated prior calling method. @ android.database.abstractwindowedcursor.checkposition(abstractwindowedcursor.java:139) @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:50) @ android.database.cursorwrapper.getstring(cursorwrapper.java:137) @ net.techabout.medappointment.agendacursoradapter.bindview(agendacursoradapter.java:86) @ android.support.v4.widget.cursoradapter.getview(cursoradapter.java:261) @ android.widget.abslistview.obtainview(abslistview.java:2346)
the error occurs in adapter class:
public void bindview(view view, context context, final cursor cursor) { viewholder viewholder = (viewholder) view.gettag(); string temp = cursor.getstring(cursor.getcolumnindexorthrow(dataprovider.col_ag_indice)); //**error here**
my fragment is:
public class agendamedfragment extends fragment implements loadermanager.loadercallbacks<cursor> { private static final string tag = agendamedfragment.class.getsimplename(); // these variables hold date values later agendacursoradapter adapter; listview listview; private int mposition = listview.invalid_position; private static final string selected_key = "selected_position"; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.agendamedfrag, null); listview = (listview) view.findviewbyid(r.id.list_cons); view v = (view) view.findviewbyid(r.id.empty_cons); listview.setemptyview(v); adapter = new agendacursoradapter(getactivity(), null,0); listview.setadapter(adapter); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int position, long l) { mposition = position; } }); if (savedinstancestate != null && savedinstancestate.containskey(selected_key)) { mposition = savedinstancestate.getint(selected_key); } edittext myfilter = (edittext) view.findviewbyid(r.id.myfilter); myfilter.addtextchangedlistener(new textwatcher() { public void aftertextchanged(editable s) { adapter.getfilter().filter(s.tostring()); } public void beforetextchanged(charsequence s, int start, int count, int after) { } public void ontextchanged(charsequence s, int start, int before, int count) { adapter.getfilter().filter(s.tostring()); } }); adapter.setfilterqueryprovider(new filterqueryprovider() { public cursor runquery(charsequence constraint) { cursor cursor = getfilterednames(constraint != null ? constraint.tostring() : null); return cursor; } }); return view; } public cursor getfilterednames (charsequence constraint) { sqlitedatabase mdb = dataprovider.getdb(); sqlitequerybuilder querybuilder = new sqlitequerybuilder(); querybuilder.settables( dataprovider.table_agenda ); string ascolumnstoreturn[] = {"*"}; if (constraint == null || constraint.length () == 0) { // return full list return querybuilder.query(mdb, ascolumnstoreturn, null, null, null, null, dataprovider.col_id + " desc"); } else { string value = "%"+constraint.tostring()+"%"; return mdb.query(dataprovider.table_agenda, ascolumnstoreturn, "nome_cliente ? ", new string[]{value}, null, null, null); } } @override public loader<cursor> oncreateloader(int i, bundle bundle) { cursorloader loader = new cursorloader(getactivity(), dataprovider.content_uri_agenda, null, null, null, dataprovider.col_id + " desc"); return loader; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); if (getloadermanager().getloader(0)==null) { getloadermanager().initloader(0, null, this); } } @override public void onloaderreset(loader<cursor> loader) { adapter.swapcursor(null); } @override public void onloadfinished(loader<cursor> loader, cursor data) { adapter.swapcursor(data); if (mposition != listview.invalid_position) { listview.smoothscrolltoposition(mposition); } } @override public void onsaveinstancestate(bundle outstate) { if (mposition != listview.invalid_position) { outstate.putint(selected_key, mposition); } super.onsaveinstancestate(outstate); } }
my adapter is:
public class agendacursoradapter extends cursoradapter { public static class viewholder { public final button butdesmarcar; public final button butvermapa; public final textview mprotocolo; public final textview mnomecliente; public final textview mnomehosp; public final textview mnomemed; public final textview mnomeconv; public final textview mnomeplano; public final textview mdatamarcada; public final textview mhoramarcada; public viewholder(view view) { butdesmarcar = (button) view.findviewbyid(r.id.butdesmarcar); butvermapa = (button) view.findviewbyid(r.id.but_ender_hosp_cons); mprotocolo = (textview) view.findviewbyid(r.id.tvindice); mnomecliente = (textview) view.findviewbyid(r.id.tvnomecliente); mnomehosp = (textview) view.findviewbyid(r.id.tvnomehosp); mnomemed = (textview) view.findviewbyid(r.id.tvnomemed); mnomeconv= (textview) view.findviewbyid(r.id.tvnomeconv); mnomeplano= (textview) view.findviewbyid(r.id.tvnomeplan); mdatamarcada= (textview) view.findviewbyid(r.id.tvdatamarcada); mhoramarcada= (textview) view.findviewbyid(r.id.tvhoramarcada); } } public agendacursoradapter(context context, cursor c, int flags) { super(context, c, flags); } @override public view newview(context context, cursor cursor, viewgroup parent) { int layoutid = r.layout.singlerowagenda; view view = layoutinflater.from(context).inflate(layoutid, parent, false); viewholder viewholder = new viewholder(view); view.settag(viewholder); return view; } @override public void bindview(view view, context context, final cursor cursor) { viewholder viewholder = (viewholder) view.gettag(); string temp = cursor.getstring(cursor.getcolumnindexorthrow(dataprovider.col_ag_indice)); viewholder.mprotocolo.settext(temp); temp = cursor.getstring(cursor.getcolumnindexorthrow(dataprovider.col_ag_nome_cliente)); viewholder.mnomecliente.settext(temp); temp = cursor.getstring(cursor.getcolumnindexorthrow(dataprovider.col_ag_nome_hosp)); viewholder.mnomehosp.settext(temp); temp = cursor.getstring(cursor.getcolumnindexorthrow(dataprovider.col_ag_nome_med)); viewholder.mnomemed.settext(temp); temp = cursor.getstring(cursor.getcolumnindexorthrow(dataprovider.col_ag_nome_conv)); viewholder.mnomeconv.settext(temp); temp = cursor.getstring(cursor.getcolumnindexorthrow(dataprovider.col_ag_nome_plano)); viewholder.mnomeplano.settext(temp); temp = cursor.getstring(cursor.getcolumnindexorthrow(dataprovider.col_ag_data_marcada)); viewholder.mdatamarcada.settext(temp); temp = cursor.getstring(cursor.getcolumnindexorthrow(dataprovider.col_ag_hora_marcada)); viewholder.mhoramarcada.settext(temp); } }
Comments
Post a Comment