利用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. UniChat-软件工程小组-第一次作业-选题

    软件工程小组项目文档 小组成员:赵有为.张天善.宋春雨.郭凯璐.孙楠.冯韵瑶 Uni-Chat项目文档 需求分析Need ​ 日常生活中我们在使用Ubuntu等系统时都会因为QQ等聊天工具对基于Lin ...

  2. java加密算法-SHA1

    public class SHAUtil { /*** * SHA加密 生成40位SHA码 * @param 待加密字符串 * @return 返回40位SHA码 */ public static S ...

  3. Python练习题---判断回文数

    设n是一个任意自然数,如果n的各位数字反向排序所得的自然数与n相等,则n被称为回文数,从键盘输入一个5位数字 ,请编写程序判断这个数字是不是回文数. 思路:先获取一个字符串,再判断该字符串是否满足是自 ...

  4. 《The One!团队》第八次作业:ALPHA冲刺(三)

    项目 内容 作业所属课程 所属课程 作业要求 作业要求 团队名称 < The One !> 作业学习目标 (1)掌握软件测试基础技术.(2)学习迭代式增量软件开发过程(Scrum) 第三天 ...

  5. [Kubernetes] Kubectl and Pod

    1. Create and run a Pod kubectl run my-nginx --image=nginx:alpine We can run kubectl get all to see ...

  6. Centos7 minimal 安装npm

    最小版本缺少很多源,需要手动去添加源 如何去判断yum中 有没有 npm 的源呢 yum list | grep npm 如果是这样的,就代表需要自己去添加 curl -sL -o /etc/yum. ...

  7. 洛谷 P1279 字串距离 题解

    每日一题 day24 打卡 Analysis 字符串+dp 仔细观察发现,对于f[i][j],它的值为以下三个值中的最小者: f[i-1][j]+k //a[i]对应空格 f[i][j-1]+k // ...

  8. YAML_11 when条件判断

    当系统负载超过0.7时,则关掉httpd ansible]# vim when.yml --- - hosts: cache   remote_user: root   tasks:     - sh ...

  9. 使用echarts生成海友网企业全国分布地图

    不分类别的效果 不同分类的分布效果图 从海友网获取各个企业名单保存进mysql cmfishhelper.py 从下列网址得到各个企业名片的网址保存进表cmfish cds = get_cds() h ...

  10. Notepad++ 编译运行java,c,c++

    1.Java NPP_SAVE cd $(CURRENT_DIRECTORY) D:\tibco\bw6\tibcojre64\1.8.0\bin\javac.exe "$(FILE_NAM ...