簡體   English   中英

如何從 SQLite Android 數據庫中加載與特定項目關聯的數據?

[英]How to load data associated with a specific item from SQLite Android Database?

在點擊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 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});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM