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>();
//把数据库的数据查询出来
//在这里写成this,是由于在myopenhelper的super构造器中,已经写死了另外三个参数;
myopenhelper oh = new myopenhelper(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);
//把这几个值封装在一个类中,这种思想要学会;由于p在这里是一局部变量,所以定义了
//一个list的全局变量的容器去存放person类型的变量p;关键学会别人的这种思想;
person p = new person(_id, name, phone, salary);
personlist.add(p);
}
linearlayout ll = (linearlayout) findviewbyid(r.id.ll);
//把数据显示至屏幕
for (person p : personlist) {
//注意,textview除了在layout里边布局之外,也可以单独new出来,
//因为其也是一个类,是view类下边的一个子类,只是此时的textview
//和layout还没有关联起来,所以记得加上第3步
//1.集合中每有一条元素,就new一个textview
textview tv = new textview(this);
//2.把人物的信息设置为文本框的内容
tv.settext(p.tostring());
tv.settextsize(18);
//设置完上述两条语句并不会把textview显示在界面上,
//所以需要第三步,将其与layout关联起来;
//3.把textview设置为线性布局的子节点
ll.addview(tv);
}
}
注:当我们数据很多的时候,那么new出来的person也很多,与此同时,
new出来的textview也很多,那么此时内存有可能扛不住;所以我们应该
做的就是:我们需要什么数据显示在界面上的时候,就创建什么数据,
而不是一下子全部创建出来,所以我们在尽可能使用listview对其进行
进一步优化。
import java.sql.resultset;
import android.content.context;
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
}
//数据库创建时,此方法会调用
public void oncreate(sqlitedatabase db) {
db.execsql(create table person(_id integer primary key autoincrement, name char(10), salary char(20), phone integer(20)));
//数据库升级时,此方法会调用
public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {
system.out.println(数据库升级了);
这段代码的作用是添加数据进数据库
import com.itheima.showdata.myopenhelper;
import android.content.contentvalues;
import android.test.androidtestcase;
public class testcase extends androidtestcase {
private myopenhelper oh;
private sqlitedatabase db;
//测试框架初始化完毕之后,在测试方法执行之前,此方法调用
protected void setup() throws exception {
super.setup();
oh = new myopenhelper(getcontext());
db = oh.getwritabledatabase();
//测试方法执行完毕之后,此方法调用
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);
转载:http://blog.csdn.net/chaoyu168/article/details/50720888