您好,欢迎来到爱玩科技网。
搜索
您的当前位置:首页Android应用中使用ListView实现数据列表显示(传智播客视频笔记)

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>

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务