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