main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MyListView">
</ListView>
</LinearLayout>
my_listitem.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="wrap_content"
android:orientation="vertical"
android:id="@+id/MyListItem"
android:paddingBottom="3dip"
android:paddingLeft="10dip">
<TextView
android:id="@+id/ItemTitle"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="30dip" />
<TextView
android:id="@+id/ItemText"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</LinearLayout>
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 绑定XML中的ListView,作为Item的容器
ListView listView = (ListView) findViewById(R.id.MyListView);
// 生成动态数组,并且转载数据
ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 10; i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("ItemTitle###", "This is Title.....");
map.put("ItemText###", "This is text.....");
arrayList.add(map);
}
// 生成适配器,数组===》ListItem
SimpleAdapter aSimpleAdapter = new SimpleAdapter(
this, // 没什么解释
arrayList,// 数据来源
R.layout.my_listitem,// ListItem的XML实现
// 动态数组与ListItem对应的子项
new String[] { "ItemTitle###", "ItemText###" },// 这里面的
// ListItem的XML文件里面的两个TextView ID
new int[] { R.id.ItemTitle, R.id.ItemText });
// 添加并且显示
listView.setAdapter(aSimpleAdapter);
}
}