android 中關於SimpleAdapter構造參數的問題。

來源:互聯網
上載者:User

 

simpleAdapter的建構函式 如下:

 

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

一個SimlpleAdapter是這個工作的。假設將SimpleAdapter用於ListView。那麼ListView的每一個清單項目就是resource參數值指定的布局。而data參數就是要載入到ListView中的資料。我們先看每一個清單項目,假設清單項目所對應的布局檔案中包含了兩個組件:TextView和EditText,id分別為textview和edittext。那麼在載入清單項目時,需要通過組件的id和data參數中List元素中的Map對象對應。因此,from參數Map對象的key,而to表示組件的id,例如,本例中的參數值為from = new String[]{"textview", "edittext"},to = new int[]{R.id.textview,R.id.edittext}。意思就是將Map對象中key為textview的value綁定到R.id.textview上,edittext也類似。

現在來看data參數,一個ListView由多個清單項目組成。每一個清單項目由一個Map對象提供資料,而多個清單項目則由List對象提供多個Map對象。OK,就是這麼簡單,只要瞭解了它們的工作原理,一切就可以搞定。如果還不太明白,自己做個例子吧。good luck!

 

 

這是我做的一個小例子,你如果有什麼不明白可以做一下,就知道了。

 

這是List_item.xml檔案

 

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <TextView

    android:id="@+id/ve_id_1"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

 

    />

    <TextView

    android:id="@+id/ve_id2"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

 

    />

 

    </LinearLayout>

 

 

這是ActivityList2 檔案

 

public class ActivityList2 extends Activity{

 

ListView listView;

ArrayList<Map<String,Object>> data ;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

listView = new ListView(this);

 

//建立一個simpleAdapter,利用系統的Layout來顯示這兩項

//SimpleAdapter adapter = new SimpleAdapter(this,data,android.R.layout.simple_list_item_2,

//new String[]{"姓名","性別"},new int[]{R.id.text1,R.id.text2});

 

//建立一個simpleAdapter,利用自己建立 的layout來顯示兩項

SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.simple_list_item_2,

new String[]{"姓名","性別"},new int[]{com.jiao.list.R.id.ve_id_1,com.jiao.list.R.id.ve_id2});

 

 

listView.setAdapter(adapter);

setContentView(listView);

 

}

public void PrepareData(){

data= new ArrayList<Map<String,Object>>();

Map<String,Object> item;

item=new HashMap<String,Object>();

item.put("姓名", "張三小朋友");

item.put("性別", "男");

data.add(item);

item=new HashMap<String,Object>();

item.put("姓名", "小張同學");

item.put("性別", "女");

data.add(item);

item=new HashMap<String,Object>();

item.put("姓名", "張玉鵬同學");

item.put("性別", "女");

data.add(item);

}

}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.