|
http://blog.csdn.net/x605940745/article/details/11981049
SimpleAdapter的參數(shù)說明 第一個參數(shù) 表示訪問整個android應用程序接口,基本上所有的組件都需要 第二個參數(shù)表示生成一個Map(String ,Object)列表選項 第三個參數(shù)表示界面布局的id 表示該文件作為列表項的組件 第四個參數(shù)表示該Map對象的哪些key對應value來生成列表項 第五個參數(shù)表示來填充的組件 Map對象key對應的資源一依次填充組件 順序有對應關系 注意的是map對象可以key可以找不到 但組件的必須要有資源填充 因為 找不到key也會返回null 其實就相當于給了一個null資源 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head}這個head的組件會被name資源覆蓋
代碼
- <LinearLayout 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:orientation="horizontal"
- tools:context=".MainActivity" >
-
- <ListView
- android:id="@+id/lt1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- </ListView>
-
- </LinearLayout>
- <?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="match_parent"
- android:orientation="horizontal" >
-
- <ImageView
- android:id="@+id/head"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingLeft="10dp" />
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
-
- <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
- 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.simpleadptertest;
-
- 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.view.Menu;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
-
- public class MainActivity extends Activity {
-
- private String[] name = { "劍蕭舞蝶", "張三", "hello", "詩情畫意" };
-
- private String[] desc = { "魔域玩家", "百家執(zhí)行", "高級的富一代", "妹子請過來..一個善于跑妹子的。。" };
-
- private int[] imageids = { R.drawable.libai, R.drawable.nongyu,
- R.drawable.qingzhao, R.drawable.tiger };
-
- private ListView lt1;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- List<Map<String, Object>> listems = new ArrayList<Map<String, Object>>();
- for (int i = 0; i < name.length; i++) {
- Map<String, Object> listem = new HashMap<String, Object>();
- listem.put("head", imageids[i]);
- listem.put("name", name[i]);
- listem.put("desc", desc[i]);
- listems.add(listem);
- }
-
-
-
-
-
-
-
-
-
-
-
- SimpleAdapter simplead = new SimpleAdapter(this, listems,
- R.layout.simple_item, new String[] { "name", "head", "desc" },
- new int[] {R.id.name,R.id.head,R.id.desc});
-
- lt1=(ListView)findViewById(R.id.lt1);
- lt1.setAdapter(simplead);
-
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- }

|