前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ListView和Adapter(文字列表)

ListView和Adapter(文字列表)

作者头像
李小白是一只喵
发布2020-04-24 08:42:31
6830
发布2020-04-24 08:42:31
举报
文章被收录于专栏:算法微时光

image.png

目录

Adapter(适配器)

Adapter是用来帮助填出数据的中间桥梁,简单点说吧:将各种数据以合适的形式显示在View中给用户看。 Adapter有很多的接口、抽象类、子类可以使用。

ArrayAdapter的参数说明:

参数

描述

第一个参数

-context上下文对象

第二个参数

-每一个item的样式,可以使用系统提供,也可以自定义就是一个TextView

第三个参数

-数据源,要显示的数据

ListView

ListView,列表视图,直接继承了AbsListView,是一个以垂直方式在项目中显示View视图的列表。

ListView的数据项,来自一个继承了ListAdapter接口的适配器。

下面开始实战使用.O(∩_∩)O

使用ListView和Adapter

MainActivity.java文件:

代码语言:javascript
复制
package com.example.user.imagetest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获取资源
        ListView list1 = (ListView)findViewById(R.id.list_view);
        // 构建Adapter
        String[] arrayName = {"天下","第一","神剑","无双","剑姬"};
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.array_item, arrayName);
        // 为listview设置适配器
        list1.setAdapter(adapter1);
    }
}

然后需要在资源文件界面中加入ListView. 主要的资源文件activity_main.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="8dp" />

</android.support.constraint.ConstraintLayout>

另外,还需要添加适配器需要的资源文件. 构建adapter的资源文件array_item.xml:

代码语言:javascript
复制
<?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="match_parent"
    android:id="@+id/textview"
    android:textSize="24dp"
    android:textColor="#985647"
    android:shadowColor="#f0f"
    android:shadowDx="4"
    android:shadowDy="4">

</TextView>

运行效果

image.png

参考

android中Adapter适配器的讲解 Android--UI之ListView

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • Adapter(适配器)
  • ListView
  • 使用ListView和Adapter
  • 运行效果
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档