適配器之SimpleAdapter

     android裏的適配器,剛開始看起來,我覺得很神奇,而且有種桃花漸欲迷人眼之感。它說白了,就是把兩個不匹配的東西,連接起來,讓它們能發揮作用。不匹配的東西種類多了,那自然適配器的種類也會很多。下面先介紹一種SimpleAdapter 的適配器。demo的作用是把一組圖片和文字內容,放到一個下拉列表裏顯示出來。

     MyAdapter.java 這個文件當相於數據源

    

public class MyAdapter {
	public MyAdapter() {
		// TODO Auto-generated constructor stub
	}
	public static List<Map<String,Object>> getListMaps(){
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		Map<String,Object> map1 = new HashMap<String, Object>();
		map1.put("Logo", R.drawable.calendar);
		map1.put("applicationName", "日曆");
		
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("Logo", R.drawable.eoemarket);
		map2.put("applicationName", "eoemarket客戶端");
		list.add(map1);
		list.add(map2);
		return list;
	}
}
MainActivity.java 主要的Actity程序

public class MainActivity extends ActionBarActivity {
	private Spinner spinner;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		spinner = (Spinner) this.findViewById(R.id.spinner);
		
		List<Map<String,Object>> listmaps = MyAdapter.getListMaps();
		//這是適配器的關鍵所在,
		SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, listmaps,
				R.layout.item, new String[] { "Logo", "applicationName" },
				new int[] { R.id.imageview, R.id.textview });
		
		spinner.setAdapter(simpleAdapter);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				String appName =((Map<String,Object>)spinner.getItemAtPosition(position)).get("applicationName").toString();
				setTitle(appName);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >  
    <Spinner android:id="@+id/spinner"
        android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="60dp"
        android:layout_height="60dp" 
        android:src="@drawable/icon"
        android:paddingLeft="10dp"/>
    <TextView android:id="@+id/textview"
        android:textColor="#000"
        android:textSize="16dp"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" 
        android:gravity="center_vertical"
        android:paddingLeft="10dp"/>
    

</LinearLayout>
效果圖如下:


源代碼下載地址:http://download.csdn.net/detail/chexitianxia/9115001

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章