編輯:關於Android編程
SimpleAdapter,跟名字一樣,一個簡單的適配器,既為簡單,就只是被設計來做簡單的應用的,比如靜態數據的綁定,不過仍然有自定義的空間,比如說在每一個ListItem中加一個按鈕並添加響應事件.首先還是先看一下SimpleAdapter的定義吧,直接翻譯下SDK doc 吧:
這是一個簡單的適配器,可以將靜態數據映射到XML文件中定義好的視圖。你可以指定由Map組成的List(比如ArrayList)類型的數據。在ArrayList中的每個條目對應List中的一行。Maps包含每一行的數據。你可以指定一個XML布局以指定每一行的視圖,根據Map中的數據映射關鍵字到指定的視圖。綁定數據到視圖分兩個階段,首先,如果設置了SimpleAdapter.ViewBinder,那麼這個設置的ViewBinder的setViewValue(android.view.View, Object, String)將被調用。如果setViewValue的返回值是true,則表示綁定已經完成,將不再調用系統默認的綁定實現。如果返回值為false,視圖將按以下順序綁定數據:
如果View實現了Checkable(例如CheckBox),期望綁定值是一個布爾類型。
TextView.期望綁定值是一個字符串類型,通過調用setViewText(TextView, String)綁定。
ImageView,期望綁定值是一個資源id或者一個字符串,通過調用setViewImage(ImageView, int) 或 setViewImage(ImageView, String)綁定數據。
如果沒有一個合適的綁定發生將會拋出IllegalStateException。
先看一下構造函數:
復制代碼 代碼如下:
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
SimpleAdapter基本上認知了其參數含義 用起來就簡單多了。
SimpleAdapter的參數說明:
下面的程序中如果 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 = { "魔域玩家", "百家執行", "高級的富一代", "妹子請過來..一個善於跑妹子的。。" };
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的參數說明
* 第一個參數 表示訪問整個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資源覆蓋
* */
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;
}
}

qq火炬火種怎麼獲得 qq火炬傳遞明星派發火種怎麼預約
裡約奧運會馬上就要開幕啦啦,這兩天大家應該都玩上了騰訊的新科技也就是qq火炬,今天又有新鮮玩法了,qq邀請了關曉彤、蔣勁夫、田亮、周冬雨等一眾明星進行火種派
Android Studio教程從入門到精通
本文目標在於指導新手,文章順序如下:Android Studio新手–>下載安裝配置–>零基礎入門–>基本使用&nd
AndroidStuio快速發布開源項目到Jcenter/Bintray
前言最近做了兩個開源項目,一個是Android網絡框架NoHttp,和Volley、OkHttp一樣做客戶端請求的,更多相關信息請百度哦;另一個是Android Web服
Android自定義popupwindow實例代碼
先來看看效果圖:一、布局 <?xml version=1.0 encoding=utf-8?><LinearLayout xm