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

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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值