android - how/where to call notifyDataSetChanged from another fragment -
i have fragment listview show content of list. update , delete items in list through fragment - dialog fragment (using content provider's delete , update methods) , works fine except "live" refresh of list view.
i know should calling notifydatasetchanged on adapter somewhere have no idea where.
since user delete/update through different fragment i'm guessing should calling notifydatasetchanged on adapter through fragment somehow. investigated little , understand should run on ui thread.
i tried several things no success. please have @ code. this fragment holds listview:
public class shiftsfragment extends android.app.fragment implements loadercallbacks<cursor> { private typeface tf, roboto; private contentresolver cr; public shiftsadapter sa = null; public static shiftsdialog sd = null; public static shiftsfragment newinstance(string title) { shiftsfragment pagefragment = new shiftsfragment(); return pagefragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); sethasoptionsmenu(true); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); cr = getactivity().getbasecontext().getcontentresolver(); getloadermanager().initloader(0, null, this); } @override public void onresume() { super.onresume(); getloadermanager().restartloader(0, null, this); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.shifts, container, false); roboto = typeface.createfromasset(getactivity().getassets(), "fonts/juralight.ttf"); tf = typeface.createfromasset(getactivity().getassets(), "fonts/advert.ttf"); listview ls = (listview) view.findviewbyid(android.r.id.list); sa = new shiftsadapter(getactivity().getbasecontext(), null, 0); ls.setadapter(sa); ls.setonitemlongclicklistener(new onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> arg0, view arg1, int linenum, long arg3) { sd = new shiftsdialog(); sd.newinstnace(arg3); sd.show(getfragmentmanager(), "shifts_dialog"); return false; } }); return view; } @override public loader<cursor> oncreateloader(int id, bundle args) { cursorloader loader = new cursorloader(getactivity() .getapplicationcontext(), myprovider.shifts_uri, null, null, null, null); return loader; } @override public void onloadfinished(loader<cursor> arg0, cursor cursor) { sa.swapcursor(cursor); log.i("shifts numbers", "" + cursor.getcount()); } @override public void onloaderreset(loader<cursor> arg0) { } }
this dialog fragment update/delete based on button user clicked:
public class shiftsdialog extends dialogfragment { private typeface roboto; private static long position = 0; private contentresolver cr = null; public static shiftsdialog newinstnace(long i) { position = i; shiftsdialog dial = new shiftsdialog(); return dial; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); cr = getactivity().getbasecontext().getcontentresolver(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { getdialog().getwindow().requestfeature(window.feature_no_title); getdialog().getwindow().setbackgrounddrawable(new colordrawable(0)); roboto = typeface.createfromasset(getactivity().getassets(), "fonts/juralight.ttf"); view view = inflater.inflate(r.layout.dialog, container, false); textview tv = (textview) view.findviewbyid(r.id.options); tv.settypeface(roboto); button delete = (button) view.findviewbyid(r.id.deletebtn); delete.settypeface(roboto); // onclick listener delete function delete.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // complete in official way string = string.valueof(position); uri rowuri = contenturis.withappendedid(myprovider.shifts_uri, position); cr.delete(rowuri, null, null); } }); button edit = (button) view.findviewbyid(r.id.editbtn); edit.settypeface(roboto); // onclick listener editing shift edit.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { editdate_dialog ed = new editdate_dialog(); ed.newinstnace(position); ed.show(getfragmentmanager(), "edit_dialog"); dismiss(); } }); return view; } }
my contentproiver's delete , update methods (rest of code omitted brevity):
@override public int delete(uri uri, string selection, string[] selectionargs) { sqlitedatabase db = helper.getwritabledatabase(); log.d("uri", uri.tostring()); switch (urimatcher.match(uri)) { case single_row: string rowid = uri.getpathsegments().get(1); selection = key_id + "=" + rowid + (!textutils.isempty(selection) ? " , (" + selection + ')' : ""); default: break; } if (selection == null) selection = "1"; // execute deletion. int deletecount = db.delete(helper.database_table, selection, selectionargs); log.d("deletecount", "" + deletecount); log.i("selection", "" + selection); getcontext().getcontentresolver().notifychange(uri, null); return deletecount; } @override public int update(uri uri, contentvalues values, string selection, string[] selectionargs) { sqlitedatabase db = helper.getwritabledatabase(); switch (urimatcher.match(uri)) { case single_row: string rowid = uri.getpathsegments().get(1); selection = key_id + "=" + rowid + (!textutils.isempty(selection) ? " , (" + selection + ')' : ""); default: break; } int updatecount = db.update(helper.database_table, values, selection, selectionargs); log.d("updated", "" + updatecount); getcontext().getcontentresolver().notifychange(uri, null); return updatecount; }
Comments
Post a Comment