ListView中SimpleAdapter的使用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<AbsoluteLayout 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" > <ListView android:id="@+id/listView1" android:layout_width="205dp" android:layout_height="312dp" android:layout_x="33dp" android:layout_y="40dp" > </ListView> </AbsoluteLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:descendantFocusability="blocksDescendants"> <TextView android:text="TextView01" android:layout_height="wrap_content" android:textSize="20dip" android:layout_width="fill_parent" android:id="@+id/ItemTitle" /> <TextView android:text="TextView02" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_below="@+id/ItemTitle" android:id="@+id/ItemText" /> </RelativeLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.list.erlistview; import java.util.ArrayList; import java.util.HashMap; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; public class ListViewActivity extends Activity { ListView listView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView)findViewById(R.id.listView1); ArrayList<HashMap<String, Object>> listData= new ArrayList<HashMap<String, Object>>(); for(int i=0 ; i<5; i++){ HashMap<String, Object> map = new HashMap<String, Object>(); map.put("ItemTitleer", "Level" + i);//可以看到此处的第一个参数不是必须和xml文件中 map.put("ItemTexter", "nei rong " + i);//的控件名称相同,但是建议保持一致。 listData.add(map); } SimpleAdapter listItemAdapter = new SimpleAdapter(getApplicationContext(), listData, R.layout.listitem, new String[] {"ItemTitleer", "ItemTexter"}, //此行参数必须和map.put(Key,Value)中的Key相同 new int[] {R.id.ItemTitle,R.id.ItemText} ); listView.setAdapter(listItemAdapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub System.out.println("点击的是" + arg2); } }); } }