android筆記 SimpleAdapter的示例代碼

1.main.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="fill_parent"
    android:orientation="vertical" >

 <ListView 
     android:id="@+id/android:list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:drawSelectorOnTop="false"
     ></ListView>

</LinearLayout>

2.row.xml,是列表中每一行的佈局文件

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="0,1,2" >

    <TableRow>

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TextView>

        <TextView
            android:id="@+id/sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            </TextView>

        <TextView
            android:id="@+id/age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            </TextView>
    </TableRow>

</TableLayout>

3.head.xml,是表頭的佈局文件

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="0,1,2"
     >
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名"
            ></TextView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性別"
            ></TextView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年齡"
            ></TextView>
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
        
    </TableRow>

</TableLayout>

4.MainActivity,主文件,注意看註釋

package tjj.SimpleAdapterTest;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView.LayoutParams;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity
{
		private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		String[] from = new String[] { "name", "sex", "age" };
		int[] to = { R.id.name, R.id.sex, R.id.age };
		TextView tv = null;

		/** Called when the activity is first created. */
		@Override
		public void onCreate(Bundle savedInstanceState)
		{
				super.onCreate(savedInstanceState);
				setContentView(R.layout.main);
				
				/*自己new一個Textiew來作爲footerview的視圖*/
				tv = new TextView(this);
				/*TextView的LayoutParams是用android.widget.AbsListView.LayoutParams類型*/
				LayoutParams params = new LayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT, 
								android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
				tv.setLayoutParams(params);

				HashMap<String, String> map1 = new HashMap<String, String>();
				map1.put("name", "wang");
				map1.put("sex", "男");
				map1.put("age", "11");

				HashMap<String, String> map2 = new HashMap<String, String>();
				map2.put("name", "li");
				map2.put("sex", "女");
				map2.put("age", "13");

				list.add(map1);
				list.add(map2);

				SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.row, from, to);
				
				/*這兩行語句必須在setListAdapter()之前執行*/
				getListView().addHeaderView(this.getLayoutInflater().inflate(R.layout.head, null), null, false);
				getListView().addFooterView(tv, null, false);
				
				setListAdapter(adapter);
		}

		@Override
		protected void onListItemClick(ListView l, View v, int position, long id)
		{
				/*通過getListView()和getItemAtPosition()得到指定位置的數據對象,因爲傳入的是HashMap類型,所以得到的要轉型爲HashMap*/
				HashMap map = (HashMap) getListView().getItemAtPosition(position);
				Toast.makeText(this, map.get("name") +"|" + map.get("sex") + "|" + map.get("age"), Toast.LENGTH_SHORT).show();
				tv.setText(map.get("name") +"|" + map.get("sex") + "|" + map.get("age"));
				super.onListItemClick(l, v, position, id);
		}
}


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