最常用和最难用的控件 -- ListView
先实现一个简单的ListView,
第一步:
随便搞一个ListView的Layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
接着,敲代码:
package com.example.listviewapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends AppCompatActivity { private String[] data = {"Apple","Banana","Grape","Coco"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data); ListView listView = (ListView)findViewById(R.id.list_view); listView.setAdapter(adapter); } }
点击查看更多内容
以上内容来自于网络,如有侵权联系即删除

相关文章
- 构建内网安全的威胁与方法
- nodejs按行读取文件和写入文件的demo
- 一,输入一个整数算阶乘!
- 腾讯云容器服务日志采集最佳实践
- 【Python 1-0】10个学习Python的理由以及Python的优势有哪些?
- 自动化测试十大必备(背)面试题!
- 一篇文章带你了解HTML表格及其主要属性介绍
- 用华为HMS ML kit人体骨骼识别技术,Android快速实现人体姿势动作抓拍
上一篇: 客户端安全相关知识点