CursorLoader in Fragment
Clash 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?
java android asynchronous sqlite cursor
add a comment |Â
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?
java android asynchronous sqlite cursor
add a comment |Â
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?
java android asynchronous sqlite cursor
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?
java android asynchronous sqlite cursor
edited Apr 15 at 4:12
Jamalâ¦
30.1k11114225
30.1k11114225
asked Apr 5 at 11:57
BadCash
1265
1265
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password