馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
小弟初學,目前跟著書上做,可是我遇到了困難= ="
程式碼都對可是進入模擬器裡就異常終止了,以下是我得程式碼
懇請高手大大們幫我看一下是哪裡出錯了,感謝大大們
package com.example.men007;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
private DatabaseHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 建立資料庫StudentDB和Table:students,Table:courses
helper = new DatabaseHelper(this);
final SQLiteDatabase db = helper.getReadableDatabase();
// 自students表取得所有資料的student_no,安置在list[]上
Cursor c = db.query("students", new String[] { "student_no" }, null,
null, null, null, null);
c.moveToFirst();
CharSequence[] list = new CharSequence[c.getCount()];
for (int i = 0; i < list.length; i++) {
list[i] = c.getString(0);
c.moveToNext();
}
c.close();
// 顯示student_no在Spinner表單-spinner上
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item, list));
// 在Spinner表單-spinner上選定查詢資料-student_no
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String student_no = ((Spinner) parent).getSelectedItem()
.toString();
// 自students表依student_no找到student_name
Cursor c;
c = db.query("students", new String[] { "student_name" },
"student_no='" + student_no + "'", null, null, null,
null);
c.moveToFirst();
String student_this = c.getString(0);
c.close();
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(student_this);
// 自courses表依student_no找到所有course_name
c = db.query("courses", new String[] { "course_name" },
"student_no='" + student_no + "'", null, null, null,
null);
c.moveToFirst();
CharSequence[] list = new CharSequence[c.getCount()];
for (int i = 0; i < list.length; i++) {
list[i] = c.getString(0);
c.moveToNext();
}
c.close();
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
MainActivity.this, android.R.layout.simple_list_item_1,
list);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
helper.close();
}
// SQLiteOpenHelper-建立資料庫StrdentDB和Table:students,Table:courses
class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "StudentDB";
private static final int DATABASE_VERSION = 1;
// Table:students的資料
private String[][] STUDENTS = { { "A-123", "Macoto Lin" },
{ "B-456", "Hatusko Ueno" }, { "C-789", "Wilson Lin" } };
// Table:courses的資料
private String[][] COURSES = { { "A-123", "Japanses" },
{ "A-123", "Computer" }, { "B-456", "English" },
{ "B-456", "English" }, { "C-789", "Computer" },
{ "C-789", "Computer" } };
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.beginTransaction();
try {
SQLiteStatement stmt;
// SQL語句for建立資料庫的表單-table:students
String sql01 = "create table students(student_no text primary key,syudent_name text not null);";
// 執行table:students SQL語句
db.execSQL(sql01);
// 建立table:students資料群
stmt = db.compileStatement("insert into students values(?,?);");
for (String[] studentname : STUDENTS) {
stmt.bindString(1, studentname[0]);
stmt.bindString(2, studentname[1]);
stmt.executeInsert();
}
// SQL語句for建立資料庫的表單-table:courses
String sql02 = "create table courses(student_no text not null,course_name text not null);";
// 執行courses SQL語句
db.execSQL(sql02);
// 建立table:courses資料群
stmt = db.compileStatement("insert into courses values(?,?);");
for (String[] coursename : COURSES) {
stmt.bindString(1, coursename[0]);
stmt.bindString(2, coursename[1]);
stmt.executeInsert();
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
|