How / where to call notifyDataSetChanged from another fragment

I have a list snippet to show the contents of my list. I update and delete items in this list through fragment fragment fragment (using the methods of deleting and updating content), and everything works fine EXCLUSIVES the "live" updating of the list.

I know that I should call notifyDataSetChanged on the adapter somewhere, but I have no idea where.

Since the user deletes / updates through another fragment, I assume that I should somehow call notifyDataSetChanged on the adapter through this fragment. I also figured it out a bit, and I understand that I have to run it in the user interface thread.

I have tried several things without success. Please see my code. This is the snippet that contains the 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 is a snippet of the dialog that I am updating / deleting, based on the button that the 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 for delete function delete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Complete it in the official way String where = 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 for editing a 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; } } 

Methods for deleting and updating My ContentProiver (the rest of the code is omitted for 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) ? " AND (" + selection + ')' : ""); default: break; } if (selection == null) selection = "1"; // Execute the 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) ? " AND (" + selection + ')' : ""); default: break; } int updateCount = db.update(helper.DATABASE_TABLE, values, selection, selectionArgs); Log.d("Updated", "" + updateCount); getContext().getContentResolver().notifyChange(uri, null); return updateCount; } 
+4
source share

All Articles