安卓學習第12課——SimpleAdapter

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   color   java   os   

SimpleAdapter一點也不簡單,他的參數就帶了5個,天哪,暈了。。

今天學的這個適配器, public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

看這個大概明白,參數分別是第一個:表示訪問整個android應用程式介面,基本上所有的組件都需要,一般都寫this(改天研究一下),第二個應該是這個List對象名,第三個是list的id,第4,5指的分別是他的list裡面的Map存的內容,String類型和其他類型(他的id)。

<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" ><ListView     android:id="@+id/mylist"    android:layout_width="match_parent"    android:layout_height="wrap_content"    /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="wrap_content"><!-- 定義一個ImageView,用於作為清單項目的一部分。 --><ImageView android:id="@+id/header"    android:layout_width="wrap_content"    android:layout_height="wrap_content"     android:paddingLeft="10dp"    /><LinearLayout    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="wrap_content"><!-- 定義一個TextView,用於作為清單項目的一部分。 --><TextView android:id="@+id/name"    android:layout_width="wrap_content"     android:layout_height="wrap_content"    android:textSize="20dp"    android:textColor="#f0f"    android:paddingLeft="10dp"    /><!-- 定義一個TextView,用於作為清單項目的一部分。 --><TextView android:id="@+id/desc"    android:layout_width="wrap_content"     android:layout_height="wrap_content"    android:textSize="14dp"    android:paddingLeft="10dp"    /></LinearLayout></LinearLayout>
package com.example.simpleadapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends Activity {//在xml中建立一個listview,用於顯示simpleAdapter的清單項目    private String[] names=new String[]{"虎頭","弄玉","李清照","李白"};    private String[] descs=new String[]{"可愛的小孩","一個擅長音樂的女孩","一個擅長文學的女性","浪漫主義詩人"};private int[] imagesIds=new int[]{R.drawable.tiger,R.drawable.nongyu,R.drawable.qingzhao,R.drawable.libai};        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //建立一個List集合,集合元素師Map        List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();        for(int i=0;i<names.length;i++){            Map<String,Object> listItem=new HashMap<String,Object>();            listItem.put("header", imagesIds[i]);            listItem.put("personname", names[i]);            listItem.put("desc", descs[i]);            listItems.add(listItem);        }        //建立一個SimpleAdapter        SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItems,R.layout.simple_item,                new String[]{"personname","header","desc"},new int[]{R.id.name,R.id.header,R.id.desc});        ListView list=(ListView) findViewById(R.id.mylist);        list.setAdapter(simpleAdapter);    }}

我認為這個代碼實現的過程是:建立一個listview,實現simpleAdapter清單項目,在建立的simpleAdapter裡面寫下清單項目內容。

備忘:這個學習了SimpleAdapter,list,HashMap.

聯繫我們

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