Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing data to two different classes from click
    text
    copied!<p>In my app i have a <code>ListActivity</code> that pulls data from a Database using <code>LoaderManager</code>. What i want to do is when an item is clicked, i want to start another activity (that pulls data from a database and displays a grid) and also pass the id to my content provider to perform the database query via the content provider. How can do this?</p> <p>I have searched for answers but i just can't seem to find one that solves this problem.</p> <p>I tried to use getIntent() in the ContentProvider but i found out that getIntent() doesn't work like in this question <a href="https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android">How do I pass data between Activities in Android application?</a></p> <p>This is the onListItemClick </p> <pre><code> protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); startActivity(i); } </code></pre> <p>For me to create the next activty, i need to know which item was clicked (hence id). This id i what will be passed to Content Provider</p> <pre><code> public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { switch (mUriMatcher.match(uri)) { case QUESTIONS: Log.d(DBHelper.TAG, "fetching categories"); return myDH.fetchCatgories(); case GRID: //row here is the id from my list activity //myDH is an instance of my DB helper class return myDH.fetchQuestions(row); default: throw new IllegalArgumentException("Unsupported URI: " + uri); } } </code></pre> <p>I have set up a <code>urimatcher</code>. My question is how do i get that id from my list to become the argument for this query. I am using LoaderManager so i have to make the query from the content provider.</p> <p>This is the ContentProvider class</p> <pre><code> private static final int QUESTIONS = 1; private static final int GRID = 2; TestAdapter myTestAdapter; DBHelper myDH; private static final UriMatcher mUriMatcher; static { mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); mUriMatcher.addURI(AUTHORITY, BASE_PATH_CATEGORY, QUESTIONS); mUriMatcher.addURI(AUTHORITY, BASE_PATH_GRID, GRID); } @Override public boolean onCreate() { myDH = new DBHelper(getContext()); try { myDH.createDatabase(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } myDH.openDatabase(); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { switch (mUriMatcher.match(uri)) { case QUESTIONS: Log.d(DBHelper.TAG, "fetching categories"); return myDH.fetchCatgories(); case GRID: //this is where i need the position that was clicked //so that i can use this value in the DBHelper. return myDH.fetchQuestions(row); default: throw new IllegalArgumentException("Unsupported URI: " + uri); } } </code></pre> <p>I am using the ContentProvider to communicate with my DBHelper. This is the relevant part of the code</p> <pre><code> //DBHelper Code public Cursor fetchQuestions(long row) throws SQLException{ Cursor cursor = myDataBase.rawQuery("select _id, used, question_level from questions_en where category" + " = " + row, null); return cursor; } </code></pre> <p>The cursor returned will be used in another activity to populate a grid. </p> <p>Without using LoaderManager, (this method is working but it uses the deprecated startManagingCursor) this is the relevant code for the listActivity </p> <pre><code> @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Intent i = new Intent(this, QuestionSelection.class); i.putExtra(DBHelper.KEY_ID, id); startActivity(i); } </code></pre> <p>Now in the next Activity's onCreate:</p> <pre><code> protected void onCreate(Bundle savedInstanceState) { Bundle extras = getIntent().getExtras(); rowId = extras.getLong(DBHelper.KEY_ID); mDbHelper = new DBHelper(this); try { mDbHelper.createDatabase(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mDbHelper.openDatabase(); Cursor cursor = mDbHelper.fetchQuestions(rowId); startManagingCursor(cursor); String[] from = {DBHelper.KEY_ID, "question_level", "used"}; int[] to = {R.id.grid_text, R.id.grid_image_right, R.id.grid_image_left}; GridView grid = (GridView)findViewById(R.id.question_grid); mAdapter = new SimpleCursorAdapter(this, R.layout.grid_item, cursor, from, to); grid.setAdapter(mAdapter); } </code></pre>
 

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