I am able to get the rows to display just fine. I use a CardView for every fifth row to notify the user to call the cops. I want the user to be able to interact with the rows and display a toast with the crime title. Here are the important bits.
private class CrimeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Crime> mCrimes;
public CrimeAdapter(List<Crime> crimes){
mCrimes = crimes;
}
class PoliceHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView mTitleTextView;
private TextView mDateTextView;
private Crime mCrime;
public PoliceHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mTitleTextView = (TextView) itemView.findViewById(R.id.crime_title);
mDateTextView = (TextView) itemView.findViewById(R.id.crime_date);
}
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), mCrime.getTitle(), Toast.LENGTH_SHORT).show();
}
}
class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView mTitleTextView;
private TextView mDateTextView;
private Crime mCrime;
public CrimeHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mTitleTextView = (TextView) itemView.findViewById(R.id.crime_title);
mDateTextView = (TextView) itemView.findViewById(R.id.crime_date);
}
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), mCrime.getTitle(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Crime crime = mCrimes.get(position);
switch (crime.isRequiresPolice()) {
case 0 :
((PoliceHolder) holder).mTitleTextView.setText(crime.getTitle());
((PoliceHolder) holder).mDateTextView.setText(crime.getDate().toString());
break;
case 1 :
((CrimeHolder) holder).mTitleTextView.setText(crime.getTitle());
((CrimeHolder) holder).mDateTextView.setText(crime.getDate().toString());
break;
}
}