使用强大的SimpleAdapter创建ListView

这几天一直在回归练习搭建listView的技能,今日学习SimpleAdapter的使用,在此总结一下,SimpleAdapter应该说是非常强大的,可以说ListView的大部分应用场景,都可以通过SimpleAdapter来提供列表项.当然,文字都太无力了,我们还是直接上代码吧:

定义界面布局文件:

<?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="wrap_content"
    android:orientation="horizontal"
    >
    <!--定义一个ListView-->
    <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
上面的界面布局文件仅定义了一个ListView,该ListView将会显示由SimpleAdapter提供的列表项.

开始完善Activity代码:

public class MainActivity extends AppCompatActivity {

    private String[] names = new String[]{"哪吒","黛玉","刘备","李白"};
    private String[] descs = new String[]{"可爱的顽孩","一个总是伤感的女孩","一个擅长发挥无能的渣渣","浪漫主义诗人"};
    private int[] imageIds = new int[]{R.drawable.govaffairs_press,R.drawable.home_press
            ,R.drawable.newscenter_press,R.drawable.setting_press};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建一个List集合,List集合的元素是map
        List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
        for (int i = 0;i<names.length;i++){
            Map<String,Object> listItem = new HashMap<String,Object>();
            listItem.put("header",imageIds[i]);
            listItem.put("personName",names[i]);
            listItem.put("desc",descs[i]);
            listItems.add(listItem);
        }
        //创建一个SimpleAdapter
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItems,R.layout.simple_item,
                new String[]{"personName","header","desc"},
                new int[] {R.id.name,R.id.header,R.id.desc});
        ListView list = (ListView) findViewById(R.id.mylist);
        //ListView设置Adapter
        list.setAdapter(simpleAdapter);
    }
}
可以看到创建了一个SimpleAdapter,这个地方就要注意此处创建对象,需要的是5个参数,后4个参数在这里备注一下:

***第2个参数:该参数应该是一个List<?extends Map<String,?>>类型的集合对象,该集合中每个Map<String,?>对象生成一个列表项.

***第3个参数:该参数指定一个界面布局的Id,此处指定R.layout.simple_item,可参考下面布局代码.

***第4个参数:该参数是一个String[]类型的参数,该参数决定提取Map<String,?>对象中那些key对应的value来生成列表项.

***第5个参数:该参数应该是一个int[]类型的参数,该参数决定填充那些组件.

ok,R.layout.simple_item对应的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!--定义一个ImageView,用于作为列表项的一部分-->
    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <!--定义一个TextView,用于作为列表项的一部分-->
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="#f0f"
        android:paddingLeft="10dp"/>
    <!--定义一个TextView,用于作为列表项的一部分-->
    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:paddingLeft="10dp"/>
</LinearLayout>
</LinearLayout>
这样就完成了SimpleAdapter创建listView,下面上图吧.

效果图如下:


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值