简体   繁体   中英

How do I set different top and bottom lines of a list item based on an object's fields?

I have objects called Vendible s that have an id and a name (both String s). To represent dozens of these without actually storing dozens in memory, I have a class called CondensedVendible , which extends Vendible and adds a count variable ( byte ).

Currently, I'm populating my list like the example code did:

    setListAdapter(
            new ArrayAdapter<Vendibles.Vendible>(
                getActivity(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                Vendibles.ITEMS
            )
    );

This, though, just fills each list item with the return of CondensedVendible#toString() . Instead, I want them to look like:

[CondensedVendible#name]
[CondensedVendible#count] left

in much the same way most Android setting screens have a setting name and short description in each list item.

I've looked at blogs and other SO answers , but I just can't understand them! Can anyone help me understand how to do this?

UPDATE


I think I'm close. Here's what I've got now:

Java

    setListAdapter(
            new SimpleAdapter(
                    getActivity(),
                    Vendibles.toMapList(),
                    android.R.layout.two_line_list_item,
                    new String[] { Vendibles.MAP_NAME, Vendibles.MAP_COUNT },
                    new int[] { R.id.list_item_line_1, R.id.list_item_line_2 }
            )
    );

[meanwhile, in Vendibles.java...]

public static final String MAP_COUNT = "count", MAP_NAME = "name";
public static List<Map<String, String>> toMapList()
{
    List<Map<String, String>> ret = new ArrayList<Map<String, String>>();
    for(Vendible v : ITEMS) {
        if (v == null) continue; // no use trying to process notyhing
        Map<String, String> uselesslySmallMap = new HashMap<String, String>();
        uselesslySmallMap.put(MAP_COUNT,
            (v instanceof CondensedVendible
                ? ((CondensedVendible) v).count
                : (byte)1
            )+"" // safer than toString, shorter than String.valueOf
        );
        uselesslySmallMap.put(MAP_NAME, v.name);

        ret.add(uselesslySmallMap);
    }
    return ret;
}

XML

<?xml version="1.0" encoding="utf-8"?>

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Line 1"
        android:id="@+id/list_item_line_1"
        android:textColor="@android:color/primary_text_dark" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Line 2"
        android:id="@+id/list_item_line_2"
        android:textColor="@android:color/secondary_text_dark" />
</LinearLayout>

But it now renders blank list items! It's not like I'm passing it an empty list, because it has the right number of them, they just don't show any text!

Have you had a look at simple_list_item_activated_1.xml in your android sdk? Here it is below:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>

It's a single TextView. If you want a more complex custom item, then you will need to provide your own custom Item layout and list adapter.
A quick google will come up with many tutorials on how to implement your own custom items: http://www.ezzylearning.com/tutorial.aspx?tid=1763429
http://hmkcode.com/android-custom-listview-items-row/
http://www.vogella.com/tutorials/AndroidListView/article.html
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
http://codehenge.net/blog/2011/05/customizing-android-listview-item-layout/
etc

Note: I'm adding a second answer as your Update is really asking a second question.

The second parameter in the SimpleList constructor is a List of Maps. Each of these Maps contains the data for an item.
In your case each of these maps will contain 2 entries. The first mapping the item's name to the key "name", the second mapping the count to the key "count".

Say for example your Vendible class has a name field and a count field like this:

public class Vendible {
    public String name;
    public int count;
}

And vendibles is a SparseArray of your Vendible class. You can then populate your list of HashMaps with your items:

SparseArray<Vendible> vendibles = new SparseArray<Vendible>;
ArrayList<HashMap<String,String>> vendibleList = new ArrayList<HashMap<String,String>>();
...

HashMap<String, String> vendibleItem;
Vendible vendible;
for(int i = 0; i < vendibles.size(); i++) {
    vendibleItem = new HashMap<String, String>;

    vendible = vendibles.valueAt(i);
    vendibleItem.put("name", vendible.name);
    vendibleItem.put("count", vendible.count);

    vendibleList.add(vendibleItem)
}

You can then pass this populated list to the constructor for the SimpleAdapter:

setListAdapter(
       new SimpleAdapter(
                getActivity(),
                vendibleList,
                android.R.layout.two_line_list_item,
                new String[] { "name", "count" },
                new int[] { R.id.list_item_line_1, R.id.list_item_line_2 }
        )
);

If you're still having problems then post your code where you populate your list, to pass to the SimpleAdapter constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM