I'm having an issue updating the correct student object in my database from viewpager2. My viewpager2 has the ability to display the students alphabetical or by grade, the students don't display in the same order they are in the database.
I tried getting the ID from the @Override onBindViewHolder and setting the results to a global int in the adapter but that number doesn't match that of the student's database ID.
private int mDbId;
//Constructor
ViewPagerAdapter(Context context, List<StudentEntity data) {
this.mContext = context;
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
StudentEntity student = mdata.get(position);
mDBId = student.getId();
mFirstName = student.getFirstName();
mLastName = student.getLastName();
mGrade = student.getGrade();
holder.inputFirstName.setText(mFirstName);
...
}
public class ViewHolder extends RecyclerView.ViewHolder {
private Button btnSave;
private TextInputEditText editFirstName;
...
ViewHolder(View itemView) {
super(itemView);
editFirst = itemView.findViewById(R.id.edit_text_first_name);
...
btnSave.setOnClickListener(view -> {
Intent saveIntent = new Intent();
// the mDbId is where I'm having issues.
saveIntent.putExtra("EXTRA_DB_id", mDbId;
saveIntent.putExtra("EXTRA_FIRST_NAME", editFirstName.getText().toString();
((Activity mContext).setResult(RESULT_OK, saveIntent);
// .finish() works as intented
((Activity mContext).finish();
}
}
}