In my app i have a ListActivity
that pulls data from a Database using LoaderManager
. 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?
I have searched for answers but i just can't seem to find one that solves this problem.
I tried to use getIntent() in the ContentProvider but i found out that getIntent() doesn't work like in this question How do I pass data between Activities in Android application?
This is the onListItemClick
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
startActivity(i);
}
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
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);
}
}
I have set up a urimatcher
. 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.
This is the ContentProvider class
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);
}
}
I am using the ContentProvider to communicate with my DBHelper. This is the relevant part of the 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;
}
The cursor returned will be used in another activity to populate a grid.
Without using LoaderManager, (this method is working but it uses the deprecated startManagingCursor) this is the relevant code for the listActivity
@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);
}
Now in the next Activity's onCreate:
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);
}