Adapter系列之ArrayAdapter

Adapter系列之ArrayAdapter

ArrayAdapter:简单、易用的Adapter,通常用于将List集合的多个值包装成多个列表项。

使用ArrayAdapter创建ListView
activity_main.xml文件:

<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/list1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#f00"
        android:dividerHeight="2px"
        android:headerDividersEnabled="false"/>
    <ListView android:id="@+id/list2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#0f0"
        android:dividerHeight="2px"
        android:headerDividersEnabled="false"/>

</LinearLayout>

item01文件:

<?xml version="1.0" encoding="utf-8"?>
 <TextView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24dp"
        android:padding="10px"
        android:textColor="#00f"/>

item02文件:

<?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" >

     <TextView 
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24dp"
        android:padding="10px"
        android:textColor="#00f"/>

</LinearLayout>

MainActivity文件

package com.example.arrayadaptertest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView list01 = (ListView) findViewById(R.id.list1);
        String[] arr1 = {
                "我欲乘风来",
                "除魔天地间",
                "有酒我逍遥"
                };
        ArrayAdapter<String>adapter01 = new ArrayAdapter<String>(this, R.layout.item01, arr1);
        list01.setAdapter(adapter01);


        ListView list02 = (ListView) findViewById(R.id.list2);
        String[] arr2 = {
                "Java",
                "C++",
                "JavaScript"
        };
        ArrayAdapter<String>adapter02 = new ArrayAdapter<String>(this,
                R.layout.item02, R.id.tv2, arr2);
        list02.setAdapter(adapter02);

    }

}

截图:
这里写图片描述

代码太简单,这里就不上传源码了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值