java - 如何从 SQLite Android 数据库加载与特定项目关联的数据?
问题描述
在点击X学期时,我希望我的应用程序加载与该特定学期相关的课程,我正在尝试使用以下代码:
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
,我正在尝试使用以下代码:
// 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
课堂上的方法,它应该获得该特定学期的所有课程:
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
as 参数,但无法弄清楚如何实际使用此参数来仅获取该特定学期的课程。这是我第一次使用数据库,值得注意的是我很难使用它并且还没有掌握它,所以任何帮助都会非常感激,因为我一直被困在这里现在大约2周。
现在,通过使用我目前拥有的代码,如果我在X学期添加课程然后打开Y学期,则在X学期添加的课程也会在Y学期加载。这就是我试图用接收String semester
as 参数的方法来解决的问题。
解决方案
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});
推荐阅读
- webpack - 'mode' 选项没有被设置,webpack 将回退到 'production' 这个值。字段 'browser' 不包含有效的别名配置
- rust - NEP-141 实施
- c - 数组分配的 GCC 编译错误
- webhooks - curl: (7) 无法连接到 localhost 端口 443:连接被拒绝
- c# - 如何使用 xamarin 在 iOS 项目中的 stacklayout 上显示背景图像
- java - 耗电的谷歌 foobar 挑战未能通过第 4 个测试用例
- python - 深度优先搜索的 N 拼图
- postgresql - PostgreSQL - 返回“是我的”布尔值的计算列
- mysql - 每月使用一次查询删除所有超过 30 天的行 MYSQL
- python - 可视化寻路算法