利用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. 宁波市第二届CTF之cripto1

    密码学第一题 给的是一个shadow文件,16进制编辑器打开 是一串Linux root用户密码的密文,猜测密码可能就是flag,于是将这一串字符放到文本. linux下爆破用户密码的工具没接触过(还 ...

  2. Redis与Python的交互

    驱动模块 redis模块用来在Python环境下驱动Redis数据库 可以直接用pip方式安装 pip install redis 或者国内镜像下载: pip install -i https://p ...

  3. Chocolatey——windows下的包管理器

    前言 windows 包管理器 | https://chocolatey.org/ 命令 文档 | https://chocolatey.org/docs 根据使用会补充命令

  4. Django --- cookie与session,中间件

    目录 1.cookie与session 1.cookie 2.session 2.中间件 1.中间件作用 2.用户可以自定义的五个方法 3.自定义中间件 1.cookie与session 1.cook ...

  5. bzoj 2480——扩展BSGS

    题意 给定 $a,b$ 和模数 $p$,求整数 $x$ 满足 $a^x \equiv  b(mod \ p)$,不保证 $a,p$ 互质. (好像是权限题,可见洛谷P4195 分析 之前讲过,可以通过 ...

  6. python - django (request 获取 访问者的 IP)

    使用 Django 获取访问者的 IP if request.META.get('HTTP_X_FORWARDED_FOR'): ip = request.META.get("HTTP_X_ ...

  7. boost.tribool

    tribool boost.tribool类似c++内建的bool类,但基于三态的布尔逻辑,在true和false之外还有一个indeterminate状态.一个例子场景是执行某项任务,在执行之前状态 ...

  8. @RestController和@GetMapping

    @RestController 可以代替@Controller使用,使用了@RestController的控制器默认所有请求方法都用了@ResponseBody注解. @GetMapping(&quo ...

  9. P1016 旅行家的预算——贪心

    P1016 旅行家的预算 贪心求,在当前点如果能到达距离最近的油价比他小的就直接去油价比他小的, 如果在可行范围内没有比他油价小的,就加满开到可行范围内油价最小的点: 这么做是对的,我不会证明: 还有 ...

  10. rsync 使用ssh协议免密

    rsync远程传输避免密码输入 每次rsync远程传输时都需要输入用户在远程机器上的密码,这样导致无法在后台自动运行rsync,可采用秘钥文件来替代人工输入密码的方式来解决. 第一步 在本地机器上使用 ...