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

关于ListView的用法

时间:2015-01-16 23:42:22      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

ListView在Android开发中非常重要,用户展示相同格式的批量数据时就经常用到。在此,记录一下ListView的使用步骤。

1、在界面的xml中添加ListView的组件。

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:id="@+id/black_list"
        >

2、新建一个layout,作为ListView中一个条目的界面。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:id="@+id/black_name"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_marginLeft="10dp"
        android:id="@+id/black_phone"/>
</LinearLayout>

3、建立ListView中的数据队列items

private ArrayList<Map<String,Object>> items = new ArrayList<Map<String,Object>>();

其中,Map用于储存ListView中每个项目的内容

4、为items添加内容

            Map<String,Object> item = new HashMap<String,Object>();
            item.put("name",black_name.get(i));
            item.put("detail",black_phone.get(i));
            items.add(item);

5、建立ListView的适配器

        SimpleAdapter simpleAdapter = new SimpleAdapter(this
                ,items
                ,R.layout.black_number_line
                ,new String[]{"name","detail"}
                ,new int[]{R.id.black_name,R.id.black_phone});

第一个变量为上下文;

第二个变量为ListView的数据队列;

第三个变量为ListView的每个item所显示的界面xml;

第四个变量为ListView的每一个item中所需要的Map中的数据;

第五个变量为ListView的每一个item中每个数据对应的位置。

6、获取ListView并设置适配器

        ListView list = (ListView) findViewById(R.id.black_list);
        list.setAdapter(simpleAdapter);

至此,ListView即可以将输入items中的

 

关于ListView的用法

标签:

原文地址:http://www.cnblogs.com/fishbone-lsy/p/4229946.html

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