最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

舉例講解Android應(yīng)用中SimpleAdapter簡單適配器的使用

 更新時(shí)間:2016年04月24日 10:21:14   作者:劍蕭舞蝶  
這篇文章主要介紹了Android應(yīng)用中SimpleAdapter簡單適配器的使用例子,SimpleAdapter經(jīng)常在ListView被使用,需要的朋友可以參考下

SimpleAdapter,跟名字一樣,一個簡單的適配器,既為簡單,就只是被設(shè)計(jì)來做簡單的應(yīng)用的,比如靜態(tài)數(shù)據(jù)的綁定,不過仍然有自定義的空間,比如說在每一個ListItem中加一個按鈕并添加響應(yīng)事件.首先還是先看一下SimpleAdapter的定義吧,直接翻譯下SDK doc 吧:

這是一個簡單的適配器,可以將靜態(tài)數(shù)據(jù)映射到XML文件中定義好的視圖。你可以指定由Map組成的List(比如ArrayList)類型的數(shù)據(jù)。在ArrayList中的每個條目對應(yīng)List中的一行。Maps包含每一行的數(shù)據(jù)。你可以指定一個XML布局以指定每一行的視圖,根據(jù)Map中的數(shù)據(jù)映射關(guān)鍵字到指定的視圖。綁定數(shù)據(jù)到視圖分兩個階段,首先,如果設(shè)置了SimpleAdapter.ViewBinder,那么這個設(shè)置的ViewBinder的setViewValue(android.view.View, Object, String)將被調(diào)用。如果setViewValue的返回值是true,則表示綁定已經(jīng)完成,將不再調(diào)用系統(tǒng)默認(rèn)的綁定實(shí)現(xiàn)。如果返回值為false,視圖將按以下順序綁定數(shù)據(jù):
如果View實(shí)現(xiàn)了Checkable(例如CheckBox),期望綁定值是一個布爾類型。
TextView.期望綁定值是一個字符串類型,通過調(diào)用setViewText(TextView, String)綁定。
ImageView,期望綁定值是一個資源id或者一個字符串,通過調(diào)用setViewImage(ImageView, int) 或 setViewImage(ImageView, String)綁定數(shù)據(jù)。
如果沒有一個合適的綁定發(fā)生將會拋出IllegalStateException。

先看一下構(gòu)造函數(shù): 

復(fù)制代碼 代碼如下:

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

SimpleAdapter基本上認(rèn)知了其參數(shù)含義 用起來就簡單多了。

SimpleAdapter的參數(shù)說明:

  •  第一個參數(shù) 表示訪問整個android應(yīng)用程序接口,基本上所有的組件都需要
  •  第二個參數(shù)表示生成一個Map(String ,Object)列表選項(xiàng)
  •  第三個參數(shù)表示界面布局的id  表示該文件作為列表項(xiàng)的組件
  •  第四個參數(shù)表示該Map對象的哪些key對應(yīng)value來生成列表項(xiàng)
  •  第五個參數(shù)表示來填充的組件 Map對象key對應(yīng)的資源一依次填充組件 順序有對應(yīng)關(guān)系
  •  注意的是map對象可以key可以找不到 但組件的必須要有資源填充  因?yàn)?找不到key也會返回null 其實(shí)就相當(dāng)于給了一個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的參數(shù)說明 
     * 第一個參數(shù) 表示訪問整個android應(yīng)用程序接口,基本上所有的組件都需要 
     * 第二個參數(shù)表示生成一個Map(String ,Object)列表選項(xiàng) 
     * 第三個參數(shù)表示界面布局的id 表示該文件作為列表項(xiàng)的組件 
     * 第四個參數(shù)表示該Map對象的哪些key對應(yīng)value來生成列表項(xiàng) 
     * 第五個參數(shù)表示來填充的組件 Map對象key對應(yīng)的資源一依次填充組件 順序有對應(yīng)關(guān)系 
     * 注意的是map對象可以key可以找不到 但組件的必須要有資源填充 因?yàn)?找不到key也會返回null 其實(shí)就相當(dāng)于給了一個null資源 
     * 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head} 
     * 這個head的組件會被name資源覆蓋 
     * */ 
    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) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
  } 
 
} 

