標籤: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.