Search code examples
javaandroidandroid-recyclerviewandroid-database

How to retrieve database id when clicking recycler view in android studio?


I've been working on an android app, and I need to get the database id when I click on specific row in the recycler view. When I wrote this app previously I updated and deleted the records based on their names, however when I came back to this project I learned pretty quick that deleted or/and changed all the records with that name. Then I wrote a simple rawQuery to get the id from a name list, using cursor. Learned that, that would pull all the ids with that name but I didnt know which one to choose, (since that query would pull options for the ids I had to choose).

 public Cursor getDbId(String studentName) {

        SQLiteDatabase db = this.getWritableDatabase();

        Cursor getDb = (db.rawQuery("SELECT " + ID_COL + " FROM " + TABLE_NAME + " WHERE " + NAME_COL + " = ?", new String[]{studentName}));
        getDb.moveToFirst();
        return getDb;
    }
 updateCourseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (cursor != null && cursor.moveToNext()) {
                    String str = cursor.getString(cursor.getColumnIndex("id"));


                    System.out.println(str);
                    dbHandler.updateCourse(str, studentNameUpd.getText().toString(), studentSubjectUpd.getText().toString(), studentHoursUpd.getText().toString(), studentCoreUpd.getText().toString());

                    // displaying a toast message that our course has been updated.
                    Toast.makeText(UpdateCourseActivity.this, "Course Updated..", Toast.LENGTH_SHORT).show();

                    // launching our main activity.
                    Intent i = new Intent(UpdateCourseActivity.this, MainActivity.class);
                    startActivity(i);
                }
                /* inside this method we are calling an update course
                   method and passing all our edit text values. */

            }
        });
@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

//        Cursor cursor = dbHandler.getDbId(studentName);
        /* on below line we are setting data
           to our views of recycler view item. */
        CourseModal modal = courseModalArrayList.get(position);

        holder.studentNameRV.setText(modal.getStudentName());
        holder.studentCoreRV.setText(modal.getStudentCore());
        holder.studentSubjectRV.setText(modal.getStudentSubject());
        holder.studentHoursRV.setText(modal.getStudentHours());



        // below line is to add on click listener for our recycler view item.
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                System.out.println();

;
                // on below line we are calling an intent.
                Intent i = new Intent(context, UpdateCourseActivity.class);

                // below we are passing all our values.
                i.putExtra("name", modal.getStudentName());
                i.putExtra("core", modal.getStudentCore());
                i.putExtra("subject", modal.getStudentSubject());
                i.putExtra("hours", modal.getStudentHours());


                // starting our activity.
                context.startActivity(i);
            }
        });
    }
public class CourseModal {

/* variables for our studentName,
   description, tracks and duration, id. */
private String studentName;
private String studentSubject;
private String studentHours;
private String studentCore;

public String getStudentName() {
    return studentName;
}

public void setStudentName(String studentName) {
    this.studentName = studentName;
}

public String getStudentSubject() {
    return studentSubject;
}

public void setStudentSubject(String studentSubject) {
    this.studentSubject = studentSubject;
}

public String getStudentHours() {
    return studentHours;
}

public void setStudentHours(String studentHours) {
    this.studentHours = studentHours;
}

public String getStudentCore() {
    return studentCore;
}

public void setStudentCore(String studentCore) {
    this.studentCore = studentCore;
}

private int id;
int o = 0;
int i = 0;

// creating getter and setter methods



public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

// constructor
public CourseModal(String studentName, String studentSubject, String studentHours, String studentCore) {
    this.studentName = studentName;
    this.studentSubject = studentSubject;
    this.studentHours = studentHours;
    this.studentCore = studentCore;
}

}

I also did try to use the onBindViewHolder position since that would get me a position of what item i clicked. I thought that would pull from the database. However since onBindViewHolder is for me pulling an array id list, that didn't work.


Solution

  • I used the array id from onBindViewHolder, used CourseModal, and set the id. Then I got that id and switched cursor to use array indexing to change each record as themselves.