利用ArrayAdapter向ListView中添加数据

<?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:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#f00"
android:dividerHeight="1dp"
android:headerDividersEnabled="false"/>
<!-- 设置使用绿色的分隔条 -->
<ListView
android:id="@+id/list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#0f0"
android:dividerHeight="1dp"
android:headerDividersEnabled="false"/>
</LinearLayout>

我们创建数组,并且将之添加到ArrayAdapter,然后跟ListView关联起来。

val list1 = findViewById<ListView>(R.id.list1)
// 定义一个数组
val arr1 = arrayOf("孙悟空", "猪八戒", "牛魔王")
// 将数组包装为ArrayAdapter
val adapter1 = ArrayAdapter(this, R.layout.array_item, arr1)
// 为ListView设置Adapter
list1.adapter = adapter1
val list2 = findViewById<ListView>(R.id.list2)
// 定义一个数组
val arr2 = arrayOf("Java", "Hibernate", "Spring", "Android")
// 将数组包装为ArrayAdapter
val adapter2 = ArrayAdapter(this, R.layout.checked_item, arr2)
// 为ListView设置Adapter
list2.adapter = adapter2

这里注意,创建adapter是传入三个参数:

  • contex
  • textViewResourceId,这个是布局文件。
  • 数组或list

布局文件是这样的:

详情是这样的:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:checkMark="@drawable/ok"
android:shadowColor="#f0f"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2"/>
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="5dp"
android:shadowColor="#f0f"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2"/>

ArrayAdapter和ListView的更多相关文章

  1. BaseAdapter&ArrayAdapter在ListView中应用

    一:BaseAdapter:共同实现的基类的适配器,是ArrayAdapter SimpleAdapter等的父类, 一般用于比较复杂的ListView,扩展性强. 详细信息可查看谷歌官方API:ht ...

  2. 使用定制的ArrayAdapter制作ListView的Items(翻译)

    Translated by:AcerWang    原文出自:customizing-android-listview-items-with-custom-arrayadapter  背景介绍 对于现 ...

  3. 第二章实例:ArrayAdapter结合ListView列表视图

    package mydefault.packge; import com.example.codeview.R; import android.app.Activity; import android ...

  4. Android ListView ArrayAdapter 的简单使用

    前面写了3篇关于android的文章,其中的演示程序都写在了一个工程中,当时为了方便测试就在启动页MainActivity中放了3个按钮,点击不同的按钮进入不同的示例程序页面,MainActivity ...

  5. ListView与ArrayAdapter的搭配使用

    在android中,ListView是一种很重要的控件,一般的使用中,常建立一个所需类型的ArrayList,再通过ArrayAdapter把ListView绑定到ArrayList上,通过Array ...

  6. ListView总结

    ListView类作为在Android开发中经常会使用到的组件,作为新手,还是感到这一块变化形式还是很多的,需要慢慢学习.现在这里大概总结一下. 基于数组的ListView:使用android:ent ...

  7. 安卓第六天笔记--ListView

    安卓第六天笔记--ListView 1.AdapteView AdapteView 继承ViewGroup它的本质是容器 AdapterView派生了3个子类: AbsListView AbsSpin ...

  8. ListView 搭配SimpleAdapter

    这是SimplerAdapter的构造函数 public SimpleAdapter(Context context, List<? extends Map<String, ?>&g ...

  9. Android开发具体解释之ListView具体解释一

    列表ListView介绍和实例  1.ListView  -- ListActivity -- ListAdapter  2.ArrayAdapter结合ListView进行显示  3.SimpleA ...

随机推荐

  1. WebService接口学习【1】

    工具: 1.Eclipse(or)IDEA编辑器 2.SoapUI测试工具 1.wsdl文件标签体的约束: 一:namespace:相当于文件的id 二:targetNamespace属性:用来指定s ...

  2. netty实现websocket发送文本和二进制数据

    原文:https://huan1993.iteye.com/blog/2433552 最近在学习netty相关的知识,看到netty可以实现 websoket,因此记录一下在netty中实现webso ...

  3. java基础(9)---静态方法和成员方法

    一.方法:  方法的区别: 静态方法:有static方法 成员方法:没有static方法 方法的定义:  方法的调用:类.静态方法,对象.成员方法 一个MyClass类包含静态方法和成员方法: 静态方 ...

  4. Kotlin星投影与泛型约束详解

    星投影(star projection): 继续来学习Kotlin泛型相关的东东,星投影(star projection),这是个啥东东呢?下面先来说一下概念: 1.对于Star<out T&g ...

  5. postgresql —— 表的继承

    示例: CREATE TABLE cities ( --父表 name text, population float, altitude int ); CREATE TABLE capitals ( ...

  6. 图中长度为k的路径的计数

    题意 给出一个有向图,其中每条边的边长都为1.求这个图中长度恰为 $k$ 的路劲的总数.($1 \leq n \leq 100, 1 \leq k\leq 10^9$) 分析 首先,$k=1$ 时答案 ...

  7. 机器学习-- 入门demo1 k临近算法

    1.k-近邻法简介 k近邻法(k-nearest neighbor, k-NN)是1967年由Cover T和Hart P提出的一种基本分类与回归方法. 它的工作原理是:存在一个样本数据集合,也称作为 ...

  8. Jenkins 通过 maven 构建编译 JAVA 项目环境

    Jenkins 通过maven 构建编译 JAVA 项目环境 官网下载合适Jenkins版本包: 1.jenkins http://mirrors.jenkins.io/war-stable/ 2.J ...

  9. 洛谷 P1823 [COI2007] Patrik 音乐会的等待 题解

    P1823 [COI2007] Patrik 音乐会的等待 题目描述 N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人.队列中任意两个人A和B,如果他们是相 ...

  10. Luogu3774 [CTSC2017]最长上升子序列 【Young表,根号分治】

    题目链接:洛谷 推荐阅读:2019年集训队论文<浅谈杨氏矩阵在信息学竞赛中的应用> 首先我们来看一个东西,叫做Young表. 它是长一个阶梯状的东西(行长和列长都是递减的),并且每一行和每 ...