在我的應用程序中,我有一個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我什麼都會被傳遞給內容提供商
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在未來活動的onCreate相關代碼現在
@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);
}
:
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什麼補充,使問題更清晰? – Jimi