androidandroid-layouttextview

Show ListView item details in second Activity


I have one Activity with a list of items. By clicking an item, it brings up a second Activity with details of the item clicked in the first Activity.

My first activity: (I am passing a string[] via 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);

My second activity: (receiving 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 );

Layout of 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" />

My Question/Problem:

I want my first screen on the app to look like this (which is working):

item1
item2
item3

Problem:

The second screen when item1 is clicked in the first Activity should be like this (not sure how to get this, above code doesn't work):

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"

How can I get such a layout as shown in the second screen above? I want Name, Description, Date to be shown on the second screen and grab certain elements of an array into each for display as item details.


Solution

  • As you asked here is an example of layout xml file for the second Activity:

    <?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>
    

    And the screenshot:

    enter image description here

    For the TextViews with IDs name, description and date you set text programmatically calling setText() method.

    Of course, as it's just an example you might need to add some attributes for aligning the text etc.