android開發之ArrayAdapter的使用

來源:互聯網
上載者:User

標籤:

xml檔案,一個是顯示布局,一個是用來顯示每一個清單項目的。

main.xml

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

array_item.xml

<?xml version="1.0" encoding="utf-8"?><TextView    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/TextView"    android:layout_width="match_parent"    android:layout_height="wrap_content"    />

顯示列表的代碼如下:

setContentView(R.layout.main);//執行個體化清單項目ListView list1 = (ListView) findViewById(R.id.list1);// 定義一個數組String[] arr1 = { "孫悟空", "豬八戒", "牛魔王" };// 將數組封裝ArrayAdapter/* * ArrayAdapter<String>(Context context, int textViewResourceId, String[] objects) * context: * textViewResourceId:TextView的布局檔案 * objects:需要傳入的數組 */ArrayAdapter<String> adapter1 = new ArrayAdapter<String>    (this, R.layout.array_item, arr1);// 為ListView設定Adapterlist1.setAdapter(adapter1);

總結起來,使用ArrayAdapter來實現ListView需要以下幾個元素

1.ListView需要顯示的數組

2.顯示每一項的TextView的布局檔案

3.利用資料和布局產生對應的適配器,即ArrayAdapter

4.為執行個體化的ListView設定相應的適配器。

android開發之ArrayAdapter的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.