The program in chap.10 runs great but I do not understand why under click event, mCrime.getId() can realize which crime object it is. Unlike the other 3 fields which is initialized by the itemView attribute inside constructor, mCrime has no initialized at all. I wonder how it is itself being the crime object under click?
Here is the magical part.
private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
private List<Crime> mCrimes;
public CrimeAdapter(List<Crime> crimes) {
mCrimes = crimes;
}
//......
@Override
public void onBindViewHolder(CrimeHolder holder, int position) {
Crime crime = mCrimes.get(position);
holder.bindCrime(crime);
}
//......
}
private abstract class CrimeHolder extends RecyclerView.ViewHolder {
protected Crime mCrime;
//......
public void bindCrime(Crime crime) {
mCrime = crime;
//......
}
}
This is exactly what I am asking. It is a great help to my study.
Thank you very much.