ListView控件和如何向ListView控件添加数据适配器

本文介绍了ListView控件的使用,包括如何通过SimpleAdapter、ArrayAdapter和自定义BaseAdapter向ListView添加数据。详细讲解了各个适配器的构造函数参数,并提供了实例代码,帮助读者掌握ListView数据适配器的运用。
摘要由CSDN通过智能技术生成

      ListView非常常用,它可以实现信息列表化及对列表信息的选择,这是实现往ListView各种适配器添加数据。适配器种类主要有SimpleAdapter,ArrayAdapter<T>,还有一种就是自己自定义的适配器。这里通过一个ListView控件使用各种适配器添加数据。

    首先定义一个主布局,在主布局里面添加一个TextView控件和一个ListView控件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView 
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="你选择了:"
        />
    <ListView
        android:layout_below="@+id/tv2"
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

接下来编写ListView控件中每个条目中的布局,也可以在代码中动态添加ListView的条目布局。条目简单的定义一个ImageView控件和一个TextView控件,水平放置。

<?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="horizontal">
  
    <ImageView 
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_weight="1"
        />
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:id="@+id/tv"
        android:layout_weight="1"
        />

</LinearLayout>

最后一步就是在activity中实现ListView控件数据的添加,我将实现三种适配器数据的添加。首先第一种实现SimpleAdapter适配器。

package co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值