In my app, I'm using an activity to add new courses and save them in the SQLite database. But when I try to access the added courses in an other activity to show them in an Spinner, the table seems to empty.
This is my code to save a course (first activity):
SQLiteHelperCourse courseDbHelper = new SQLiteHelperCourse(this);
SQLiteDatabase dbw = courseDbHelper.getWritableDatabase();
String courseNameString, yearString, semesterString, creditsString;
AlertCourseError courseAlert = new AlertCourseError();
if(v.getId() == R.id.btnSaveCourse)
{
if(!etCourseName.getText().toString().matches("") || !etCredits.getText().toString().matches(""))
{
courseNameString = etCourseName.getText().toString();
yearString = yearSpinner.getSelectedItem().toString();
semesterString = semesterSpinner.getSelectedItem().toString();
creditsString = etCredits.getText().toString();
ContentValues values = new ContentValues();
values.put(CourseEntry.COLUMN_NAME_COURSE_NAME, courseNameString);
values.put(CourseEntry.COLUMN_NAME_YEAR, yearString);
values.put(CourseEntry.COLUMN_NAME_SEMESTER, semesterString);
values.put(CourseEntry.COLUMN_NAME_CREDITS, creditsString);
dbw.insert(CourseEntry.TABLE_NAME, CourseEntry.COLUMN_NAME_NULLABLE, values);
}
}
This is the code to show the saved courses in a Spinner (second activity)
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);
Aucun commentaire:
Enregistrer un commentaire