【问题标题】:Passing data to two different classes from click通过点击将数据传递给两个不同的类
【发布时间】:2013-11-09 12:53:08
【问题描述】:

在我的应用程序中,我有一个 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);
}

【问题讨论】:

  • @DavidWasser 有什么可以让问题更清楚的补充吗?

标签: android android-intent android-listview android-sqlite android-contentprovider


【解决方案1】:

当您像这样启动它时,您可以将项目位置从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...

【讨论】:

  • 感谢您的回复。我知道如何使用i.putEtra 传递 id。问题是我需要 contentProvider 类来知道被点击的 id 以便执行数据库查询,就像这部分代码 myDH.fetchQuestions(row) 一样,但我不能在内容提供程序中使用 getIntent()。使用 SimpleCursorAdapter 可以正常工作,但我正在尝试使用 contentprovider,因为我想使用 LoaderManager,因为不推荐使用 startmagingcursor
  • 对不起,我不明白上下文。您能否将代码发布在您需要的内容提供程序中并显示您如何访问内容提供程序?这将为更好地理解问题提供更多背景信息。
  • 我在您的个人资料中看到了您的电子邮件,如果您需要进一步说明,我可以发送图片吗?我没有足够的声誉在我的问题中发布图片。
  • 是的,您可以给我发一封电子邮件。但不能保证我会回复。
猜你喜欢
  • 2015-07-18
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
相关资源
最近更新 更多