2012-01-05 67 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