我一直收到这个错误,我检查了所有内容,但无法找到问题。我试图用一些测试数据填充数据库表,而不是使用这些数据填充微调器。以下是我在活动的onCreate中的代码:
courseSpinner = (Spinner) findViewById(R.id.spinCourse);
SQLiteHelperCourse courseDbHelper = new SQLiteHelperCourse(this);
SQLiteDatabase dbw = courseDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CourseEntry.COLUMN_NAME_COURSE_ID, "1");
values.put(CourseEntry.COLUMN_NAME_COURSE_NAME, "Android");
values.put(CourseEntry.COLUMN_NAME_CREDITS, "3");
values.put(CourseEntry.COLUMN_NAME_SEMESTER, "1st");
values.put(CourseEntry.COLUMN_NAME_YEAR, "3rd");
dbw.insert(CourseEntry.TABLE_NAME, CourseEntry.COLUMN_NAME_NULLABLE, values);
SQLiteDatabase dbr = courseDbHelper.getReadableDatabase();
String[] projection = { CourseEntry.COLUMN_NAME_COURSE_NAME};
final ArrayList<String> list = new ArrayList<String>();
Cursor curs = dbr.query(CourseEntry.TABLE_NAME,
projection,
null,
null,
null,
null,
null);
curs.moveToFirst();
while(curs.moveToNext())
{
list.add(curs.getString(curs.getColumnIndexOrThrow(CourseEntry.COLUMN_NAME_COURSE_NAME)));
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_spinner_item, list);
courseSpinner.setAdapter(adapter);
这是合同类:
public final class CourseContract {
public CourseContract() {}
public static abstract class CourseEntry implements BaseColumns {
public static final String TABLE_NAME = "courses";
public static final String COLUMN_NAME_COURSE_ID = "courseid";
public static final String COLUMN_NAME_COURSE_NAME = "coursename";
public static final String COLUMN_NAME_YEAR = "year";
public static final String COLUMN_NAME_SEMESTER = "semester";
public static final String COLUMN_NAME_CREDITS = "credits";
public static final String COLUMN_NAME_NULLABLE = "null";
}
}
这是SQLiteHelper类:
public class SQLiteHelperCourse extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "AndroidApplication.db";
public SQLiteHelperCourse(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + CourseEntry.TABLE_NAME + " (" + CourseEntry._ID + " INTEGER PRIMARY KEY, "
+ CourseEntry.COLUMN_NAME_COURSE_ID + TEXT_TYPE + COMMA_SEP
+ CourseEntry.COLUMN_NAME_COURSE_NAME + TEXT_TYPE + COMMA_SEP
+ CourseEntry.COLUMN_NAME_YEAR + TEXT_TYPE + COMMA_SEP
+ CourseEntry.COLUMN_NAME_SEMESTER + TEXT_TYPE + COMMA_SEP
+ CourseEntry.COLUMN_NAME_CREDITS + TEXT_TYPE + ")";
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + CourseEntry.TABLE_NAME;
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}