風人園

          弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
          隨筆 - 99, 文章 - 181, 評論 - 56, 引用 - 0

          導航

          <2016年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(11)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          收藏夾

          友情鏈接

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          Android之SimpleAdapter簡單實例和SimpleAdapter參數說明(zt)

          http://blog.csdn.net/x605940745/article/details/11981049

          SimpleAdapter的參數說明
           第一個參數 表示訪問整個android應用程序接口,基本上所有的組件都需要
           第二個參數表示生成一個Map(String ,Object)列表選項
           第三個參數表示界面布局的id  表示該文件作為列表項的組件
           第四個參數表示該Map對象的哪些key對應value來生成列表項
           第五個參數表示來填充的組件 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資源覆蓋


          代碼

          [html] view plain copy
           在CODE上查看代碼片派生到我的代碼片
          1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
          2.     xmlns:tools="http://schemas.android.com/tools"  
          3.     android:layout_width="match_parent"  
          4.     android:layout_height="match_parent"  
          5.     android:orientation="horizontal"  
          6.     tools:context=".MainActivity" >  
          7.   
          8.     <ListView  
          9.         android:id="@+id/lt1"  
          10.         android:layout_width="match_parent"  
          11.         android:layout_height="wrap_content" >  
          12.     </ListView>  
          13.   
          14. </LinearLayout>  

          [html] view plain copy
           在CODE上查看代碼片派生到我的代碼片
          1. <?xml version="1.0" encoding="utf-8"?>  
          2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
          3.     android:layout_width="match_parent"  
          4.     android:layout_height="match_parent"  
          5.     android:orientation="horizontal" >  
          6.   
          7.     <ImageView  
          8.         android:id="@+id/head"  
          9.         android:layout_width="wrap_content"  
          10.         android:layout_height="wrap_content"  
          11.         android:paddingLeft="10dp" />  
          12.   
          13.     <LinearLayout  
          14.         android:layout_width="match_parent"  
          15.         android:layout_height="wrap_content"  
          16.         android:orientation="vertical" >  
          17.           
          18.         <TextView   
          19.             android:id="@+id/name"  
          20.             android:layout_width="wrap_content"  
          21.             android:layout_height="wrap_content"  
          22.             android:textSize="20dp"  
          23.             android:textColor="#f0f"  
          24.             android:paddingLeft="10dp"/>  
          25.           
          26.                   
          27.         <TextView   
          28.             android:id="@+id/desc"  
          29.             android:layout_width="wrap_content"  
          30.             android:layout_height="wrap_content"  
          31.             android:textSize="14dp"  
          32.             android:paddingLeft="10dp"/>  
          33.           
          34.     </LinearLayout>  
          35.   
          36. </LinearLayout>  


          [java] view plain copy
           在CODE上查看代碼片派生到我的代碼片
          1. package com.example.simpleadptertest;  
          2.   
          3. import java.util.ArrayList;  
          4. import java.util.HashMap;  
          5. import java.util.List;  
          6. import java.util.Map;  
          7.   
          8. import android.app.Activity;  
          9. import android.os.Bundle;  
          10. import android.view.Menu;  
          11. import android.widget.ListView;  
          12. import android.widget.SimpleAdapter;  
          13.   
          14. public class MainActivity extends Activity {  
          15.   
          16.     private String[] name = { "劍蕭舞蝶""張三""hello""詩情畫意" };  
          17.   
          18.     private String[] desc = { "魔域玩家""百家執行""高級的富一代""妹子請過來..一個善于跑妹子的。。" };  
          19.   
          20.     private int[] imageids = { R.drawable.libai, R.drawable.nongyu,  
          21.             R.drawable.qingzhao, R.drawable.tiger };  
          22.       
          23.     private ListView lt1;  
          24.   
          25.     @Override  
          26.     protected void onCreate(Bundle savedInstanceState) {  
          27.         super.onCreate(savedInstanceState);  
          28.         setContentView(R.layout.activity_main);  
          29.         List<Map<String, Object>> listems = new ArrayList<Map<String, Object>>();  
          30.         for (int i = 0; i < name.length; i++) {  
          31.             Map<String, Object> listem = new HashMap<String, Object>();  
          32.             listem.put("head", imageids[i]);  
          33.             listem.put("name", name[i]);  
          34.             listem.put("desc", desc[i]);  
          35.             listems.add(listem);  
          36.         }  
          37.           
          38.         /*SimpleAdapter的參數說明 
          39.          * 第一個參數 表示訪問整個android應用程序接口,基本上所有的組件都需要 
          40.          * 第二個參數表示生成一個Map(String ,Object)列表選項 
          41.          * 第三個參數表示界面布局的id  表示該文件作為列表項的組件 
          42.          * 第四個參數表示該Map對象的哪些key對應value來生成列表項 
          43.          * 第五個參數表示來填充的組件 Map對象key對應的資源一依次填充組件 順序有對應關系 
          44.          * 注意的是map對象可以key可以找不到 但組件的必須要有資源填充  因為 找不到key也會返回null 其實就相當于給了一個null資源 
          45.          * 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head} 
          46.          * 這個head的組件會被name資源覆蓋 
          47.          * */  
          48.         SimpleAdapter simplead = new SimpleAdapter(this, listems,  
          49.                 R.layout.simple_item, new String[] { "name""head""desc" },  
          50.                 new int[] {R.id.name,R.id.head,R.id.desc});  
          51.           
          52.         lt1=(ListView)findViewById(R.id.lt1);  
          53.         lt1.setAdapter(simplead);  
          54.           
          55.     }  
          56.   
          57.     @Override  
          58.     public boolean onCreateOptionsMenu(Menu menu) {  
          59.         // Inflate the menu; this adds items to the action bar if it is present.  
          60.         getMenuInflater().inflate(R.menu.main, menu);  
          61.         return true;  
          62.     }  
          63.   
          64. }  


          posted on 2016-12-01 13:12 風人園 閱讀(186) 評論(0)  編輯  收藏 所屬分類: Android

          主站蜘蛛池模板: 康马县| 南陵县| 永新县| 长丰县| 凌海市| 宜宾市| 固原市| 丰台区| 神池县| 阿拉善左旗| 上思县| 全州县| 霍城县| 林周县| 黔江区| 平果县| 兴文县| 金寨县| 五原县| 兰坪| 正安县| 象山县| 兴城市| 玉树县| 宝应县| 江都市| 广宁县| 宜昌市| 宁都县| 额敏县| 崇仁县| 嘉义市| 密山市| 蕉岭县| 蓝田县| 普兰县| 六枝特区| 凌源市| 华安县| 东乡县| 松溪县|