天天看點

06_Android中ArrayAdapter的使用



1 目标界面

06_Android中ArrayAdapter的使用

2 編寫androidmanifest.xml檔案

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.itheima28.arrayadapterdemo"

    android:versioncode="1"

    android:versionname="1.0" >

    <uses-sdk

        android:minsdkversion="8"

        android:targetsdkversion="19" />

    <application

        android:allowbackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/apptheme" >

        <activity

            android:name="com.itheima28.arrayadapterdemo.mainactivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.main" />

                <category android:name="android.intent.category.launcher" />

            </intent-filter>

        </activity>

    </application>

</manifest>

3 編寫布局檔案activity_main.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingbottom="@dimen/activity_vertical_margin"

    android:paddingleft="@dimen/activity_horizontal_margin"

    android:paddingright="@dimen/activity_horizontal_margin"

    android:paddingtop="@dimen/activity_vertical_margin"

    tools:context="com.itheima28.arrayadapterdemo.mainactivity$placeholderfragment" >

    <listview

        android:id="@+id/listview"

        android:layout_width="match_parent"

        android:layout_height="match_parent"/>

</relativelayout>

4 編寫代碼

package com.itheima28.arrayadapterdemo;

import android.app.activity;

import android.os.bundle;

import android.widget.arrayadapter;

import android.widget.listview;

/**

 * arrayadapter的使用

 * @author toto

 *

 */

public class mainactivity extends activity {

    @override

    protected void oncreate(bundle savedinstancestate) {

       super.oncreate(savedinstancestate);

       setcontentview(r.layout.activity_main);

       listview mlistview = (listview) findviewbyid(r.id.listview);

       string[] textarray = {"功能1","功能2","功能3","功能4","功能5","功能6","功能7","功能8"};

       /**

        * 定義資料擴充卡

        */

       arrayadapter<string> adapter = new arrayadapter<string>(

              this,

              android.r.layout.simple_list_item_1,

              textarray);

       mlistview.setadapter(adapter);

    }

}

繼續閱讀