0

在我的應用程序中,我有一個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); 
} 
+0

@DavidWasser什麼補充,使問題更清晰? – Jimi

回答

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... 
+0

感謝您的回覆。我知道如何使用'i.putEtra'傳遞id。問題是我需要contentProvider類來注意被點擊的id,以便執行數據庫查詢,就像在代碼myDH.fetchQuestions(row)的這一部分中一樣,但是我不能使用getIntent()內容提供商。使用'SimpleCursorAdapter'工作正常,但我正在嘗試contentprovider,因爲我想使用'LoaderManager',因爲'startmagingcursor'已被棄用。 – Jimi

+0

對不起,我不瞭解上下文。您可以在您需要的內容提供商處發佈代碼,並顯示您如何訪問內容提供商?這將提供更多的背景來更好地理解周圍的情況。 –

+0

我在您的個人資料中看到了您的電子郵件,如果您需要進一步的解釋,我可以發送圖片嗎?我沒有足夠的聲望在我的問題中發佈圖片。 – Jimi

相關問題