Android ListViewview入门
接着上文《Android 数据库的事务》,往person数据表中插入50条数据
public void testAdd() throws Exception {
PersonDao dao = new PersonDao(getContext());
// .add("wangwu", "123", 50000);
// dao.add("zhangsan", "234", 17000);
int number = 857600001;
Random random = new Random();
for (int i = 0; i < 50; i++) {
dao.add("wuyudong" + i, Long.toString(number + i),
random.nextInt(5000)); }
}
常规方法显示数据
首先不使用ListViewview,而是直接使用程序来呈现数据表中的数据,代码如下:
package com.wuyudong.db; import java.util.List; import com.wuyudong.db.dao.PersonDao;
import com.wuyudong.db.domain.Person; import android.os.Bundle;
import android.text.style.LeadingMarginSpan;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ll_root = (LinearLayout)findViewById(R.id.ll_root);
PersonDao dao = new PersonDao(this);
List<Person> persons = dao.findAll();
for (Person person : persons) {
String info = person.toString();
TextView tv = new TextView(this);
tv.setTextSize(20);
tv.setTextColor(Color.BLACK);
tv.setText(info);
ll_root.addView(tv);
}
}
}
activity_main.xml中的代码如下:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:id="@+id/ll_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
</LinearLayout> </ScrollView>
运行结果如下图:
使用ListView显示数据
数据显示(listview)
需求: 把数据库的内容全部显示在界面上
符合MVC模型
m: model 数据模型 -- Person
v: view 视图 -- ListView
c: controller 控制器 --Adapter 数据适配器
下面使用ListViewview来实现这个功能
修改activity_main.xml中的代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView> </LinearLayout>
界面如下:
代码如下:
package com.wuyudong.db; import java.util.List; import com.wuyudong.db.dao.PersonDao;
import com.wuyudong.db.domain.Person; import android.os.Bundle;
import android.text.style.LeadingMarginSpan;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color; public class MainActivity extends Activity { private ListView lv;
private List<Person> persons; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PersonDao dao = new PersonDao(this);
persons = dao.findAll();
lv = (ListView) findViewById(R.id.lv); lv.setAdapter(new MyAdapter());
} // 默认实现类形如: simplexxx, basexxx, defaultxxx
private class MyAdapter extends BaseAdapter { private static final String TAG = "MyAdapter"; /**
* 控制ListView里面总共有多少条目
*/
@Override
public int getCount() {
return persons.size(); // 条目个数==集合的size
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) { Log.i(TAG, "返回view对象,位置: " + position);
TextView tv = new TextView(getApplicationContext());
tv.setTextSize(20);
tv.setTextColor(Color.BLACK);
// 得到某个位置对应的person对象
Person person = persons.get(position);
tv.setText(person.toString()); return tv;
} }
}
运行结果如下:
使用logcat过滤器查看:
只显示5个位置,说明手机屏幕只能显示这么多,如果滑动手机屏幕的话,位置数量会越来越多
Android ListViewview入门的更多相关文章
- [译]:Xamarin.Android开发入门——Hello,Android Multiscreen深入理解
原文链接:Hello, Android Multiscreen_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android Multiscreen深入理解. 本 ...
- [译]:Xamarin.Android开发入门——Hello,Android深入理解
返回索引目录 原文链接:Hello, Android_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android深入理解 本部分介绍利用Xamarin开发And ...
- [译]:Xamarin.Android开发入门——Hello,Android快速上手
返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...
- Hello, Android 快速入门
Hello, Android Android 开发与 Xamarin 简介 在这两节指南中,我们将 (使用 Xamarin Studio或 Visual Studio)建立我们的第一个 Xamarin ...
- Android工程师入门(二)——不忙不累怎么睡。。
安卓开发迫在眉睫,这周入个门吧! Android工程师入门(二) 四.在界面中显示图片 ImageView 是显示图片的一个控件. --属性 src——内容图片: background——背景图片/背 ...
- [电子书] 《Android编程入门很简单》
<Android编程入门很简单>是一本与众不同的Android学习读物,是一本化繁为简,把抽象问题具体化,把复杂问题简单化的书.本书避免出现云山雾罩.晦涩难懂的讲解,代之以轻松活泼.由浅入 ...
- Android实现入门界面布局
Android实现入门界面布局 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 首先是常量的定义,安卓中固定字符串应该定义在常量中. stri ...
- Android从入门到精通pdf+书源代码
不须要积分,免费放送 Android从入门到精通的pdf,入门的好书籍,因为csdn文件大小的限制所以分成了两部分. part1地址:http://download.csdn.net/detail/a ...
- 教我徒弟Android开发入门(一)
前言: 这个系列的教程是为我徒弟准备的,也适合还不懂java但是想学android开发的小白们~ 本系列是在Android Studio的环境下运行,默认大家的开发环境都是配置好了的 没有配置好的同学 ...
随机推荐
- Google C++ 风格指南 命名约定 转
命名约定 最重要的一致性规则是命名管理. 命名风格快速获知名字代表是什么东东: 类型? 变量? 函数? 常量? 宏 ... ? 甚至不需要去查找类型声明. 我们大脑中的模式匹配引擎可以非常可靠的处理这 ...
- spring 数据校验之Hibernate validation
1.需要的jar包 2.springsevlet-config.xml配置 在spring3之后,任何支持JSR303的validator(如Hibernate Validator)都可以通过简单配置 ...
- ES6笔记(1) -- 环境配置支持
系列文章 -- ES6笔记系列 虽然ES6已经发布一年多了,但在各大浏览器之中的支持度还不是很理想,在这查看ES6新特性支持度 Chrome的最新版本浏览器大部分已经支持,在Node.js环境上支持度 ...
- html5和css3的常用参考网
当我们使用HTML5, CSS3,甚至Bootstrap设计网站的时候,有些方面是必须考虑的,比如字体大小,标题大小,行间距,每行字数,字体,颜色,背景图片和文字的搭 配,图标,留白和布局...... ...
- ASP.NET MVC系列:添加控制器
基于MVC的应用程序包含三个部分 Models(模型):对应用程序的数据进行处理 Views(视图):动态生成HTML,显示数据 Controllers(控制器):应用程序中处理用户交互的部分,处理浏 ...
- Razor语法
1. 截取字符串 @(i.Title.Length > 18 ? i.Title.Substring(0, 18) + "" : i.Title) 2. 格式化日期 @s ...
- The system cannot find the file specified
在家工作,程序在家里的电脑运行时,出现一个异常,还是第一见到: Server Error in '/' Application. The system cannot find the file spe ...
- When using SqlDependency without providing an options value, SqlDependency.Start() must be called prior to execution of a command added to the SqlDependency instance.
在调试SignalR程序时,提示一个异常:When using SqlDependency without providing an options value, SqlDependency.Star ...
- IOS系统概述与层次
一.概述 IOS是apple公司为其自己的移动设备(iPhone,iPod touch,iPad)而开发的操作系统,IOS许多的技术是基于苹果的Mac OSX桌面系统的,如果你开发过苹果的mac系统应 ...
- IIS 部署WCF 4.0
上一章节讲解如何新建WCF服务,此文讲解如何在IIS上发布,并能正常访问 本地部署IIS 首先在本机安装IIS,IIS如何勾选,哪些是必须的?不太清楚,有清楚的大牛请指正!目前我的基本配置如下: 配置 ...