直接贴代码:
ListActivity.java
package com.example.zzmap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ListActivity extends Activity {
private ListView listView;
private SimpleAdapter adapter;
private List<Map<String, String>> data = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
listView = (ListView) findViewById(R.id.listview);
data = MyDataSource.getMaps();
adapter = new SimpleAdapter(ListActivity.this, data,
R.layout.list, new String[] { "pname", "price",
"address" }, new int[] { R.id.pname, R.id.price,
R.id.address });
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
// TODO Auto-generated method stub
switch (position) {
case 0:
Log.e("WQR", "苹果");
break;
case 1:
Log.e("WQR", "西瓜");
break;
case 2:
Log.e("WQR", "梨子");
break;
default:
break;
}
}
});
}
}
MyDataSource.java
package com.example.zzmap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MyDataSource {
public MyDataSource() {
}
public static List<Map<String, String>> getMaps(){
List<Map<String, String>> listMaps = new ArrayList<Map<String,String>>();
//第一条数据
Map<String, String> map1 = new HashMap<String, String>();
map1.put("pname", "苹果");
map1.put("price", "¥2.5");
map1.put("address", "合肥");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("pname", "西瓜");
map2.put("price", "¥0.9");
map2.put("address", "南京");
Map<String, String> map3 = new HashMap<String, String>();
map3.put("pname", "梨子");
map3.put("price", "¥9.5");
map3.put("address", "江西");
listMaps.add(map1);
listMaps.add(map2);
listMaps.add(map3);
return listMaps;
}
}
list.xml
<?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="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/pname"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="产品名称" />
<TextView
android:id="@+id/price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="产品价格" />
<TextView
android:id="@+id/address"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="产品产地" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>