码迷,mamicode.com
首页 > 其他好文 > 详细

ListView之SimpleAdapter的使用

时间:2014-11-14 17:16:31      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:android   style   http   io   ar   使用   sp   java   文件   

SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

参数context:上下文,比如this。关联SimpleAdapter运行的视图上下文

参数data:Map列表,列表要显示的数据,它是包含Map类型的元素的List集合,并且要求Map的键必须是String 类型的。

参数resource:ListView单项布局文件的Id,这个布局就是你自定义的布局了,你想显示什么样子的布局都在这个布局中。这个布局中必须包括了to中定义的控件id

参数 from:一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称。

参数 to:是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from一一对应。

下面举个例子说明:

自定义布局显示本地资源;列表项的单项显示格式,定义main.xml文件,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView 
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="3px"
        />
    <LinearLayout 
        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:textSize="16sp"
            />
        <TextView
            android:id="@+id/info"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="10sp"
            />
    </LinearLayout>

</LinearLayout>

如果直接继承ListAcitivty,则不需要自定义ListView
public class DemoActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SimpleAdapter adapter = new SimpleAdapter(this, getData(),
                R.layout.main, new String[] { "img", "title", "info" },
                new int[] { R.id.img, R.id.title, R.id.info });
        setListAdapter(adapter);
    }
 private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("img", R.drawable.ic_icon1);
        map.put("title", "zhangsan");
        map.put("info", "asd");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.drawable.ic_icon2);
        map.put("title", "wangwu");
        map.put("info", "sadf");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.drawable.e04b);
        map.put("title", "zhaoliu");
        map.put("info", "fegg");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.drawable.e04e);
        map.put("title", "dog");
        map.put("info", "efdfdg");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.drawable.e11a);
        map.put("title", "hello");
        map.put("info", "every thing");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.drawable.e11d);
        map.put("title", "world");
        map.put("info", "hello world");
        list.add(map);

        return list;
    }
}

 

如果自定义ListView而不是继承ListActivity需要如下实现,定义一个mylist.xml,在ListView中可以定义其他属性。

<?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" >
    <ListView 
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ></ListView>
</LinearLayout>

如此,DemoActivity中的代码就要做如下修改了:

public class Demo13Activity extends Activity {
    private ListView lv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylist);
        lv = (ListView)findViewById(R.id.listview);
        SimpleAdapter adapter = new SimpleAdapter(this, getData(),
                R.layout.main, new String[] { "img", "title", "info" },
                new int[] { R.id.img, R.id.title, R.id.info });
              lv.setAdapter(adapter);
    }

 

ListView之SimpleAdapter的使用

标签:android   style   http   io   ar   使用   sp   java   文件   

原文地址:http://www.cnblogs.com/superior/p/4097516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!