2014-07-16 89 views
0

我有一個Activity帶有項目列表。通過點擊一個項目,它會彈出第二個Activity,並在第一個Activity中點擊該項目的詳細信息。顯示列表查看第二項活動詳情活動

我的第一活動:(我傳遞一個string[]經由Intent)我的第二活性

Bundle bundle= new Bundle(); 
bundle.putStringArray("item_details", new String[]{item_name, item_desc, item_date, item_loc}); 

Intent intent = new Intent(getApplicationContext(), DataProductDetail.class); 
intent.putExtras(extras); 

(接收string[]

Bundle bundle = this.getIntent().getExtras(); 
String[] itemArray = bundle.getStringArray("item_details"); 

ArrayAdapter<String> itemArrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, itemArray); 

ListView list = (ListView)findViewById(R.id.list); 
     detailsList.setAdapter(itemArrayAdapter); 

佈局LIST_ITEM

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:textSize="18sp" 
    android:textColor="#0c2d63" /> 

我的問題/問題:

我希望我的應用程序第一個屏幕,看起來像這樣(這是工作):

item1 
item2 
item3 

問題:

的第二個屏幕當item1被點擊時第一個Activity應該是這樣的(不知道如何得到這個,上面的代碼不起作用):

Name: xyz is "item_name" from array "itemArray" received from intent 
Description: This is the description from "item_desc" from array "itemArray" 
Date: This is the date string grabbed from item_date from array "itemArray" 

如何獲得如上面第二個屏幕所示的佈局?我想要Name, Description, Date顯示在第二個屏幕上,並將數組中的某些元素分別顯示爲項目詳細信息。

回答

1

至於你問這裏是layout xml文件第二活動的一個例子:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Name: " /> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="xyz is item_name from array itemArray received from intent" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Description: " /> 

    <TextView 
     android:id="@+id/description" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is the description from item_desc from array itemArray" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Date: " /> 

    <TextView 
     android:id="@+id/date" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is the date string grabbed from item_date from array itemArray" /> 

</LinearLayout> 
</LinearLayout> 

,然後將截圖:

enter image description here

對於TextViewsIDs描述 and date yo你以編程方式設置文本調用setText()方法。

當然,因爲它只是一個例子,您可能需要添加一些屬性來對齊文本等。