`
ssun125
  • 浏览: 44623 次
文章分类
社区版块
存档分类
最新评论

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>

分享到:
评论

相关推荐

    Android中采用ListView实现数据列表显示案例

    在Android开发中,ListView是...总之,Android中的ListView是显示数据列表的核心组件,通过适配器与数据源的配合,我们可以实现丰富的数据展示和交互。同时,合理地优化性能和处理数据源的动态变化,能够提升用户体验。

    传智播客_Andorid_Android基础视频video_第三天-第10节_10_listview入门

    传智播客_Andorid_Android基础视频video_第三天-第10节_10_listview入门.avi

    Android ListView实现图文列表显示

    本文实例为大家分享了Android ListView实现图文列表显示的具体代码,供大家参考,具体内容如下 目标效果:   ListView如果内容过多,可以滑动屏幕来显示,并且点击某一行可使用吐司方法弹出对应的水果名字。 1....

    Android studio listview实现列表数据显示 数据循环显示效果

    本文将详细介绍 Android Studio 中 ListView 的实现列表数据显示和循环显示效果,帮助开发者更好地掌握 ListView 的使用和应用。 ListView 的基本概念 ListView 是 Android 中的一个控件,用于显示大量数据的列表...

    Android studio listview实现列表数据显示 数据循环显示

    Android studio listview实现列表数据显示 样式不好看!想要好看的样式可以私我,我加! item.xml main.xml MainActivity .java public class MainActivity extends Activity { List list; List ...

    Android学习笔记-采用ListView实现数据列表显示-以及各种适配器使用-和如何写.pdf

    Android学习笔记-采用ListView实现数据列表显示-以及各种适配器使用-和如何写.pdf

    采用listview实现数据列表显示

    本教程将详细介绍如何利用ListView来实现数据列表的显示,并重点探讨其中涉及到的三种适配器。 首先,ListView的基础概念是关键。ListView是一个可以滚动的视图,它能够按需加载并显示多个视图项(View Item),以...

    Android用listview显示数据库中的数据

    5. **Android客户端显示数据**:在Android应用中,我们需要一个ListView控件来展示数据。ListView是一种可滚动的视图,可以显示多个项目,适合于显示列表数据。 6. **创建数据适配器**:ListView的工作原理是通过...

    android的listview嵌套listview,列表嵌套列表 android studio版本

    在Android开发中,ListView是一种常用的视图组件,用于展示大量数据列表。当需求涉及列表中的每个条目本身也需要展示一个子列表时,我们就会遇到ListView的嵌套问题。本教程将详细讲解如何在Android Studio环境下...

    传智播客的android开发源代码

    19_采用ListView实现数据列表显示.avi 所在项目:db 20_采用ContentProvider对外共享数据.avi 所在项目:db & other 21_监听ContentProvider中数据的变化.avi 所在项目:db & other & Aapp 22_访问通信录中的联系人...

    Android应用源码使用listView实现的树状结构.zip

    1. **ListView基础**:ListView是Android中的一个视图容器,它能够显示一列可滚动的项目列表。每个项目通常由一个布局文件定义,可以自定义样式。 2. **Adapter原理**:在ListView中,数据与视图的桥梁是Adapter。...

    传智播客Android视频教程-课程源码.rar

    采用ListView实现数据列表显示 2&gt; 采用ContentProvider对外共享数据 第四天 1&gt; 往通信录添加联系人,和获取联系人 2&gt; 网络--获取数据(图片、网页、xml、Json等) 3&gt; 如何把数据通过HTTP协议提交到网络上的Web...

    传智播客_Andorid_Android项目手机卫士视频video_day12视频

    在本课程"传智播客_Andorid_Android项目手机卫士视频video_day12视频"中,我们聚焦于Android应用程序开发,特别是针对一个名为"手机卫士"的项目进行深入学习。这个项目的目的是创建一个实用的安全应用,类似于现实...

    Android 根据EditText搜索框 ListView动态显示数据

    这就是如何在Android中根据EditText搜索框动态显示ListView数据的基本步骤。使用SimpleAdapter可以简化数据绑定,而TextWatcher则帮助我们监听并处理搜索事件。在实际应用中,你可能需要根据具体需求对数据结构和...

    android listview嵌套listview实现折叠列表

    本文将详细介绍如何在Android中使用ListView实现嵌套的可折叠效果。 首先,我们需要了解基本的ListView工作原理。ListView通过Adapter来绑定数据源,Adapter负责将数据转化为ListView可显示的View。在Android中,...

    Android实现的ListView-ListViewAdapter(新闻列表事例)

    综上所述,"Android实现的ListView-ListViewAdapter(新闻列表事例)"是一个涵盖了Android基础组件使用、数据绑定、事件处理、性能优化等多个方面的实践案例,对于深入理解Android开发非常有帮助。通过这个实例,...

    Android之ListView列表视图和界面跳转实现

    在Android开发中,ListView是一种非常常见的控件...以上就是关于如何在Android中实现ListView列表视图以及点击列表项进行界面跳转的基本步骤。通过不断实践和优化,你将能创建出功能丰富且用户体验良好的ListView应用。

Global site tag (gtag.js) - Google Analytics