Finally I have had some success with TwoLineListItem’s. Here is how I implemented my solution.

Firstly, I created a simple class with two Strings and the associated getters and setters. This class represents each item in the list I will be creating later.

Next, I created a customised ArrayAdapter where I override the getView method. Inside the getView method you need to use the LayoutInflater to get the view from our main.xml file. Then we set the two lines of text.

Lastly, we extend the Activity class to extend from ListActivity. In the onCreate method (which is overridden) we call setListAdapter with our new custom ArrayAdapter as created above. From here we generate some data in our data class, add to a list and pass this list as a parameter to setListAdapter.

Code is below: apologies for the readability of the code. Hopefully I will get the hand of this blogging and fix it up to look good.

item.java <My Data Type>

package com.itemman.main;

public class item {

private String itemName;

private String itemDesc;

public String getitemName() {

returnitemName;

}

publicvoid setitemName(String itemName) {

this.itemName = itemName;

}

public String getitemDesc() {

returnitemDesc;

}

publicvoid setitemDesc(String itemDesc) {

this.itemDesc = itemDesc;

}

}

/res/layout/main.xml <The main layout file>

<?xmlversion=“1.0”encoding=“utf-8”?>

<TwoLineListItemxmlns:android=http://schemas.android.com/apk/res/android&#8221;

  android:layout_width=“fill_parent”

  android:layout_height=“wrap_content”>

<TextViewandroid:id=“@android:id/text1”

android:layout_marginTop=“5dp”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textSize=“15dp”

android:textStyle=“italic”/>

<TextViewandroid:id=“@android:id/text2”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@android:id/text1”

android:layout_alignLeft=“@android:id/text1”

android:paddingBottom=“10dp”

android:includeFontPadding=“false”

android:textSize=“10sp”

android:textStyle=“normal”/>

</TwoLineListItem>

itemArrayAdapter.java <My custom ArrayAdapter class>

package com.itemman.main;

import java.util.List;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.TwoLineListItem;

public class itemArrayAdapter extends ArrayAdapter {

private final int resourceId;

public itemArrayAdapter(Context context, int textViewResourceId, List objects) {

super(context, textViewResourceId, objects);

resourceId = textViewResourceId;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

item t = (item) getItem(position);

if (t == null) {

return null;

}

LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

TwoLineListItem view;

if (convertView == null) {

view = (TwoLineListItem) inflater.inflate(resourceId, parent, false);

} else {

view = (TwoLineListItem) convertView;

}

if (view.getText1() != null) {

view.getText1().setText(t.getitemName());

}

if (view.getText2() != null) {

view.getText2().setText(t.getitemDesc());

}

return view;

}

}

Advertisements