我一直在开发一个Android应用程序,当我单击回收器视图中的特定行时,我需要获取数据库ID。当我之前编写这个应用程序时,我根据记录的名称更新和删除了记录,但是当我回到这个项目时,我很快就了解到删除或/和更改了具有该名称的所有记录。然后我编写了一个简单的 rawQuery 来使用游标从姓名列表中获取 id。了解到,这将拉出所有具有该名称的 id,但我不知道该选择哪一个(因为该查询会拉出我必须选择的 id 的选项)。
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;
}
}
我也尝试使用 onBindViewHolder 位置,因为这会让我知道我单击的项目的位置。我认为这会从数据库中提取。然而,由于 onBindViewHolder 对我来说是拉取数组 id 列表,所以这不起作用。
我使用了 onBindViewHolder 中的数组 id,使用了 CourseModal,并设置了 id。然后我获取了该 id 并将光标切换为使用数组索引来更改每个记录本身。