在我的应用中,我有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);
}
答案 0 :(得分: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...