2012-01-05 65 views
0

好的,所以到目前爲止,我讓我的應用程序顯示一個活動,並可以鏈接到基於點擊案件的不同活動。現在,我的問題是,你如何使用與listview項目中的數據相對應的數據填充打開的活動?ListView在點擊後打開活動,用數據填充新打開的活動,如何?

易於表示,

ListView (for ex. 10 items) 

On click, opens ContentViewer activity 

void ContentViewer::onCreate() { 
    setContentView contentviewer(xml); 
} 
(contentviewer has different textviews and imageviews with diff IDs.) 

現在,當contentviewer通過點擊情況下爲0(在列表視圖第一項),然後數據0,圖像0等打開。

任何想法?

回答

1

從本質上講,您正在尋找將params傳遞給第一個活動的方法嗎?具體方法如下:

Activity1.java: 

    Intent intent = new Intent(this, Activity2.class); 
    intent.putExtra(ReportActivity.REPORT_TYPE, reportId); 
    startActivity(intent); 

Activity2.java: 

protected void onCreate(Bundle savedInstanceState) { 
    Intent intent = getIntent(); 
    if (intent != null) { 
     Bundle bundle = intent.getExtras(); 
     if (bundle != null) { 
      int reportId = bundle.getInt(REPORT_TYPE); 
     } 
    } 

的此想法把名稱/值對到您Intent從調用活動,並在您閱讀的名稱/值對關閉調用意圖的所謂活動。在上面的示例中,我將int(reportID)傳遞給調用活動。您可以將任何其他基元類型傳遞給它。如果您想要傳遞自定義對象,則需要實施Parcelable