如何从 SQLite Android 数据库加载与特定项目关联的数据?
How to load data associated with a specific item from SQLite Android Database?
点击 X 学期时,我希望我的应用程序加载与该特定学期相关联的 classes,我正在尝试使用以下方法执行此操作一段代码:
public void openSemestersActivity() {
final Intent semester = new Intent(this, SemesterActivity.class);
semesterListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// This works if nothing is deleted. If something is deleted we would have to add another +1 to the position
semester.putExtra("semester", db.getSemesterNameString(position + 1));
Log.d("Semester Name", db.getSemesterNameString(position + 1));
startActivity(semester);
}
});
}
现在,我想在 SemesterActivity
上加载与该特定学期关联的 classes,我正在尝试使用以下代码来完成:
// Retrieving the Extra and determining the semester we want to load
Intent myIntent = getIntent();
String semester = myIntent.getStringExtra("semester");
// Creating the Database and loading the ListView
db = new DataBaseHelperC(this);
myCourses.addAll(db.getAllCoursesForThisSemester(semester));
customCourseAdapter = new CourseAdapter(getApplicationContext(), R.layout.course_row, myCourses);
courseListView.setAdapter(customCourseAdapter);
customCourseAdapter.notifyDataSetChanged();
这是我的 DatabaseHelper
class 上的方法,应该可以获取该特定学期的所有课程:
public List<Course> getAllCoursesForThisSemester(String semester) {
List<Course> courses = new ArrayList<>();
// Select all query
String selectQuery = "SELECT * FROM " + Course.TABLE_NAME + " ORDER BY " + Course.COLUMN_ID + " ASC";
SQLiteDatabase db = this.getWritableDatabase();
@SuppressLint("Recycle") Cursor cursor = db.rawQuery(selectQuery, null);
// Looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Course course = new Course();
course.setNameOfCourse(cursor.getString(cursor.getColumnIndex(Course.COLUMN_COURSE)));
course.setCodeOfCourse(cursor.getString(cursor.getColumnIndex(Course.COLUMN_COURSECODE)));
course.setCreditsOfCourse(cursor.getString(cursor.getColumnIndex(Course.COLUMN_COURSECREDITS)));
course.setNameOfBackground(cursor.getString(cursor.getColumnIndex(Course.COLUMN_BACKGROUND)));
course.setId(cursor.getInt(cursor.getColumnIndex(Course.COLUMN_ID)));
courses.add(course);
}
while (cursor.moveToNext());
}
// Close db connection
db.close();
return courses;
}
我正在传递一个 String semester
作为参数,但无法弄清楚如何实际使用此参数来仅获取该特定学期的课程。这是我第一次使用数据库,值得注意的是我遇到了困难并且还没有掌握它,所以任何帮助都会非常感激,因为我一直被困在这里现在大约 2 周。
现在,通过使用我目前拥有的代码,如果我在 X 学期添加一门课程,然后打开 Y 学期, 在 X 学期添加的课程也在 Y 学期加载。这就是我试图用接收 String semester
作为参数的方法修复的问题。
sql语句:
SELECT * FROM " + Course.TABLE_NAME + " ORDER BY " + Course.COLUMN_ID + " ASC"
需要一个 WHERE
子句。
如果包含学期的列的名称类似于 semester
,您可以这样做:
String selectQuery = "SELECT * FROM " + Course.TABLE_NAME +
" WHERE semester = ?" + // replace with the actual column name
" ORDER BY " + Course.COLUMN_ID + " ASC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, new String[] {semester});
点击 X 学期时,我希望我的应用程序加载与该特定学期相关联的 classes,我正在尝试使用以下方法执行此操作一段代码:
public void openSemestersActivity() {
final Intent semester = new Intent(this, SemesterActivity.class);
semesterListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// This works if nothing is deleted. If something is deleted we would have to add another +1 to the position
semester.putExtra("semester", db.getSemesterNameString(position + 1));
Log.d("Semester Name", db.getSemesterNameString(position + 1));
startActivity(semester);
}
});
}
现在,我想在 SemesterActivity
上加载与该特定学期关联的 classes,我正在尝试使用以下代码来完成:
// Retrieving the Extra and determining the semester we want to load
Intent myIntent = getIntent();
String semester = myIntent.getStringExtra("semester");
// Creating the Database and loading the ListView
db = new DataBaseHelperC(this);
myCourses.addAll(db.getAllCoursesForThisSemester(semester));
customCourseAdapter = new CourseAdapter(getApplicationContext(), R.layout.course_row, myCourses);
courseListView.setAdapter(customCourseAdapter);
customCourseAdapter.notifyDataSetChanged();
这是我的 DatabaseHelper
class 上的方法,应该可以获取该特定学期的所有课程:
public List<Course> getAllCoursesForThisSemester(String semester) {
List<Course> courses = new ArrayList<>();
// Select all query
String selectQuery = "SELECT * FROM " + Course.TABLE_NAME + " ORDER BY " + Course.COLUMN_ID + " ASC";
SQLiteDatabase db = this.getWritableDatabase();
@SuppressLint("Recycle") Cursor cursor = db.rawQuery(selectQuery, null);
// Looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Course course = new Course();
course.setNameOfCourse(cursor.getString(cursor.getColumnIndex(Course.COLUMN_COURSE)));
course.setCodeOfCourse(cursor.getString(cursor.getColumnIndex(Course.COLUMN_COURSECODE)));
course.setCreditsOfCourse(cursor.getString(cursor.getColumnIndex(Course.COLUMN_COURSECREDITS)));
course.setNameOfBackground(cursor.getString(cursor.getColumnIndex(Course.COLUMN_BACKGROUND)));
course.setId(cursor.getInt(cursor.getColumnIndex(Course.COLUMN_ID)));
courses.add(course);
}
while (cursor.moveToNext());
}
// Close db connection
db.close();
return courses;
}
我正在传递一个 String semester
作为参数,但无法弄清楚如何实际使用此参数来仅获取该特定学期的课程。这是我第一次使用数据库,值得注意的是我遇到了困难并且还没有掌握它,所以任何帮助都会非常感激,因为我一直被困在这里现在大约 2 周。
现在,通过使用我目前拥有的代码,如果我在 X 学期添加一门课程,然后打开 Y 学期, 在 X 学期添加的课程也在 Y 学期加载。这就是我试图用接收 String semester
作为参数的方法修复的问题。
sql语句:
SELECT * FROM " + Course.TABLE_NAME + " ORDER BY " + Course.COLUMN_ID + " ASC"
需要一个 WHERE
子句。
如果包含学期的列的名称类似于 semester
,您可以这样做:
String selectQuery = "SELECT * FROM " + Course.TABLE_NAME +
" WHERE semester = ?" + // replace with the actual column name
" ORDER BY " + Course.COLUMN_ID + " ASC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, new String[] {semester});