public class DbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "xxxx.sqlite"; //資料庫名稱
private static final int DATABASE_VERSION = 1; //資料庫版本
private static final String table_name = "table";
private SQLiteDatabase db;
public DbHelper(Context context) { //建構子
super(context, DATABASE_NAME, null, DATABASE_VERSION);
db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) { //初始化資料庫
DATABASE_CREATE_TABLE =
"create table class1 ("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "class_name TEXT"
+ ");";
db.execSQL(DATABASE_CREATE_TABLE);
String TABLE_Insert =
"INSERT INTO class1 (class_name) VALUES ('食');";
db.execSQL(TABLE_Insert);
TABLE_Insert =
"INSERT INTO class1 (class_name) VALUES ('衣');";
db.execSQL(TABLE_Insert);
}
public Cursor getclass1(){ //取得class1資料表內容
return db.query("class1",new String[] {"_id","class_name"} , null, null, null, null, null);
}
MainActivity.java
public class MainActivity extends Activity {
private DbHelper dbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstlayout);
Spinner sp01 = (Spinner)findViewById(R.id.spinner01);
dbHelper = new DbHelper(this);
final Cursor cursor = dbHelper.getclass1();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item ,cursor, new String[] { "class_name" }, new int[] {android.R.id.text1});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp01.setAdapter(adapter);
//.....處理點選後行為....
}
}





