简体   繁体   中英

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

On the click of X semester I want my app to load the classes associated with that specific semester, which I'm trying to do with the following piece of code:

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

Now, I want to load the classes associated with that specific semester on the SemesterActivity , which I'm trying to do with the following code:

// 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(); 

This is the method, located on my DatabaseHelper class, that's supposed to get all the courses for that specific semester:

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

I'm passing a String semester as parameter, but can't figure out how to actually use this parameter to only get the courses for that specific semester. This is my first time working with a Database and it is worth noting that I'm having a hard time with it and haven't gotten the hang of it just yet so any help would be really appreciated since I've been stuck here for around 2 weeks now.

Right now, by using the code that I currently have, if I add a course in X semester and then open Y semester, the courses added on X semester are loaded on the Y semester too. This is what I'm trying to fix with that method that receives a String semester as parameter.

The sql statement:

SELECT * FROM " + Course.TABLE_NAME + " ORDER BY " + Course.COLUMN_ID + " ASC"

needs a WHERE clause.

If the column containing the semester has a name like semester , you can do it like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM