風(fēng)人園

          弱水三千,只取一瓢,便能解渴;佛法無(wú)邊,奉行一法,便能得益。
          隨筆 - 99, 文章 - 181, 評(píng)論 - 56, 引用 - 0
          數(shù)據(jù)加載中……

          Android之SimpleAdapter簡(jiǎn)單實(shí)例和SimpleAdapter參數(shù)說(shuō)明(zt)

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

          SimpleAdapter的參數(shù)說(shuō)明
           第一個(gè)參數(shù) 表示訪問(wèn)整個(gè)android應(yīng)用程序接口,基本上所有的組件都需要
           第二個(gè)參數(shù)表示生成一個(gè)Map(String ,Object)列表選項(xiàng)
           第三個(gè)參數(shù)表示界面布局的id  表示該文件作為列表項(xiàng)的組件
           第四個(gè)參數(shù)表示該Map對(duì)象的哪些key對(duì)應(yīng)value來(lái)生成列表項(xiàng)
           第五個(gè)參數(shù)表示來(lái)填充的組件 Map對(duì)象key對(duì)應(yīng)的資源一依次填充組件 順序有對(duì)應(yīng)關(guān)系
           注意的是map對(duì)象可以key可以找不到 但組件的必須要有資源填充  因?yàn)?找不到key也會(huì)返回null 其實(shí)就相當(dāng)于給了一個(gè)null資源
           下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head}
          這個(gè)head的組件會(huì)被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""詩(shī)情畫意" };  
          17.   
          18.     private String[] desc = { "魔域玩家""百家執(zhí)行""高級(jí)的富一代""妹子請(qǐng)過(guò)來(lái)..一個(gè)善于跑妹子的。。" };  
          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的參數(shù)說(shuō)明 
          39.          * 第一個(gè)參數(shù) 表示訪問(wèn)整個(gè)android應(yīng)用程序接口,基本上所有的組件都需要 
          40.          * 第二個(gè)參數(shù)表示生成一個(gè)Map(String ,Object)列表選項(xiàng) 
          41.          * 第三個(gè)參數(shù)表示界面布局的id  表示該文件作為列表項(xiàng)的組件 
          42.          * 第四個(gè)參數(shù)表示該Map對(duì)象的哪些key對(duì)應(yīng)value來(lái)生成列表項(xiàng) 
          43.          * 第五個(gè)參數(shù)表示來(lái)填充的組件 Map對(duì)象key對(duì)應(yīng)的資源一依次填充組件 順序有對(duì)應(yīng)關(guān)系 
          44.          * 注意的是map對(duì)象可以key可以找不到 但組件的必須要有資源填充  因?yàn)?nbsp;找不到key也會(huì)返回null 其實(shí)就相當(dāng)于給了一個(gè)null資源 
          45.          * 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head} 
          46.          * 這個(gè)head的組件會(huì)被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 風(fēng)人園 閱讀(185) 評(píng)論(0)  編輯  收藏 所屬分類: Android

          主站蜘蛛池模板: 平和县| 板桥市| 长治市| 建德市| 图们市| 扬中市| 镇坪县| 罗山县| 凤山县| 外汇| 莆田市| 乳山市| 山丹县| 大安市| 阿鲁科尔沁旗| 曲阳县| 呼和浩特市| 通海县| 芒康县| 安顺市| 怀化市| 石狮市| 沙湾县| 开鲁县| 旬阳县| 广昌县| 九寨沟县| 昂仁县| 蒙阴县| 蕲春县| 德清县| 新兴县| 抚顺市| 大悟县| 美姑县| 南澳县| 城口县| 英超| 大厂| 任丘市| 宁晋县|