您的位置:首页 > 移动开发 > Android开发

Android应用中使用ListView实现数据列表显示(传智播客视频笔记)

2012-06-02 20:09 861 查看

Android应用中使用ListView实现数据列表显示

UsingListViewActivity.java源码:

package com.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="http://schemas.android.com/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="http://schemas.android.com/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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: