Android_03_擷取資料庫資訊並顯示在介面上
MainActivity.java
這段代碼的作用是從資料庫中擷取到資料並顯示在介面上
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 personList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); personList = new ArrayList(); //把資料庫的資料查詢出來 //在這裡寫成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一個textViewTextView tv = new TextView(this);//2.把人物的資訊設定為文字框的內容tv.setText(p.toString());tv.setTextSize(18);//設定完上述兩條語句並不會把TextView顯示在介面上,//所以需要第三步,將其與layout關聯起來;//3.把textView設定為線性布局的子節點ll.addView(tv);} } }
註:當我們資料很多的時候,那麼new出來的person也很多,與此同時,
new出來的TextView也很多,那麼此時記憶體有可能扛不住;所以我們應該
做的就是:我們需要什麼資料顯示在介面上的時候,就建立什麼資料,
而不是一下子全部建立出來,所以我們在下一節使用ListView對其進行
進一步最佳化
MyOpenHelper.java
這段代碼的作用是建立資料庫
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}//資料庫建立時,此方法會調用@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL(create table person(_id integer primary key autoincrement, name char(10), salary char(20), phone integer(20)));}//資料庫升級時,此方法會調用@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {System.out.println(資料庫升級了);}}
TestCase.java
這段代碼的作用是添加資料進資料庫
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;//測試架構初始化完畢之後,在測試方法執行之前,此方法調用@Overrideprotected void setUp() throws Exception {super.setUp();oh = new MyOpenHelper(getContext());db = oh.getWritableDatabase();}//測試方法執行完畢之後,此方法調用@Overrideprotected void tearDown() throws Exception {// TODO Auto-generated method stubsuper.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);}}}
註:由於這裡用到了測試單元架構,所以記得在資訊清單檔中添加兩條屬性,可參考資料庫操作那節;也可以根據提示的錯誤資訊再進行添加