Java代码
public class MainActivity extends ListActivity {//这里继承的是ListActivity
char name = 'a';
int age = 97;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//做了20组显示数据
for (int i = 0; i<20; i++){
HashMap<String,String> map = new HashMap<String, String>();
map.put("name", ""+name);
map.put("age", ""+age);
list.add(map);
name = (char) (age);
age = age +1;
}
//适配器参类型分别为:
//Context context,
//List<? extends Map<String, ?>> data, --规定了参数类型
//int resource, -----单元布局文件
//String[] from, -----通过这俩别称
//int[] to -----分别匹配到这俩控件
SimpleAdapter listASimpleAdapter =
new SimpleAdapter(this,
list,
R.layout.unit,
new String[]{"name","age"},
new int[]{R.id.name,R.id.age});
setListAdapter(listASimpleAdapter);//进行适配
}
@Override
//ListView触屏事件
//四个参数分别为:ListView本身,控件,位置,控件id
//SimpleAdapter 直接把data的id就作为position,CurserAdapter 根据View的positon来获取到这个view上数据id
//上面这句话引自这句话是引自--http://blog.youkuaiyun.com/azuremaple/article/details/7748353
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
}
主界面布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical" />
</LinearLayout>
</LinearLayout>
每单元布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/name"
android:layout_width="100dp"
android:layout_height="30dp"
android:gravity="center"/>
<TextView
android:id="@+id/age"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"/>
</LinearLayout>