CursorLoader in Fragment

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I'm not sure how to properly implement a simple CursorLoader that loads data directly from a SQLite database and displays it in a ListView in a Fragment, as most examples I've found require creating a ContentProvider.



The code I have at the moment seems to be working as expected, but I want to make sure there are no hidden surprises that are waiting to crash my application.



public class TodoFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> 

private TodoAdapter mAdapter = null;

public TodoFragment()
// Required empty public constructor



@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, null, this);


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.todo_fragment, container, false);

mAdapter = new TodoAdapter(getActivity(), null, 0);

ListView listView = ((ListView) view.findViewById(R.id.todo_listview));
listView.setAdapter(mAdapter);

return view;


/**
* My custom CursorLoader for Todo-items
*/
private static class TodoCursorLoader extends CursorLoader
public TodoCursorLoader(Context context)
super(context);


@Override
public Cursor loadInBackground()
// This method returns a Cursor to all the items in the DB table
return MyApplication.getDbHelper().getAllTodoItems();





/**
* LoaderManager.LoaderCallbacks implementation
*/
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args)
return new TodoCursorLoader(getActivity());


@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
mAdapter.swapCursor(cursor);


@Override
public void onLoaderReset(Loader<Cursor> loader)
mAdapter.swapCursor(null);

/** -- **/



The fragment can be hidden and shown, and INSERT/UPDATE/DELETE can only be performed on the database while the TodoFragment is not visible (from another Fragment).



Does this look correct?







share|improve this question



























    up vote
    0
    down vote

    favorite












    I'm not sure how to properly implement a simple CursorLoader that loads data directly from a SQLite database and displays it in a ListView in a Fragment, as most examples I've found require creating a ContentProvider.



    The code I have at the moment seems to be working as expected, but I want to make sure there are no hidden surprises that are waiting to crash my application.



    public class TodoFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> 

    private TodoAdapter mAdapter = null;

    public TodoFragment()
    // Required empty public constructor



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    super.onActivityCreated(savedInstanceState);
    getLoaderManager().initLoader(0, null, this);


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    View view = inflater.inflate(R.layout.todo_fragment, container, false);

    mAdapter = new TodoAdapter(getActivity(), null, 0);

    ListView listView = ((ListView) view.findViewById(R.id.todo_listview));
    listView.setAdapter(mAdapter);

    return view;


    /**
    * My custom CursorLoader for Todo-items
    */
    private static class TodoCursorLoader extends CursorLoader
    public TodoCursorLoader(Context context)
    super(context);


    @Override
    public Cursor loadInBackground()
    // This method returns a Cursor to all the items in the DB table
    return MyApplication.getDbHelper().getAllTodoItems();





    /**
    * LoaderManager.LoaderCallbacks implementation
    */
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args)
    return new TodoCursorLoader(getActivity());


    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
    mAdapter.swapCursor(cursor);


    @Override
    public void onLoaderReset(Loader<Cursor> loader)
    mAdapter.swapCursor(null);

    /** -- **/



    The fragment can be hidden and shown, and INSERT/UPDATE/DELETE can only be performed on the database while the TodoFragment is not visible (from another Fragment).



    Does this look correct?







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm not sure how to properly implement a simple CursorLoader that loads data directly from a SQLite database and displays it in a ListView in a Fragment, as most examples I've found require creating a ContentProvider.



      The code I have at the moment seems to be working as expected, but I want to make sure there are no hidden surprises that are waiting to crash my application.



      public class TodoFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> 

      private TodoAdapter mAdapter = null;

      public TodoFragment()
      // Required empty public constructor



      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState)
      super.onActivityCreated(savedInstanceState);
      getLoaderManager().initLoader(0, null, this);


      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
      View view = inflater.inflate(R.layout.todo_fragment, container, false);

      mAdapter = new TodoAdapter(getActivity(), null, 0);

      ListView listView = ((ListView) view.findViewById(R.id.todo_listview));
      listView.setAdapter(mAdapter);

      return view;


      /**
      * My custom CursorLoader for Todo-items
      */
      private static class TodoCursorLoader extends CursorLoader
      public TodoCursorLoader(Context context)
      super(context);


      @Override
      public Cursor loadInBackground()
      // This method returns a Cursor to all the items in the DB table
      return MyApplication.getDbHelper().getAllTodoItems();





      /**
      * LoaderManager.LoaderCallbacks implementation
      */
      @Override
      public Loader<Cursor> onCreateLoader(int id, Bundle args)
      return new TodoCursorLoader(getActivity());


      @Override
      public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
      mAdapter.swapCursor(cursor);


      @Override
      public void onLoaderReset(Loader<Cursor> loader)
      mAdapter.swapCursor(null);

      /** -- **/



      The fragment can be hidden and shown, and INSERT/UPDATE/DELETE can only be performed on the database while the TodoFragment is not visible (from another Fragment).



      Does this look correct?







      share|improve this question













      I'm not sure how to properly implement a simple CursorLoader that loads data directly from a SQLite database and displays it in a ListView in a Fragment, as most examples I've found require creating a ContentProvider.



      The code I have at the moment seems to be working as expected, but I want to make sure there are no hidden surprises that are waiting to crash my application.



      public class TodoFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> 

      private TodoAdapter mAdapter = null;

      public TodoFragment()
      // Required empty public constructor



      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState)
      super.onActivityCreated(savedInstanceState);
      getLoaderManager().initLoader(0, null, this);


      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
      View view = inflater.inflate(R.layout.todo_fragment, container, false);

      mAdapter = new TodoAdapter(getActivity(), null, 0);

      ListView listView = ((ListView) view.findViewById(R.id.todo_listview));
      listView.setAdapter(mAdapter);

      return view;


      /**
      * My custom CursorLoader for Todo-items
      */
      private static class TodoCursorLoader extends CursorLoader
      public TodoCursorLoader(Context context)
      super(context);


      @Override
      public Cursor loadInBackground()
      // This method returns a Cursor to all the items in the DB table
      return MyApplication.getDbHelper().getAllTodoItems();





      /**
      * LoaderManager.LoaderCallbacks implementation
      */
      @Override
      public Loader<Cursor> onCreateLoader(int id, Bundle args)
      return new TodoCursorLoader(getActivity());


      @Override
      public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
      mAdapter.swapCursor(cursor);


      @Override
      public void onLoaderReset(Loader<Cursor> loader)
      mAdapter.swapCursor(null);

      /** -- **/



      The fragment can be hidden and shown, and INSERT/UPDATE/DELETE can only be performed on the database while the TodoFragment is not visible (from another Fragment).



      Does this look correct?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 15 at 4:12









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Apr 5 at 11:57









      BadCash

      1265




      1265

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191323%2fcursorloader-in-fragment%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191323%2fcursorloader-in-fragment%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?