1

在我的应用程序中,我有一个ListActivity使用LoaderManager. 我想要做的是当一个项目被点击时,我想启动另一个活动(从数据库中提取数据并显示一个网格)并将 id 传递给我的内容提供者以通过内容提供者执行数据库查询。怎么能做到这一点?

我已经搜索了答案,但我似乎无法找到解决这个问题的答案。

我尝试在 ContentProvider 中使用 getIntent() 但我发现 getIntent() 不像这个问题那样工作How do I pass data between Activities in Android application?

这是 onListItemClick

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

}

为了让我创建下一个活动,我需要知道点击了哪个项目(因此是 id)。这个 id 我将传递给 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);
    }
}

我已经设置了一个urimatcher. 我的问题是如何从列表中获取该 id 以成为此查询的参数。我正在使用 LoaderManager,所以我必须从内容提供者那里进行查询。

这是 ContentProvider 类

    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);
    }
}

我正在使用 ContentProvider 与我的 DBHelper 进行通信。这是代码的相关部分

  //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;
}

返回的光标将在另一个活动中用于填充网格。

不使用 LoaderManager,(此方法有效,但它使用已弃用的 startManagingCursor)这是 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);

}

现在在下一个Activity的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);
}
4

1 回答 1

0

ListView当您像这样启动它时,您可以将项目位置从 传递到您的活动:

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

然后,在另一个活动中,您可以像这样获取项目位置:

int itemPosition = getIntent().getIntExtra("itemPosition", -1);
if (itemPosition != -1) {
    // itemPosition is the position in the ListView where the user clicked...
于 2013-11-12T14:15:03.103 回答