Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to manipulate cursor's data before displaying it in a ListView (using a Loader)?
    text
    copied!<p>Each item of my <code>SherlockListFragment</code> contains 3 <code>TextView</code>s : a name and two numbers. The data comes from the table <code>objective</code> of my database, which has the following structure : </p> <pre><code>CREATE TABLE objective ( _id INTEGER PRIMARY KEY, id_project INTEGER NOT NULL, activity_code INTEGER NOT NULL, day_duration INTEGER NOT NULL, week_frequency INTEGER NOT NULL, FOREIGN KEY(id_project) REFERENCES project(id_project) ); </code></pre> <p>Moreover, I have read that populating a list from a cursor should be done by using a loader (particularly when using a database because it can be a very slow operation). I have found a <code>SimpleCursorLoader</code> class <a href="https://stackoverflow.com/a/7422343/615882">here on stackoverflow</a>, but it maps data to a field directly.</p> <p>That is not really what I want, because as you can see, in my <code>objective</code> table I have an <code>activity_code</code>. So I would like to replace it by a string (I have an Enum that lists all of my activity codes and returns, for each one, a string resource identifier).</p> <p>Do you know how I could manipulate data before it is displayed in the <code>TextView</code>s ?</p> <p>Here is my <code>SherlockListFragment</code></p> <pre><code>public class ObjectivesDisplayFragment extends SherlockListFragment implements LoaderManager.LoaderCallbacks&lt;Cursor&gt; { private Activity activity; private SimpleCursorAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.objectives_display, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); activity = getActivity(); String[] columns = new String[] { "activity_code", "day_duration", "week_frequency" }; int[] to = new int[] { R.id.activityName, R.id.objectiveDuration, R.id.objectiveFrequency }; getLoaderManager().initLoader(0x01, null, this); adapter = new SimpleCursorAdapter(activity.getApplicationContext(), R.layout.objective_row, null, columns, to, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); setListAdapter(adapter); } public Loader&lt;Cursor&gt; onCreateLoader(int arg0, Bundle arg1) { return new SimpleCursorLoader(activity) { @Override public Cursor loadInBackground() { DatabaseHelper dbHelper = DatabaseHelper.getInstance(activity); String query = "SELECT _id, activity_code, day_duration, week_frequency FROM objective WHERE id_project = ?"; String[] args = new String[] { "1" }; // projectId Cursor results = dbHelper.getReadableDatabase().rawQuery(query, args); return results; } }; } public void onLoadFinished(Loader&lt;Cursor&gt; arg0, Cursor cursor) { adapter.swapCursor(cursor); } public void onLoaderReset(Loader&lt;Cursor&gt; arg0) { adapter.swapCursor(null); } } </code></pre> <p>EDIT : I do not need to close the cursor and the database in SimpleCursorLoader > loadInBackground, right ? Otherwise the data could not be read. So is the closing operation automatically handled or do I need to do it myself elsewhere?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload