JP2014
JP2014

Reputation: 4532

Proper usage of twoLineListItem Android

I am new to development on android and trying to create a list that has a bold header
and a smaller description for each item. Like the one shown here (sorry can't post images yet):
https://i.sstatic.net/UVqzq.png
This is the XML that I have to start with:

<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TwoLineListItem android:layout_width="fill_parent"
android:layout_height="wrap_content" android:mode="twoLine">
<TextView android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@android:id/text1" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:layout_below="@android:id/text1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_alignLeft="@android:id/text1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@android:id/text2" />
</TwoLineListItem>
</LinearLayout> 

Is there a way to populate the list from an array in another xml file? If not how would I populate a list like this from the Java code? Like I said I'm new to development, so I'm probably way off. Thanks for any input!

Upvotes: 2

Views: 10940

Answers (3)

Solostaran14
Solostaran14

Reputation: 1666

In the case of an Array of Objects, you just have to implement your own adapter. It can work for a TwoLineListItem or any other custom layout.

For example, if I have a list of items of the following type :

public class ProfileItem {
    public String host, name, tag;

    public ProfileItem(String host, String name, String tag) {
        this.host = host;
        this.name = name;
        this.tag = tag;
    }

    @Override
    public String toString() {
        return name+"#"+tag;
    }
}

Then I create the following adapter :

public class ProfileItemAdapter extends ArrayAdapter<ProfileItem> {

private Context context;
private int layoutResourceId;   
private List<ProfileItem> objects = null;

public ProfileItemAdapter(Context context, int layoutResourceId, List<ProfileItem> objects) {
    super(context, layoutResourceId, objects);
    this.context = context;
    this.layoutResourceId = layoutResourceId;
    this.objects = objects;
}

public View getView(int position, View convertView, ViewGroup parent)  {
    View v = convertView;
    if(v == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        v = inflater.inflate(layoutResourceId, parent, false);
    }

    ProfileItem item = objects.get(position);
    TextView titletext = (TextView)v.findViewById(android.R.id.text1);
    titletext.setText(item.toString());
    TextView mainsmalltext = (TextView)v.findViewById(android.R.id.text2);
    mainsmalltext.setText(item.host);
    return v;
}

}

Then everything is in place, in my activity (or fragment), I just have to set this adapter in the onCreate method :

setListAdapter(new ProfileItemAdapter(getActivity(),
            android.R.layout.two_line_list_item, // or my own custom layout 
            ProfileListContent.ITEMS));

Upvotes: 1

Siddharth Lele
Siddharth Lele

Reputation: 27748

I have a similar implementation, but instead of a 2 line listview, I have 3 lines.

This is the XML:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"  >
    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#111111"
        android:textSize="17sp"
        android:textStyle="bold"
        android:text="PROJECT NAME"
        android:typeface="monospace"
        android:paddingBottom="2dp"
        android:paddingTop="2dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:gravity="left|center"   >
    </TextView>
    <TextView
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#111111"
        android:text="Selected Type"
        android:textSize="16sp"
        android:paddingBottom="1dp"
        android:paddingTop="1dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"  >
    </TextView>
    <TextView
        android:id="@+id/text3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#111111"
        android:textSize="16sp"
        android:paddingTop="1dp"
        android:paddingBottom="1dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:text="Project Description"  >
    </TextView>
</LinearLayout>

And this is the JAVA code for filling the lines with data returned from a DB. This code is in the onCreate() method:

String[] fields = new String[]  {   db.TABLE_PRJ_NAME, db.TABLE_PRJ_TYPE, db.TABLE_PRJ_DESC };
    int[] views = new int[] {   R.id.text1, R.id.text2, R.id.text3  };

    c = db.getAllProjects();
    startManagingCursor(c);

    // Set the ListView
    SimpleCursorAdapter prjName = new SimpleCursorAdapter(
            this,
            R.layout.project_list_rows,
            //android.R.layout.simple_list_item_1,
            c, fields, views);
    setListAdapter(prjName);

Upvotes: 0

Related Questions