My CimeListFragment class
public class CrimeListFragment extends Fragment {
private RecyclerView mRecyclerview;
CrimeAdapter mAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_crime_list,container,false);
mRecyclerview = (RecyclerView) view.findViewById(R.id.crime_recycler_view);
mRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
private void updateUI() {
CrimeLab crimeLab = CrimeLab.get(getActivity());
List<Crime> crimes = crimeLab.getCrimes();
mAdapter = new CrimeAdapter(crimes);
mRecyclerview.setAdapter(mAdapter);
}
private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView mTextview;
private TextView mDateTextView;
private Button mcontactPoliceButton;
public CrimeHolder(LayoutInflater inflater,ViewGroup parent){
super(inflater.inflate(R.layout.list_item_crime,parent,false));
mTextview = (TextView) itemView.findViewById(R.id.title_item_view);
mDateTextView = (TextView) itemView.findViewById(R.id.date_item_view);
mcontactPoliceButton = (Button) itemView.findViewById(R.id.contact_police);
itemView.setOnClickListener(this);
}
private void bind(Crime crime){
mTextview.setText(crime.getmTitle());
mDateTextView.setText(crime.getMdate().toString());
if(crime.isMrequirePolice()){
mcontactPoliceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"contqacting police",Toast.LENGTH_SHORT).show();
}
});
}
else
mcontactPoliceButton.setVisibility(View.GONE);
}
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"clicked an itemView",Toast.LENGTH_SHORT).show();
}
}
private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>{
private List<Crime> mCrimes;
public CrimeAdapter(List<Crime> crimes){
mCrimes = crimes;
}
@Override
public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
return new CrimeHolder(inflater,parent);
}
@Override
public void onBindViewHolder(CrimeHolder holder, int position) {
Crime crime = mCrimes.get(position);
holder.bind(crime);
}
@Override
public int getItemCount() {
return mCrimes.size();
}
}
}