标签:
使用单元测试添加数据:
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); } } }
标签:
原文地址:http://www.cnblogs.com/yaowen/p/4927859.html