ListView+ArrayAdapter

时间:2020-05-14
本文章向大家介绍ListView+ArrayAdapter,主要包括ListView+ArrayAdapter使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

 可以显示文本信息

ListView 的使用方法可以概括为:

1、构造数据

2、获取Adapter

3、Adapter绑定ListView

        ListView lv_main = findViewById(R.id.lv_main);
        String [] data = new String[100];
        for(int i=0;i<data.length;i++){
            data[i]=String.valueOf(i);
        }
        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this,R.layout.item_array_adapter,data);
        lv_main.setAdapter(stringArrayAdapter);
View Code

这里我们要给定两个布局文件,一个是包含ListView的主布局文件

<?xml version="1.0" encoding="utf-8"?>
<ListView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/lv_main" xmlns:android="http://schemas.android.com/apk/res/android">
</ListView>
activity_main.xml

另一个是显示文本的布局文件

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
         android:layout_height="50dp"
        android:textSize="20sp"/>
item_array_adapter.xml

原文地址:https://www.cnblogs.com/superxuezhazha/p/12890493.html