使用单元测试添加数据:

package com.itheima.showdata;

import java.sql.ResultSet;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper; public class MyOpenHelper extends SQLiteOpenHelper { public MyOpenHelper(Context context) {
super(context, "people.db", null, 1);
// TODO Auto-generated constructor stub
} //数据库创建时,此方法会调用
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(_id integer primary key autoincrement, name char(10), salary char(20), phone integer(20))"); } //数据库升级时,此方法会调用
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("数据库升级了");
} }
package com.itheima.sqlitedatabase.test;

import com.itheima.showdata.MyOpenHelper;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase; public class TestCase extends AndroidTestCase { private MyOpenHelper oh;
private SQLiteDatabase db;
//测试框架初始化完毕之后,在测试方法执行之前,此方法调用
@Override
protected void setUp() throws Exception {
super.setUp(); oh = new MyOpenHelper(getContext());
db = oh.getWritableDatabase();
} //测试方法执行完毕之后,此方法调用
@Override
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
super.tearDown();
db.close();
} public void insertApi(){
//把要插入的数据全部封装至ContentValues对象
for (int i = 0; i < 50; i++) {
ContentValues values = new ContentValues();
values.put("name", "赵"+i);
values.put("phone", "159"+i+i);
values.put("salary", "160"+i+i);
db.insert("person", null, values);
}
} }

第一种页面显示:

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
> </LinearLayout>
</ScrollView>
package com.itheima.showdata;

import java.util.ArrayList;
import java.util.List; import com.itheima.showdata.domain.Person; import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView; public class MainActivity extends Activity { List<Person> personList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); personList = new ArrayList<Person>();
//把数据库的数据查询出来
MyOpenHelper oh = new MyOpenHelper(this);//这里上下文传this就可以。
SQLiteDatabase db = oh.getWritableDatabase();
Cursor cursor = db.query("person", null, null, null, null, null, null, null);
while(cursor.moveToNext()){
String _id = cursor.getString(0);
String name = cursor.getString(1);
String salary = cursor.getString(2);
String phone = cursor.getString(3); Person p = new Person(_id, name, phone, salary);
personList.add(p);
} LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
//把数据显示至屏幕
for (Person p : personList) {
//1.集合中每有一条元素,就new一个textView
TextView tv = new TextView(this);
//2.把人物的信息设置为文本框的内容
tv.setText(p.toString());
tv.setTextSize(18);
//3.把textView设置为线性布局的子节点
ll.addView(tv);
}
} }

第二种页面显示:

主activity和条目item

Activity:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
> <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout> 条目item: <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="名字"
android:textSize="25sp"
/>
<LinearLayout
android:layout_alignParentRight="true" 位于父容器右边
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" 竖直排列的线性布局
>
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="号码"
/>
<TextView
android:id="@+id/tv_salary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="工资"
/>
</LinearLayout>
</RelativeLayout>

java代码:

package com.itheima.showdata;

import java.util.ArrayList;
import java.util.List; import com.itheima.listviewshowdata.R;
import com.itheima.showdata.domain.Person; import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity {
List<Person> personList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
personList = new ArrayList<Person>();
//把数据库的数据查询出来
MyOpenHelper oh = new MyOpenHelper(this);
SQLiteDatabase db = oh.getWritableDatabase();
Cursor cursor = db.query("person", null, null, null, null, null, null, "10,1000");//"10,1000"是分页查询,表示从10条开始查查1000条,
while(cursor.moveToNext()){
String _id = cursor.getString(0);
String name = cursor.getString(1);
String salary = cursor.getString(2);
String phone = cursor.getString(3); Person p = new Person(_id, name, phone, salary);
personList.add(p);
}
//ListView是列表,ListView只带滑动的功能,ListView的每一行数据是item条目View对象。
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new MyAdapter());//M:personList V:ListView C:MyAdapter
} //如果一屏幕只能显示10个,则一开始只显示10个调用getView方法10次,
//向下滑动的时候会依次调用getView方法并传入position的值为11,12,13,向上滚动的时候也会依次调用getView并传入position为9,8,7
//滚动屏幕的时候把后面的数据加载进来前面的移除,保证内存永远只有10条数据,
class MyAdapter extends BaseAdapter{
//系统调用,用来获知集合中有多少条元素,适配器要处理的数据量
@Override
public int getCount() {
return personList.size();
}
//由系统调用,获取一个View对象,作为ListView的条目
//position:本次getView方法调用所返回的View对象,在listView中是处于第几个条目,那么position的值就是多少
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Person p = personList.get(position);
// TextView tv = new TextView(MainActivity.this);
// tv.setText(p.toString());
// tv.setTextSize(18);
System.out.println("getView调用:" + position + ";" + convertView);
View v = null;//将每一个条目封装为View对象
//判断条目是否有缓存,移出的条目不会立即销毁而是会缓存起来,再次滑动出来的时候不会取而是从缓存取出来,
if(convertView == null){
//把布局文件填充成一个View对象,ViewGroup是View的子类,可以有子节点,View不能有子节点,TextView也不能有子节点
v = View.inflate(MainActivity.this, R.layout.item_listview, null);
}
else{
v = convertView;//用的是缓存,例如当滑出第12个的时候系统发现有缓存(第1第2个的缓存),则会把第1的缓存放到第12的位置,所以即使使用的是缓存也要写下面的代码重新把第1的缓存变成第12的(内存不会新增只是做了改变)。
}
//获取布局填充器对象
// LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
// 使用布局填充器填充布局文件
// View v2 = inflater.inflate(R.layout.item_listview, null);
// LayoutInflater inflater2 = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);//布局填充服务,用于填充布局。
// View v3 = inflater2.inflate(R.layout.item_listview, null); //通过资源id查找组件,注意调用的是View对象的findViewById
TextView tv_name = (TextView) v.findViewById(R.id.tv_name);
tv_name.setText(p.getName());
TextView tv_phone = (TextView) v.findViewById(R.id.tv_phone);
tv_phone.setText(p.getPhone());
TextView tv_salary = (TextView) v.findViewById(R.id.tv_salary);
tv_salary.setText(p.getSalary());
return v;
} @Override
public Object getItem(int position) {
return null;
} @Override
public long getItemId(int position) {
return 0;
} }
/*###BaseAdapter
* 必须实现的两个方法
* 每一个条目都是一个View对象:View对象是控件的基类,所以就能够显示任意控件。
* 第一个
//系统调用此方法,用来获知模型层有多少条数据
@Override
public int getCount() {
return people.size();
}
* 第二个
//系统调用此方法,获取要显示至ListView的View对象
//position:是return的View对象所对应的数据在集合中的位置
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("getView方法调用" + position);
TextView tv = new TextView(MainActivity.this);
//拿到集合中的元素
Person p = people.get(position);
tv.setText(p.toString());
//把TextView的对象返回出去,它会变成ListView的条目
return tv;
}
* 屏幕上能显示多少个条目,getView方法就会被调用多少次,屏幕向下滑动时,getView会继续被调用,
创建更多的View对象显示至屏幕
###条目的缓存
* 当条目划出屏幕时,系统会把该条目缓存至内存,当该条目再次进入屏幕,
系统在重新调用getView时会把缓存的条目作为convertView参数传入,
但是传入的条目不一定是之前被缓存的该条目,即系统有可能在调用getView方法获取第一个条目时,
传入任意一个条目的缓存*/ }

