1

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);
}
Community
  • 1
  • 1
Jimi
  • 539
  • 5
  • 21

1 Answers1

0

You can pass the item position from the ListView to your activity when you start it like this:

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id); 
    i.putExtra("itemPosition", position);
    startActivity(i);
}

Then, in the other activity, you can get the item position like this:

int itemPosition = getIntent().getIntExtra("itemPosition", -1);
if (itemPosition != -1) {
    // itemPosition is the position in the ListView where the user clicked...
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thanks for the reply. I know how to pass the id using `i.putEtra`. The problem is i need the contentProvider class to be aware of the id that was clicked as well in order to perform the Database query as in this portion of the code `myDH.fetchQuestions(row)` but i cant use getIntent() in the content provider. Using a `SimpleCursorAdapter` works fine but i am trying the contentprovider because i want to use `LoaderManager` because `startmagingcursor` is deprecated. – Jimi Nov 12 '13 at 14:25
  • Sorry, i don't understand the context. Can you post the code in the content provider where you need this and show how you are accessing the content provider? That will provide more context to better understand the peroblem. – David Wasser Nov 12 '13 at 14:51
  • i saw your email in your profile, can i send a picture if you need further explanation? I dont have enough reputation to post image in my question. – Jimi Nov 12 '13 at 16:13
  • Yes, you may send me an email. No guarantees that I will respond, though. – David Wasser Nov 12 '13 at 16:31