Android簡易實戰教程--第十八話《ListView顯示,簡單的適配器SimpleAdapter》

來源:互聯網
上載者:User

標籤:

本篇介紹Listview的顯示,對於listview有許多的適配器,如ArrayAdapter,BaseAdapter,SimpleAdapter等等。本篇先熱身一下,介紹最簡單的SimpleAdapter適配器。

對於安卓介面的顯示。

首先在主介面布局檔案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"    tools:context=".MainActivity" >    <ListView        android:id="@+id/lv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        /></RelativeLayout>

只有一個顯示資料的組件:ListView。

然後,給ListView的Item定義一個子布局檔案。它代表,listview的列表每個條目的布局item_listview.xml。如下:

<?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="wrap_content"    android:orientation="horizontal" >        <ImageView         android:id="@+id/iv_photo"        android:layout_width="40dp"        android:layout_height="40dp"        android:src="@drawable/photo3"        />    <TextView         android:id="@+id/tv_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="22sp"        android:layout_gravity="center_vertical"        /></LinearLayout>
好了,現在就在MainActivity中加入資料顯示的代碼吧:

package com.itydl.arraysimple;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//擷取listview對象ListView lv = (ListView) findViewById(R.id.lv);//集合中每個元素都包含ListView條目需要的所有資料,該案例中每個條目需要一個字串和一個整型,所以使用一個map來封裝這兩種資料List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();//定義三條map資訊Map<String, Object> map1 = new HashMap<String, Object>();map1.put("photo", R.drawable.photo1);map1.put("name", "小志的兒子");data.add(map1);Map<String, Object> map2 = new HashMap<String, Object>();map2.put("photo", R.drawable.photo2);map2.put("name", "小志");data.add(map2);Map<String, Object> map3 = new HashMap<String, Object>();map3.put("photo", R.drawable.photo3);map3.put("name", "趙帥哥");data.add(map3);lv.setAdapter(new SimpleAdapter(this, data, R.layout.item_listview, new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name}));//這裡注意from和to兩個位置要對應//就是制定鍵和值,在布局檔案中哪的子節點中顯示。不要搞錯和搞反了。}}

其中注意一點:就是new SimpleAdapter(this, data, R.layout.item_listview,
                new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name})

參數含義:上下文,資料來源,item的布局檔案id,from,to。其中from是一個數組,裡面的鍵,與map的鍵相同;to也是個數組,表示ite顯示的組件id,注意要與from的順序一致,不然會報錯。

好了,現在運行程式,結果如下:


Android簡易實戰教程--第十八話《ListView顯示,簡單的適配器SimpleAdapter》

聯繫我們

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