AdapterView及其子类之四:基于ListView及SimpleAdapter实现列表
生活随笔
收集整理的這篇文章主要介紹了
AdapterView及其子类之四:基于ListView及SimpleAdapter实现列表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼請見SimpleAdapterDemo.zip。
步驟如下:
1、創建主布局文件
<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=".MainActivity" ><ListView android:id="@+id/listview"android:layout_height="wrap_content"android:layout_width="match_parent"android:divider="#00ff00"android:dividerHeight="2dp"android:headerDividersEnabled="false"/></RelativeLayout> 2、創建每個列表選項的視圖 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/ll_listview"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><ImageView android:id="@+id/iv_header"android:layout_width="0dp"android:layout_height="40dp"android:layout_weight="1"android:contentDescription="@string/header"/><LinearLayoutandroid:id="@+id/ll_title_content"android:layout_width="0dp"android:layout_height="40dp"android:orientation="vertical" android:layout_weight="4"><TextViewandroid:id="@+id/title"android:layout_width="match_parent"android:layout_height="20dp"android:background="#000000" /><TextViewandroid:id="@+id/content"android:layout_width="match_parent"android:layout_height="20dp"android:background="#00ff00" /></LinearLayout></LinearLayout>3、創建主類 package com.ljh.listviewdemo;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import android.app.Activity;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// (1)創建要顯示的文本內容String[] arr = { "header", "title", "content" };// (2)與使用ListActivity的最大區別:使用findViewById得到一個ListViewListView lv = (ListView) findViewById(R.id.listview);// (3)創建ArrayAdapter,其中第二個參數resource:The resource ID for a layout file// containing a TextView to use when instantiating views.是要以一個layout作為// 參數,且此layout需要包含textview。/** ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,* R.layout.list, arr);*/String[] titles = { "title1", "title2", "title3", "title4" };String[] contents = { "content1", "content2", "content3", "content4" };int[] headers = { R.drawable.p1, R.drawable.p2, R.drawable.p3,R.drawable.p4 };List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();/** Parameters: * context The context where the View associated with this* SimpleAdapter is running data A List of Maps. Each entry in the List* corresponds to one row in the list. The Maps contain the data for* each row, and should include all the entries specified in "from"* resource Resource identifier of a view layout that defines the views* for this list item. The layout file should include at least those* named views defined in "to" from A list of column names that will be* added to the Map associated with each item. to The views that should* display column in the "from" parameter. These should all be* TextViews. The first N views in this list are given the values of the* first N columns in the from parameter.*/SimpleAdapter adapter = new SimpleAdapter(this, datas,R.layout.list, arr, new int[] { R.id.iv_header, R.id.title,R.id.content });for (int i = 0; i < titles.length; i++) {Map<String, Object> map = new HashMap<String, Object>();map.put("title", titles[i]);map.put("content", contents[i]);map.put("header", headers[i]);datas.add(map);}// (4)為ListActivity設置adapter.lv.setAdapter(adapter);lv.setOnItemClickListener(new OnItemClickListener() {// 定義當某個選項被點擊時的操作。@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Toast.makeText(MainActivity.this,position + " item is clicked.", Toast.LENGTH_LONG).show();}});}}
1、使用SimpleAdapter與ArrayAdapter相比,它可以支持復雜的表項結構,而不只是單一的textview。
2、其創建過程的兩個主要區別是:
(1)每個列表項的布局文件不一樣
(2)創建SimpleAdapter的操作更為復雜,需要構建一個List<Map<String, Object>>。
轉載于:https://www.cnblogs.com/eaglegeek/p/4557954.html
總結
以上是生活随笔為你收集整理的AdapterView及其子类之四:基于ListView及SimpleAdapter实现列表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript笔记 基础知识总结2
- 下一篇: 1049-飞机最少换乘次数问题