android之ArrayAdaper應用

來源:互聯網
上載者:User

標籤:android   layout   

listView頁面配置:layout/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.example.day_05_022.MainActivity" >


    <ListView 
        android:id="@+id/lvMonths"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#ccc"
        android:dividerHeight="6dp"/>


</RelativeLayout>

該布局下的java代碼:

package com.example.day_05_022;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity {
private ListView lvMonths;//listView對象
private BaseAdapter arrayAdapter;//適配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}


private void init() {
// TODO Auto-generated method stub
//初始化listView對象
lvMonths = (ListView) findViewById(R.id.lvMonths);
//初始化ArrayAdaper對象

/重點解釋第一個參數是要顯示的布局所在的activity對象,第二個參數是指要顯示的集合資料的物理地址,第三個參數是

                // 指適配的布局頁面,這裡直接用了android內建的布局

arrayAdapter = ArrayAdapter.createFromResource(this,
R.array.day_of_week,
android.R.layout.simple_expandable_list_item_1);
//設定適配器
lvMonths.setAdapter(arrayAdapter);
}



}

String字串資源:values/strings.xml

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


    <string name="app_name">Day_05_022</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
<string-array name="day_of_week">
   <item >Sunday</item>
   <item>Monday</item>
   <item>Tuesday</item>
   <item>Wednesday</item>
   <item>Thursday</item>
   <item>Friday</item>
   <item>Saturday</item>
   
</string-array>
</resources>

效果:

650) this.width=650;" src="http://img.blog.csdn.net/20141130102436889" />

本文出自 “追隨心的彼岸” 部落格,謝絕轉載!

android之ArrayAdaper應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.