Android應用中使用ListView實現資料列表顯示
UsingListViewActivity.java源碼:
package com.sinaapp.ssun.listview;</p><p>import java.util.ArrayList;<br />import java.util.HashMap;<br />import java.util.List;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.widget.ListView;<br />import android.widget.SimpleAdapter;</p><p>public class UsingListViewActivity extends Activity {<br />private List<Person> persons = new ArrayList<Person>();<br />private ListView listView; </p><p>@Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> listView = (ListView) this.findViewById(R.id.listView);</p><p> for(int i=0; i<10; i++){<br /> Person p = new Person("SSUN-"+i,"12340-"+i,i*100);<br /> persons.add(p);<br /> }</p><p> show();<br /> }</p><p>private void show() {<br />List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();</p><p>for(Person p : persons){<br />HashMap<String, Object> hm = new HashMap<String, Object>();<br />hm.put("name", p.getName());<br />hm.put("phone", p.getPhone());<br />hm.put("amount", p.getAmount());<br />data.add(hm);<br />}</p><p>SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.item,<br />new String[]{"name","phone","amount"},<br />new int[]{R.id.name,R.id.phone,R.id.amount});<br />listView.setAdapter(adapter);<br />}<br />}</p><p>class Person{<br />private String name;<br />private String phone;<br />private Integer amount;</p><p>public Person(String name,String phone,Integer amount){<br />this.name = name;<br />this.phone = phone;<br />this.amount = amount;<br />}</p><p>public String getName() {<br />return name;<br />}</p><p>public void setName(String name) {<br />this.name = name;<br />}</p><p>public String getPhone() {<br />return phone;<br />}</p><p>public void setPhone(String phone) {<br />this.phone = phone;<br />}</p><p>public Integer getAmount() {<br />return amount;<br />}</p><p>public void setAmount(Integer amount) {<br />this.amount = amount;<br />}<br />}</p><p>
main.xml檔案:
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:orientation="vertical" ></p><p><LinearLayout<br /> android:layout_width="match_parent"<br /> android:layout_height="wrap_content"<br /> android:orientation="horizontal" > </p><p> <TextView<br /> android:textSize="22sp"<br /> android:layout_width="100dp"<br /> android:layout_height="wrap_content"<br /> android:text="@string/name" /></p><p> <TextView<br /> android:textSize="22sp"<br /> android:layout_width="100dp"<br /> android:layout_height="wrap_content"<br /> android:text="@string/phone" /></p><p> <TextView<br /> android:textSize="22sp"<br /> android:layout_width="100dp"<br /> android:layout_height="wrap_content"<br /> android:text="@string/amount" /><br /></LinearLayout></p><p><ListView<br /> android:id="@+id/listView"<br /> android:layout_width="match_parent"<br /> android:layout_height="wrap_content" ><br /></ListView></p><p></LinearLayout>
item.xml檔案:
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent"<br /> android:orientation="horizontal" ></p><p> <TextView<br /> android:id="@+id/name"<br /> android:layout_width="100dp"<br /> android:layout_height="wrap_content"<br /> android:textSize="22sp" /></p><p> <TextView<br /> android:id="@+id/phone"<br /> android:layout_width="100dp"<br /> android:layout_height="wrap_content"<br /> android:textSize="22sp" /></p><p> <TextView<br /> android:id="@+id/amount"<br /> android:layout_width="100dp"<br /> android:layout_height="wrap_content"<br /> android:textSize="22sp" /></p><p></LinearLayout>
string.xml檔案:
<?xml version="1.0" encoding="utf-8"?><br /><resources></p><p> <string name="app_name">ListView應用</string><br /> <string name="name">姓名</string><br /> <string name="phone">電話</string><br /> <string name="amount">金額</string></p><p></resources>