android - Cursor Loaders filtering, old data shown in for a split second -
i have weird behavior in app i'm struggling understand. have 2 lists (a & b), initial view list textbox on top filters results of list b.
i use cursor loaders, adapters , listview implement these 2 lists.
when user enters keyword search, restart loader b entered keyword , load adapter , set listview display adapter b.
everything runs fine but instead of displaying results right away, wrong behavior happens: full list b shown split second, filtered results shows. of course creates bad ux (user experience), want achieve here filtered results shown right away without split second view of full list b.
i want understand why behavior occurring... after implementation style of querying 1 used in official example of cursor loaders :)
the code big these relevant parts: ontextchange produces bad behaviour:
@override public void ontextchanged(charsequence s, int start, int before, int count) { getloadermanager().restartloader(loader_id_2, null, this); }
these cursorloader methods:
public loader<cursor> oncreateloader(int id, bundle args) { if (id== loader_id_1) { //calculate , return cursorloader } else { if (mfilter == null || mfilter.length() == 0) { return new cursorloader(this, uri, cadapter.projection, null,null,null); } //else // calculations return new cursorloader(this,uri, cadapter.projection, wherestmt, whereargs, orderby); } } public void onloadfinished(loader<cursor> loader, cursor cursor) { if (loader.getid()== loader_id_2) { mcadapter.swapcursor(cursor); } else if (loader.getid()== loader_id_1) { matrixcursor newcursor = new matrixcursor(dadapter.projection); if (cursor.movetofirst()) { { // i'm grouping filtering returned cursor , populating new matrix cursor filtered data because didn't find better way filter , group results (get distinct rows) }while (cursor.movetonext()); } mdadapter.changecursor(newcursor); //i used change cursor because cursor loader handle closing "cursor" , not "newcursor" created , put adapter, hope doesn't backfire :) } public void onloaderreset(loader<cursor> loader) { if (loader.getid()== loader_id_2 ) mcadapter.swapcursor(null); else if (loader.getid()== loader_id_1 ) mdadapter.swapcursor(null); }
try this;
public loader<cursor> oncreateloader(int id, bundle args) { loader<cursor> cursorloader if (id== loader_id_1) { //calculate , return cursorloader if(cursorloader == null) { return new cursorloader(this,uri,
cadapter.projection, wherestmt, whereargs, orderby); }//end inner if
}// end if else if (mfilter == null || mfilter.length() == 0) { return new cursorloader(this, uri, cadapter.projection, null,null,null);
}// end else if
}// end method
Comments
Post a Comment