首页 > 解决方案 > 如何从 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 semesteras 参数,但无法弄清楚如何实际使用此参数来仅获取该特定学期的课程。这是我第一次使用数据库,值得注意的是我很难使用它并且还没有掌握它,所以任何帮助都会非常感激,因为我一直被困在这里现在大约2周。

现在,通过使用我目前拥有的代码,如果我在X学期添加课程然后打开Y学期,则在X学期添加的课程也会在Y学期加载。这就是我试图用接收String semesteras 参数的方法来解决的问题。

标签: javaandroidsqliteandroid-sqlite

解决方案


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});

推荐阅读