android 70 使用ListView把数据显示至屏幕的更多相关文章

  1. 一起学android之设置ListView数据显示的动画效果(24)

    效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/40 ...

  2. Android--数据库数据显示至屏幕

    MainActivity.java 这段代码的作用是从数据库中获取到数据并显示在界面上 import java.util.ArrayList; import java.util.List; impor ...

  3. Android—自定义控件实现ListView下拉刷新

    这篇博客为大家介绍一个android常见的功能——ListView下拉刷新(参考自他人博客,网址忘记了,阅读他的代码自己理解注释的,希望能帮助到大家): 首先下拉未松手时候手机显示这样的界面: 下面的 ...

  4. Android开发学习——ListView+BaseAdapter的使用

    ListView 就是用来显示一行一行的条目的MVC结构 * M:model模型层,要显示的数据           ----people集合 * V:view视图层,用户看到的界面          ...

  5. Android UI组件----ListView列表控件详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  6. Android——列表视图(ListView)

    列表视图是android中最常用的一种视图组件,它以垂直列表的形式列出需要显示的列表项.在android中有两种方法向屏幕中添加列表视图:一种是直接使用ListView组件创建:另外一种是让Activ ...

  7. 【转】整理一下Android中的ListView

    原文网址:http://sunbofu.blog.51cto.com/6431507/1280441 Android中的listview目测是一个使用频率很高的组件,所以今天来总结一下listview ...

  8. Android SQLite与ListView的简单使用

    2017-04-25 初写博客有很多地方都有不足,希望各位大神给点建议. 回归主题,这次简单的给大家介绍一下Android SQLite与ListView的简单使用sqlite在上节中有介绍,所以在这 ...

  9. Android开发 ---实现ListView的A-Z字母排序和过滤搜索功能

    效果图: 1.activity.xml 描述: 线性布局中一个层叠布局 <?xml version="1.0" encoding="utf-8"?> ...

随机推荐

  1. CSAPP(深入理解计算机系统)读后感

    9月到10月8号,包括国庆七天,大概每天5小时以上的时间,把Computer System: A Programmer Perspective 2rd version(深入理解计算机系统)的英文版啃完 ...

  2. ORACLE 定时任务JOB

    http://www.cnblogs.com/xclw/archive/2009/12/04/1616945.html

  3. Stanford CoreNLP--Part of Speech

    Stanford CoreNLP Part Of Speech简称POS,主要是对待分析的句子中的单词进行标记的功能,如标记名词.动词等,该组件是CoreNLP工程的一部分,详细内容可参考:CoreN ...

  4. 【UVA 10816】 Travel in Desert (最小瓶颈树+最短路)

    [题意] 有n个绿洲, m条道路,每条路上有一个温度,和一个路程长度,从绿洲s到绿洲t,求一条道路的最高温度尽量小, 如果有多条, 选一条总路程最短的. InputInput consists of ...

  5. 【BZOJ 2005】[Noi2010]能量采集 (容斥原理| 欧拉筛+ 分块)

    能量采集 Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋 ...

  6. wcf 远程终结点已终止该序列 可靠会话出错

    https://social.msdn.microsoft.com/Forums/office/zh-CN/9f0c76d2-85b0-4cd3-979d-ceda7947bcd1/-?forum=w ...

  7. git撤销提交到remote的commit

    Reseting remote to a certain commit Assuming that your branch is called master both here and remotel ...

  8. linux上TCP connection timeout的原因查找

    linux上TCP connection timeout的原因查找 好久没有写文章了, 今天解决了一个网络连接超时的问题, 记录以备查看. 最近在线上nginx经常出现输出connection tim ...

  9. The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

    The server is temporarily unable to service your request due to maintenance downtime or capacity pro ...

  10. HttpServletRequest、request常用方法、request常见应用、请求转发、RequestDispatcher

        HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象的方法,可以获得客户这些信息. ...