Dan Harvey
Dan Harvey

Reputation: 798

List view with title, image and text?

I need to make a list view with an image on left, a title floated right of that image and a description under that. I have checked out the list view in the docs but i cannot seem to find an example for this. I have a tab view as defined in XML (as per android example tabs1) with a list view as the first tab content. However i want to add the content via a RSS newsfeed to the listview in code.

I was thinking of using a webview using an html injection string but how would i insert images from the drawable folder. (these images are just a "news icon" stored locally, not an image from the internet)

Sorry if it is a bit of a nooby question, but any help appreciated :)

Upvotes: 8

Views: 14895

Answers (4)

user3664529
user3664529

Reputation:

You need to design listview items to contain textview and imageview.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal" >

 <ImageView
 android:id="@+id/imageView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="40dp"
 android:layout_gravity="center_vertical"
 android:text="TextView" />
<TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="40dp"
 android:layout_gravity="center_vertical"
 android:text="TextView" />

</LinearLayout>

Then you need to design listview layout. Then you need to inflate your row item layout by getting layout inflater.

  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View single_row = inflater.inflate(R.layout.list_row, null,
 true);

Upvotes: 0

Mayank
Mayank

Reputation: 8852

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





    <GridLayout
        android:id="@+id/gridLayout1"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1.07"
        android:columnCount="2" >

        <ImageView
            android:id="@+id/chkItemImage"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_column="0"
            android:layout_gravity="left"
            android:layout_row="0"
            android:layout_rowSpan="3"
            android:src="@drawable/ic_launcher"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/chkItemName"
            android:layout_column="1"
            android:layout_gravity="left"
            android:layout_row="0"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/chkItemDescription"
            android:layout_column="1"
            android:layout_gravity="left"
            android:layout_row="1"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/chkItemPrice"
            android:layout_column="1"
            android:layout_gravity="left"
            android:layout_row="2"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />


    </GridLayout>

Upvotes: 0

Martin
Martin

Reputation: 4816

You might also check out this page here for a very nice layout android-layout-tricks-1

Upvotes: 1

kgiannakakis
kgiannakakis

Reputation: 104178

I suppose that the structure you are describing refers to the content of the ListView items. You can achieve this by defining a layout for the individual item. The code below has worked for me in a similar situation:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:padding="6dip" android:layout_height="?android:attr/listPreferredItemHeight">
    <ImageView
        android:id="@+id/result_icon"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="6dip"        
        android:src="@drawable/image1"/>
    <TextView
        android:id="@+id/result_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        android:layout_toRightOf="@id/result_icon"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"        
        android:layout_alignWithParentIfMissing="true"                
        android:gravity="center_vertical"
        android:text="Title" />
    <TextView  
        android:id="@+id/result_second_line"
        android:layout_width="fill_parent"
        android:layout_height="26dip"      
        android:layout_toRightOf="@id/result_icon"
        android:layout_below="@id/result_name"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"        
        android:singleLine="true"
        android:ellipsize="marquee"
        android:text="Second line" />
</RelativeLayout>

You then need to extend a BaseAdapter to use with your ListActivity. You will need to inflate the layout and populate it with data from the RSS feed.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Result result = this.results.get(position);


    LayoutInflater inflater = (LayoutInflater) context.getSystemService(
                                        Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout view = (RelativeLayout) 
            inflater.inflate(R.layout.result_item, null, false);

    ImageView image = (ImageView) view.findViewById(R.id.result_icon);
    image.setImageResource(result.imageResource);

    TextView name = (TextView) view.findViewById(R.id.result_name);
    name.setText(result.location);

    TextView secondLine = (TextView) view.findViewById(R.id.result_second_line);
    secondLine.setText(result.shortDescription);

    return view;
}

Upvotes: 10

Related Questions