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);
}
}