网站首页 > 博客文章 正文
制作android app 最常用的就是ListView,使用java操作的例程网上很多,对比看看使用kotlin编写时代码量的减少程度
1、array listView
代码如下
val mes ="diao#lou#test"
var strlist = mes.split("#")
var arr_aAdapter: ArrayAdapter<String>? = null
arr_aAdapter = ArrayAdapter(this, android.R.layout.simple_selectable_list_item, strlist)
listview1.setAdapter(arr_aAdapter)
2、SimpleAdapter
代码如下
var sim_aAdapter : SimpleAdapter
var dataList = ArrayList<Map<String,Any>>()
for (i in 0..29) {
var map = HashMap<String,Any>()//HashMap
map.put("pic0",R.mipmap.ic_ssball)
map.put("text0", "list message"+i)
dataList.add(map)
}
sim_aAdapter = SimpleAdapter(this, dataList, R.layout.item1, arrayOf("pic0", "text0"), intArrayOf(R.id.pic0, R.id.text0))
listview1.setAdapter(sim_aAdapter)
3、CustomListAdapter
代码如下
类定义
inner class CustomListAdapter(private val context: Activity, private val itemname: Array<String>, private val imgid: Array<Int>)
: ArrayAdapter<String>(context, R.layout.item, itemname) {
override fun getView(position: Int, view: View?, parent: ViewGroup): View {
val inflater = context.layoutInflater
val rowView = inflater.inflate(R.layout.item, null, true)
val txtTitle = rowView.findViewById(R.id.tv) as TextView
val imageView = rowView.findViewById(R.id.img) as ImageView
val extratxt = rowView.findViewById(R.id.info) as TextView
txtTitle.text = itemname[position]
imageView.setImageResource(imgid[position])
extratxt.text = "Description " + itemname[position]
return rowView
}
}
使用代码
var itemname = arrayOf("Safari", "Camera")
var imgid = arrayOf<Int>(R.mipmap.ic_ssball, R.mipmap.ic_title)
val adapter = CustomListAdapter(this, itemname, imgid)
listview1.setAdapter(adapter)
这里省去xml布局文件代码,布局文件的代码量没有变化。再看看响应消息代码
listview1.setOnItemClickListener(OnItemClickListener { parent, view, position, id ->
val Slecteditem = itemname[+position]
Toast.makeText(applicationContext, Slecteditem, Toast.LENGTH_SHORT).show()
})
kotlin带来了代码量的减少,也提供了java代码自动转换成kotlin的方法,但语法的不熟悉使得处理拷贝来的java代码资源还是有些费时费力,你会开始尝试kotlin吗?
盛铭逸品,精致生活从选择开始
猜你喜欢
- 2024-09-10 全新版Jetpack进阶提升,系统性落地短视频App(完结)
- 2024-09-10 Android开发新选择:Kotlin(kotlin android开发教程)
- 2024-09-10 高兼容低成本,开箱即用的首页性能优化方式被我们找到了
- 2024-09-10 使用Kotlin实战一个BaseActivity并制作一个登录页
- 2024-09-10 Kotlin 1.2 Beta 发布,来看看新特性!
- 2024-09-10 Android自定义控件(高手级)——JOJO同款能力分析图
- 2024-09-10 Spring Boot 自定义Jackson ObjectMapper
- 2024-09-10 使用Kotlin实战一个Android架构中的MVP模式,简单实用
- 2024-09-10 使用kotlin实现沉浸式状态栏(kotlin ui框架)
- 2024-09-10 用Kotlin开发了一个Android应用,我只用了8小时
你 发表评论:
欢迎- 06-23MySQL合集-mysql5.7及mysql8的一些特性
- 06-23MySQL CREATE TABLE 简单设计模板交流
- 06-23MYSQL表设计规范(mysql设计表注意事项)
- 06-23MySQL数据库入门(四)数据类型简介
- 06-23数据丢失?别慌!MySQL备份恢复攻略
- 06-23MySQL设计规范(mysql 设计)
- 06-23MySQL数据实时增量同步到Elasticsearch
- 06-23MySQL 避坑指南之隐式数据类型转换
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)