ListView最基本的使用方法SimpleAdapter(二)

ListView最基本的使用方法SimpleAdapter(二)
和上面文章一样,简单的屡一下ListView每个item上显示多个控件的用法,通过SampleAdapter实现的。先看代码

listlayout.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">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#6591F9"
        android:dividerHeight="1dp"
        android:fadingEdge="none"
        android:scrollbarStyle="outsideOverlay"
        android:id="@+id/myList">
    </ListView>

</LinearLayout>

listitem.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<!-- 简单定义一个listItem的布局,有三个textView,可以根据具体业务更改布局文件 -->

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/icon"
        android:src="@drawable/pic1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/name"
        android:textSize="15dp"
        android:textColor="#761243"
        android:text="content"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/button"
        android:layout_weight="1"
        android:text="链接"/>

</LinearLayout>

MainActivity的代码

public class MainActivity extends Activity {

    private ListView listView;              
    private List<HashMap<String,Object>> personalList;       
    private SimpleAdapter adapter;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listlayout);

        listView = (ListView)findViewById(R.id.myList);

        personalList = new ArrayList<HashMap<String,Object>>();
        for(int i=0;i<10;i++){
            HashMap<String,Object> map=new HashMap<String,Object>();
            map.put("icon",R.drawable.pic1);
            map.put("name","小明"+i+"号");
            map.put("button","第"+i+"列");
            personalList.add(map);
        }

        adapter = new SimpleAdapter(this,personalList,R.layout.listitem,
          new String[]{"icon","name","button"},new int[]{R.id.icon,R.id.name,R.id.button});


//注意实例化adapter的时候,第二个参数是Adapter要绑定的数据,第三个参数是该List上对应的每一个item要显示的布局,第四个参数是上面构建hashmap是的put的字符串标志,第四个参数是对该字符串对应的具体控件的id,该字符串就是具体控件的id和上面要显示的数据的桥梁,对应关系。
    }
}

使用simpleAdapter的场景:
列表的每一节对应ListView的每一行,通过SimpleAdapter的构造函数,将HashMap的每个键的数据映射到布局文件中对应控件上。这个布局文件一般根据自己的需要来自己定义。梳理一下使用SimpleAdapter的步骤。
(1)根据需要定义ListView每行所实现的布局。
(2)定义一个HashMap构成的列表,将数据以键值对的方式存放在里面。
(3)构造SimpleAdapter对象。
(4)将LsitView绑定到SimpleAdapter上。

也可以再添加listView的响应函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值