自定义列表项的ListView的android [英] Custom list item to ListView android

查看:154
本文介绍了自定义列表项的ListView的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩清单活动的教程:

I have been playing with the list activity tutorial here:

<一个href="http://developer.android.com/resources/tutorials/views/hello-listview.html">http://developer.android.com/resources/tutorials/views/hello-listview.html

它告诉你要开始扩展列表活动。

which tells you to start off extending List activity.

by public class Main extends ListActivity {

这是基于夸大一个TextView只布局。

Which is based on inflating a textview only layout.

   <?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="16sp" >
</TextView>

如果我想通过添加图片更多自定义布局,列表适配器使用这个如果于方法让我怎么办呢etc-是有可能高于一个额外的线性布局?

If I want to customize the layout more by adding images,and an extra linear layout above the list adapter etc- is it possible using this method- if so how do I do it?

推荐答案

这是可以通过使用<一个href="http://developer.android.com/reference/android/widget/SimpleAdapter.html"><$c$c>SimpleAdapter.

It is possible by using a SimpleAdapter.

下面是一个例子:

    // Create the item mapping
    String[] from = new String[] { "title", "description" };
    int[] to = new int[] { R.id.title, R.id.description };

现在标题被映射到 R.id.title 和说明,以 R.id.description (在下面的XML定义)。

Now "title" is mapped to R.id.title, and "description" to R.id.description (defined in the XML below).

    // Add some rows
    List<HashMap<String, Object>> fillMaps = new ArrayList<HashMap<String, Object>>();

    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("title", "First title"); // This will be shown in R.id.title
    map.put("description", "description 1"); // And this in R.id.description
    fillMaps.add(map);

    map = new HashMap<String, Object>();
    map.put("title", "Second title");
    map.put("description", "description 2");
    fillMaps.add(map);

    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.row, from, to);
    setListAdapter(adapter);

这是相应的XML布局,这里命名为 row.xml

This is the corresponding XML layout, here named row.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
        android:id="@+id/description"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>

我用了两个TextViews,但它的工作原理相同的任何一种观点。

I used two TextViews but it works the same with any kind of view.

这篇关于自定义列表项的ListView的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