mViewPager.setCurrentItem() not working

mViewPager.setCurrentItem() is not working, it always loads the first crime on the list rather than the one which I click on. Everything else works normally. I am on page 288 of the book.

Code:

CrimePagerActivity:

public static Intent newIntent(Context packageContext, UUID crimeId){
Intent intent = new Intent(packageContext, CrimePagerActivity.class);
intent.putExtra(EXTRA_CRIME_ID, crimeId);
return intent;
}

mCurrentItem = mCrimes.indexOf(CrimeLab.get(this).getCrime(crimeID));
mViewPager.setCurrentItem(mCurrentItem);

CrimeListFragment:

// CrimeHolder for normal crimes. It will store itemviews.
private class AbstractCrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private TextView mTitleTextView, mDateTextView;
private Crime mCrime;

//Crime holder for normal crimes
public AbstractCrimeHolder(int layoutId, LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(layoutId, parent, false));
itemView.setOnClickListener(this);

mTitleTextView = (TextView) itemView.findViewById(R.id.crime_title);
mDateTextView = (TextView) itemView.findViewById(R.id.crime_date);

if(layoutId==R.layout.list_item_crime_serious) {
Button mContactPoliceButton = itemView.findViewById(R.id.contact_police);
mContactPoliceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), mCrime.getTitle() + " Police Contacted!", Toast.LENGTH_SHORT).show();

}
});
}
}

//Bind method will bind the names and dates to each RecyclerView itemView.
public void Bind(Crime crime) {
mCrime = crime;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDisplayDate());

}
@Override
public void onClick(View v) {
Intent intent = CrimePagerActivity.newIntent(getActivity(),mCrime.getId());
mLayoutPosition.add(getLayoutPosition());
startActivity(intent);

}
}

CrimeLab:

//Getting crimes from the cursor
public List<Crime> getCrimes() {
List<Crime> crimes = new ArrayList<>();

CrimeCursorWrapper cursor = queryCrimes(null, null);

try {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
crimes.add(cursor.getCrime());
cursor.moveToNext();
}
} finally {
cursor.close();
}
return crimes;
}

public Crime getCrime(UUID id) {

CrimeCursorWrapper cursor = queryCrimes(CrimeTable.COLS.UUID + " = ?", new String[]{id.toString()});

try {
if (cursor.getCount() == 0) return null;

cursor.moveToFirst();
return cursor.getCrime();

} finally {
cursor.close();
}
}
private CrimeCursorWrapper queryCrimes(String whereClause, String[] whereArgs) {

Cursor cursor = mDatabase.query(
CrimeTable.NAME,
null,
whereClause,
whereArgs,
null,
null,
null
);

return new CrimeCursorWrapper(cursor);
}

public void updateCrime(Crime crime) {
String uuiDString = crime.getId().toString();
ContentValues values = getContentValues(crime);

mDatabase.update(CrimeTable.NAME, values, CrimeTable.COLS.UUID + " = ?", new String[]{uuiDString});

}

private static ContentValues getContentValues(Crime crime) {
ContentValues values = new ContentValues();
values.put(CrimeTable.COLS.UUID, crime.getId().toString());
values.put(CrimeTable.COLS.TITLE, crime.getTitle());
values.put(CrimeTable.COLS.DATE, crime.getDate().getTime());
values.put(CrimeTable.COLS.SOLVED, crime.isSolved() ? 1 : 0);
return values;
}

CrimeCursorWrapper

public class CrimeCursorWrapper extends CursorWrapper {

public CrimeCursorWrapper(Cursor cursor) {
super(cursor);
}

public Crime getCrime(){

String uuidString = getString(getColumnIndex(CrimeTable.COLS.UUID));
String title = getString(getColumnIndex(CrimeTable.COLS.TITLE));
long date = getLong(getColumnIndex(CrimeTable.COLS.DATE));
int isSolved = getInt(getColumnIndex(CrimeTable.COLS.SOLVED));

Crime crime = new Crime(UUID.fromString(uuidString));
crime.setTitle(title);
crime.setDate(new Date(date));
crime.setSolved(isSolved != 0);

return crime;
}

}

After doing some debugging it seems like List.indexOf() is always returning -1 no matter which crime I pass in. Still trying to figure out what’s causing this.

List.IndexOf() was always returning -1 when I passed in any Crime object argument despite it existing in the list. I created a workaround which solved the problem. I created a custom method in CrimeLab which fetches the index of the crime:

    public int getIndexOf(Crime crime) {

    List<Crime> mCrimes = getCrimes();
    for (Crime c : mCrimes) {
        if (crime.getId().toString().equals(c.getId().toString())) {
            return mCrimes.indexOf(c);
        }
    }

    return -1;
}

This solved the problem.