zoukankan      html  css  js  c++  java
  • 20210202 ArrayAdapter(数组适配器)

    一、ArrayAdapter(数组适配器),这个适配器使用有一定的局限性,只能显示一行文本数据

    (1)基本使用实例

    布局文件:

    复制代码
    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ListView
            android:id="@+id/ll1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>
    </LinearLayout>
    复制代码
    复制代码

    Java文件

    复制代码
    复制代码
    package com.example.test3;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    public class MainActivity extends Activity{
    //    定义要显示的数据
        private String[] datas = {"张三","李四","王五","麻子","小强"};
        private ArrayAdapter<String> adapter;
        private ListView listView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = (ListView) findViewById(R.id.ll1);
    //        初始化适配器
            adapter = new ArrayAdapter<>(this,android.R.layout.simple_expandable_list_item_1,datas);
            listView.setAdapter(adapter);
        }
    }
    复制代码
    复制代码

    效果图:

    (2)实现上面还可以先在resvalue下创建一个数组资源的xml文件:arrays.xml,然后在listview中使用entries

  • 相关阅读:
    openjudge-NOI 2.6-1996 登山
    openjudge-NOI 2.6-1944 吃糖果
    openjudge-NOI 2.6-1808 公共子序列
    openjudge-NOI 2.6-1775 采药
    openjudge-NOI 2.6-1768 最大子矩阵
    openjudge-NOI 2.6-1759 最长上升子序列
    tyvj P1050 最长公共子序列
    动态规划-最长公共子序列
    动态规划-最长上升子序列
    HDU 1811 Rank of Tetris
  • 原文地址:https://www.cnblogs.com/huangmouren233/p/14912710.html
Copyright © 2011-2022 走看看