2016424102052967.jpg (418×350)

相關(guān)文章

  • Android開發(fā)筆記 改變字體顏色的三種方法

    Android開發(fā)筆記 改變字體顏色的三種方法

    在TextView中添加文本時(shí)有時(shí)需要改變一些文本字體的顏色,今天學(xué)到了三種方法,拿出來分享一下
    2012-11-11
  • Android布局之LinearLayout線性布局

    Android布局之LinearLayout線性布局

    LinearLayout是線性布局控件:要么橫向排布,要么豎向排布,下面通過本篇文章給大家介紹Android布局之LinearLayout線性布局,涉及到android linearlayout 布局相關(guān)知識,對本文感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • android實(shí)現(xiàn)九宮格程序

    android實(shí)現(xiàn)九宮格程序

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)九宮格程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android仿QQ空間主頁面的實(shí)現(xiàn)

    Android仿QQ空間主頁面的實(shí)現(xiàn)

    今天模仿安卓QQ空間,打開程序的啟動畫面和導(dǎo)航頁面我就不做了,大家可以模仿微信的那個做一下,很簡單。這次主要做一下主頁面的實(shí)現(xiàn),感興趣的朋友可以參考下
    2013-01-01
  • android文件存儲和SharedPreferences存儲的項(xiàng)目實(shí)例

    android文件存儲和SharedPreferences存儲的項(xiàng)目實(shí)例

    本文主要介紹了android文件存儲和SharedPreferences存儲的項(xiàng)目實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Android 實(shí)現(xiàn)抖音小游戲潛艇大挑戰(zhàn)的思路詳解

    Android 實(shí)現(xiàn)抖音小游戲潛艇大挑戰(zhàn)的思路詳解

    《潛水艇大挑戰(zhàn)》是抖音上的一款小游戲,最近特別火爆,很多小伙伴都玩過。接下來通過本文給大家分享Android 手?jǐn)]抖音小游戲潛艇大挑戰(zhàn)的思路,需要的朋友可以參考下
    2020-04-04
  • Android編程調(diào)用紅外線遙控功能示例

    Android編程調(diào)用紅外線遙控功能示例

    這篇文章主要介紹了Android編程調(diào)用紅外線遙控功能,結(jié)合實(shí)例形式分析了Android紅外線功能的相關(guān)組件調(diào)用與布局操作實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-08-08
  • Android實(shí)現(xiàn)笑臉進(jìn)度加載動畫

    Android實(shí)現(xiàn)笑臉進(jìn)度加載動畫

    這篇文章主要介紹了Android實(shí)現(xiàn)笑臉進(jìn)度加載動畫的方法,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-05-05
  • Android自定義View實(shí)現(xiàn)微信支付密碼輸入框

    Android自定義View實(shí)現(xiàn)微信支付密碼輸入框

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)微信支付密碼輸入框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Android SurfaceView畫板操作

    Android SurfaceView畫板操作

    這篇文章主要為大家詳細(xì)介紹了Android SurfaceView畫板操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05

最新評論

马鞍山市| 四会市| 铜陵市| 崇仁县| 惠水县| 宁城县| 屯留县| 廉江市| 方正县| 汾西县| 金门县| 乌拉特前旗| 嵩明县| 临颍县| 始兴县| 桃园市| 梁平县| 会宁县| 河东区| 蒲城县| 光泽县| 宁波市| 邮箱| 鄂尔多斯市| 大连市| 富宁县| 磐石市| 利川市| 赤水市| 宝兴县| 碌曲县| 抚州市| 和林格尔县| 华池县| 东乌珠穆沁旗| 五家渠市| 徐汇区| 宜兰县| 明溪县| 蓬安县| 宿松县|