ArrayAdapter和ListView
利用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的更多相关文章
- BaseAdapter&ArrayAdapter在ListView中应用
一:BaseAdapter:共同实现的基类的适配器,是ArrayAdapter SimpleAdapter等的父类, 一般用于比较复杂的ListView,扩展性强. 详细信息可查看谷歌官方API:ht ...
- 使用定制的ArrayAdapter制作ListView的Items(翻译)
Translated by:AcerWang 原文出自:customizing-android-listview-items-with-custom-arrayadapter 背景介绍 对于现 ...
- 第二章实例:ArrayAdapter结合ListView列表视图
package mydefault.packge; import com.example.codeview.R; import android.app.Activity; import android ...
- Android ListView ArrayAdapter 的简单使用
前面写了3篇关于android的文章,其中的演示程序都写在了一个工程中,当时为了方便测试就在启动页MainActivity中放了3个按钮,点击不同的按钮进入不同的示例程序页面,MainActivity ...
- ListView与ArrayAdapter的搭配使用
在android中,ListView是一种很重要的控件,一般的使用中,常建立一个所需类型的ArrayList,再通过ArrayAdapter把ListView绑定到ArrayList上,通过Array ...
- ListView总结
ListView类作为在Android开发中经常会使用到的组件,作为新手,还是感到这一块变化形式还是很多的,需要慢慢学习.现在这里大概总结一下. 基于数组的ListView:使用android:ent ...
- 安卓第六天笔记--ListView
安卓第六天笔记--ListView 1.AdapteView AdapteView 继承ViewGroup它的本质是容器 AdapterView派生了3个子类: AbsListView AbsSpin ...
- ListView 搭配SimpleAdapter
这是SimplerAdapter的构造函数 public SimpleAdapter(Context context, List<? extends Map<String, ?>&g ...
- Android开发具体解释之ListView具体解释一
列表ListView介绍和实例 1.ListView -- ListActivity -- ListAdapter 2.ArrayAdapter结合ListView进行显示 3.SimpleA ...
随机推荐
- Eclips+ADT+SDK构建android开发环境及android自动化测试开发环境
一. 需要用到的包: 1.adt-bundle-windows-x86_64-20140702.zip+JDK+ant 2.ant下载地址:http://ant.apache.org/bindownl ...
- 《少年先疯队》第八次团队作业:Alpha冲刺第三天
前言 第三天冲刺会议 时间:2019.6.16 地点:宿舍 3.1 今日完成任务情况以及遇到的问题. 3.1.1今日完成任务情况 姚玉婷:酒店的会员信息的管理,如会员的添加,删除 马 ...
- c语言实现整数转换为字符串——不考虑负数
#include <stdio.h> #include <string.h> #define MAX_LEN 16 #define ESP 1e-5 typedef int i ...
- 流媒体知识 wiki
媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等.最近因为项目的关系,需要理清媒 ...
- java书籍推荐[转]
作为Java程序员来说,最痛苦的事情莫过于可以选择的范围太广,可以读的书太多,往往容易无所适从.下面就按照学习顺序,给大家推荐下面这些JAVA书籍.一.Java编程入门类,选择大于努力,入门太重要.对 ...
- GDB core命令的使用调试段错误
#include <stdio.h> void func(){ int *p = NULL; printf("*p:%d\n", *p);//断错误 } int mai ...
- Ubuntu16.4 内核降级
.cp /etc/apt/sources.list /etc/apt/sources.list.bak #备份sources.list .vi /etc/apt/sources.list #在sour ...
- Transform详解(超详细) Attention is all you need论文
一.背景 自从Attention机制在提出 之后,加入Attention的Seq2 Seq模型在各个任务上都有了提升,所以现在的seq2seq模型指的都是结合rnn和attention的模型.传统的基 ...
- C 库函数 - strcspn()
定义 size_t strcspn(const char *str1, const char *str2) 参数 str1 -- 要被检索的 C 字符串. str2 -- 该字符串包含了要在 str1 ...
- Failed opening libc!的解决方法
方法来源: http://www.cfd-online.com/Forums/fluent/135879-setting-process-affinity-failed-opening-libc.ht ...