import java.util.ArrayList;
import java.util.List;
-
import android.app.Activity;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lecture);
- ArrayList<Task> tasks=(ArrayList<Task>)getIntent().getSerializableExtra("task");
- if(tasks==null){
- tasks=new ArrayList<Task>();
- tasks.add(new Task(1,"課題1"));
- tasks.add(new Task(2,"課題2"));
- tasks.add(new Task(3,"課題3"));
- tasks.add(new Task(4,"課題4"));
- tasks.add(new Task(5,"課題5"));
- tasks.add(new Task(6,"課題6"));
+ //データベース読み取りor作成
+ DatabaseHelper h=new DatabaseHelper(this);
+ final SQLiteDatabase db=h.getReadableDatabase();
+ //LECTURE_KEYを受け取って、データベースから講義情報を検索
+ String[][] list;
+ Integer lec=getIntent().getIntExtra(LectureFolderActivity.LECTURE_KEY,1);
+ {
+ String[] arg={""};
+ arg[0]=lec.toString();
+ Cursor c=db.rawQuery("SELECT lectures.name,teachers.id,teachers.name from lectures,teachers WHERE lectures.id=? AND lectures.teacher_id=teachers.id;", arg);
+ //結果をlistに格納
+ c.moveToFirst();
+ int col=3;
+ list = new String[c.getCount()][col];
+ for (int i = 0; i < list.length; i++) {
+ for(int j=0;j<col;j++){
+ list[i][j] = c.getString(j);
+ }
+ c.moveToNext();
+ }
+ c.close();
}
- Lecture lec=(Lecture)getIntent().getSerializableExtra(LectureFolderActivity.LECTURE_KEY);
- //見つからなかった
- if(lec==null){
- Teacher t=new Teacher(0, "ニャンコ先生");
+ //Teacher,Lectureクラスに格納
+ Teacher t;
+ Lecture lecture;
+ if(list.length==0){//見つからなかった
+ t=new Teacher(0, "ニャンコ先生");
List<Integer> l=new ArrayList<Integer>();
for(int i=0;i<10;i++){
l.add(i);
}
- lec=new Lecture(0,"授業:ネコ耳学",t,l);
+ lecture=new Lecture(0,"授業:ネコ耳学",t,l);
+ }else{//見つかった
+ t=new Teacher(Integer.parseInt(list[0][1]),list[0][2]);
+ lecture=new Lecture(lec,list[0][0],t,null);
}
//ヘッダ
ImageView img=(ImageView)findViewById(R.id.imageButtonLecture);
*/
//スクロール
LinearLayout scr=(LinearLayout)findViewById(R.id.linearViewLecture);
- //
- /*
- WakuwakuLecture lec=new WakuwakuLecture("にゃーー");
- lec.hw.add(new WakuwakuHomework("にゃん"));
- lec.hw.add(new WakuwakuHomework("ねこー"));
- lec.hw.add(new WakuwakuHomework("わん"));
- */
- //
+ //ボタンに講義情報表示
Button btn=(Button)findViewById(R.id.buttonLecture);
- btn.setText(getLectureString(lec));
- //
- Integer i=0;
- for(Integer id : lec.getTaskIds() ){
- Button b=new Button(this);
- try{
- Task t=tasks.get(id);
- b.setId(t.getId());
- b.setText(getTaskString(i,t));
- }catch(IndexOutOfBoundsException e){
- b.setId(0);
- b.setText("????");
- }
- /*
- //背景
- b.setBackgroundDrawable(d);
- */
- /*
- //左にアイコン
- Drawable d=getResources().getDrawable(R.drawable.nekonote);
- d.setBounds(0, 0, d.getIntrinsicWidth()/2, d.getIntrinsicHeight()/2);
- b.setCompoundDrawables(d, null, null, null);
- */
- b.setHeight(128);
- b.setOnClickListener(this);
- scr.addView(b);
- i++;
+ btn.setText(getLectureString(lecture));
+ //データベースからタスクを情報取得して、ボタンに表示
+ {
+ String[] arg={""};
+ arg[0]=lec.toString();
+ Cursor c=db.rawQuery("SELECT tasks.id,tasks.name from tasks,lectures WHERE tasks.lecture_id=? AND tasks.lecture_id=lectures.id;",arg);
+ //結果をlistに格納
+ c.moveToFirst();
+ int col=2;
+ String[][] tasklist = new String[c.getCount()][col];
+ for (int i = 0; i < tasklist.length; i++) {
+ for(int j=0;j<col;j++){
+ tasklist[i][j] = c.getString(j);
+ }
+ //ボタン作成
+ Button b=new Button(this);
+ /*
+ //背景
+ b.setBackgroundDrawable(d);
+ */
+ /*
+ //左にアイコン
+ Drawable d=getResources().getDrawable(R.drawable.nekonote);
+ d.setBounds(0, 0, d.getIntrinsicWidth()/2, d.getIntrinsicHeight()/2);
+ b.setCompoundDrawables(d, null, null, null);
+ */
+ b.setHeight(128);
+ b.setOnClickListener(this);
+ scr.addView(b);
+ try{
+ Task tsk=new Task(Integer.parseInt(tasklist[i][0]),tasklist[i][1]);
+ b.setId(tsk.getId());
+ b.setText(getTaskString(i,tsk));
+ }catch(IndexOutOfBoundsException e){
+ b.setId(0);
+ b.setText("????");
+ }
+ c.moveToNext();
+ }
+ c.close();
}
}
private String getLectureString(Lecture lec){
switch (v.getId()) {
}
}
+ class DatabaseHelper extends SQLiteOpenHelper {
+ public DatabaseHelper(Context context) {
+ super(context, "test.db", null, 1);
+ }
+
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+ db.execSQL("CREATE TABLE \"jars\" (\"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \"name\" varchar(255), \"revision\" integer, \"task_id\" integer, \"created_at\" datetime NOT NULL, \"updated_at\" datetime NOT NULL);");
+ db.execSQL("CREATE TABLE \"lectures\" (\"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \"name\" varchar(255), \"teacher_id\" integer, \"created_at\" datetime NOT NULL, \"updated_at\" datetime NOT NULL);");
+ db.execSQL("CREATE TABLE \"schema_migrations\" (\"version\" varchar(255) NOT NULL);");
+ db.execSQL("CREATE TABLE \"tasks\" (\"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \"name\" varchar(255), \"lecture_id\" integer, \"created_at\" datetime NOT NULL, \"updated_at\" datetime NOT NULL);");
+ db.execSQL("CREATE TABLE \"teachers\" (\"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \"name\" varchar(255), \"created_at\" datetime NOT NULL, \"updated_at\" datetime NOT NULL);");
+ db.execSQL("CREATE INDEX \"index_jars_on_task_id\" ON \"jars\" (\"task_id\");");
+ db.execSQL("CREATE INDEX \"index_lectures_on_teacher_id\" ON \"lectures\" (\"teacher_id\");");
+ db.execSQL("CREATE INDEX \"index_tasks_on_lecture_id\" ON \"tasks\" (\"lecture_id\");");
+ // add lecture
+ String insertLec = "INSERT INTO \"lectures\" (\"name\" , \"teacher_id\" , \"created_at\", \"updated_at\") VALUES (?,?,datetime() ,datetime() );";
+ // name,teacher_id
+ Object[][] lecData = { { "lec1", 1 }, { "lec2", 2 }, { "lec3", 3 }, };
+ for (Object[] lec : lecData) {
+ db.execSQL(insertLec, lec);
+ }
+ // add tasks
+ String insertTask = "INSERT INTO \"tasks\" (\"name\" , \"lecture_id\" , \"created_at\" , \"updated_at\" ) VALUES (?,?,datetime(),datetime());";
+ // name,lecture_id
+ Object[][] tskData = { { "task1-1", 1 }, { "task1-2", 1 },
+ { "task2-1", 2 }, { "task2-2", 2 }, { "task3", 3 }, };
+ for (Object[] tsk : tskData) {
+ db.execSQL(insertTask, tsk);
+ }
+ //
+ String insertTeacher = "INSERT INTO \"teachers\" (\"name\" , \"created_at\", \"updated_at\") VALUES (?,datetime() ,datetime() );";
+ // name,teacher_id
+ Object[][] teacherData = { { "teacher1" }, { "teacher2" },
+ { "teacher3" }, };
+ for (Object[] teacher : teacherData) {
+ db.execSQL(insertTeacher, teacher);
+ }
+ // db.execSQL("INSERT INTO \"teachers\" (\"name\" , \"teacher_id\" , \"created_at\", \"updated_at\") VALUES (\"test\",3,'2001/4/1','2001/4/1');");
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+ }
+ }
}