我正在创建一个应用程序来组织学校课程并为每个班级输入作业。单击列表视图中的课程时,应显示该特定课程分配的另一个布局。我将如何保存每门课程的作业,以便在选择另一门课程时,使用如下数据库显示特定课程作业?
提前致谢。
public class CourseTable {
// Database table
public static final String TABLE_COURSE = "course";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAMECOURSE = "namecourse";
public static final String COLUMN_DAYOFCOURSE = "dayofcourse";
public static final String COLUMN_TIMEOFCOURSE = "timeofcourse";
// Database creation SQL statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_COURSE
+ "("
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_NAMECOURSE + " text not null, "
+ COLUMN_DAYOFCOURSE + " text not null,"
+ COLUMN_TIMEOFCOURSE + " text not null,"
+ ");";
public static void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
public static void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
Log.w(CourseTable.class.getName(), "Upgrading database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS " + TABLE_COURSE);
onCreate(database);
}
内容提供商:
public class CourseContentProvider extends ContentProvider {
// database
private CourseDBHelper database;
// Used for the UriMacher
private static final int COURSES = 10;
private static final int COURSE_ID = 20;
private static final String AUTHORITY = "entcproject.organizer.tamu.main.contentprovider";
private static final String BASE_PATH = "courses";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/" + BASE_PATH);
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
+ "/courses";
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
+ "/course";
private static final UriMatcher sURIMatcher = new UriMatcher(
UriMatcher.NO_MATCH);
static {
sURIMatcher.addURI(AUTHORITY, BASE_PATH, COURSES);
sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", COURSE_ID);
}
@Override
public boolean onCreate() {
database = new CourseDBHelper(getContext());
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
checkColumns(projection);
queryBuilder.setTables(CourseTable.TABLE_COURSE);
int uriType = sURIMatcher.match(uri);
switch (uriType) {
case COURSES:
break;
case COURSE_ID:
queryBuilder.appendWhere(CourseTable.COLUMN_ID + "="
+ uri.getLastPathSegment());
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
SQLiteDatabase db2 = database.getWritableDatabase();
Cursor cursor = queryBuilder.query(db2, projection, selection,
selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
int uriType = sURIMatcher.match(uri);
SQLiteDatabase sqlDB = database.getWritableDatabase();
int rowsDeleted = 0;
long id = 0;
switch (uriType) {
case COURSES:
id = sqlDB.insert(CourseTable.TABLE_COURSE, null, values);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return Uri.parse(BASE_PATH + "/" + id);
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int uriType = sURIMatcher.match(uri);
SQLiteDatabase sqlDB = database.getWritableDatabase();
int rowsUpdated = 0;
switch (uriType) {
case COURSES:
rowsUpdated = sqlDB.update(CourseTable.TABLE_COURSE,
values,
selection,
selectionArgs);
break;
case COURSE_ID:
String id = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {
rowsUpdated = sqlDB.update(CourseTable.TABLE_COURSE,
values,
CourseTable.COLUMN_ID + "=" + id,
null);
} else {
rowsUpdated = sqlDB.update(CourseTable.TABLE_COURSE,
values,
CourseTable.COLUMN_ID + "=" + id
+ " and "
+ selection,
selectionArgs);
}
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsUpdated;
}
private void checkColumns(String[] projection) {
String[] available = { CourseTable.COLUMN_NAMECOURSE,
CourseTable.COLUMN_DAYOFCOURSE, CourseTable.COLUMN_TIMEOFCOURSE,
CourseTable.COLUMN_ID };
if (projection != null) {
HashSet<String> requestedColumns = new HashSet<String>(
Arrays.asList(projection));
HashSet<String> availableColumns = new HashSet<String>(
Arrays.asList(available));
// Check if all columns which are requested are available
if (!availableColumns.containsAll(requestedColumns)) {
throw new IllegalArgumentException(
"Unknown columns in projection");
}
}
}
}