Chapter 10 - Challenge


#1

I did the challenge without startActivityForResult, I’ve seen some solutions with this approach but I think the result is a value already known by the fragment (the clicked position) is not something created by the crime activity, therefore I saved the last clicked position in the fragment from the ChrimeHolder#onClick callback. Is there a downside with my solution?

public class CrimeListFragment extends Fragment {

    private RecyclerView crimeRecyclerView;
    private CrimeAdapter crimeAdapter;
    private int lastPositionClicked;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_crime_list, container, false);

        crimeRecyclerView = view.findViewById(R.id.crime_recycler_view);
        crimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        updateUI();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        updateUI();
    }

    private void updateUI() {
        Collection<Crime> crimes = CrimeLab.INSTANCE.getCrimes();
        if (crimeAdapter == null) {
            crimeAdapter = new CrimeAdapter(crimes);
            crimeRecyclerView.setAdapter(crimeAdapter);
        } else {
            crimeAdapter.notifyItemChanged(lastPositionClicked);
        }
    }

    private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        int position;
        private Crime crime;
        private TextView txtViewTitle;
        private TextView txtViewDate;
        private ImageView imgViewCrimeSolved;

        public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_item_crime, parent, false));
            itemView.setOnClickListener(this);

            txtViewTitle = itemView.findViewById(R.id.crime_title);
            txtViewDate = itemView.findViewById(R.id.crime_date);
            imgViewCrimeSolved = itemView.findViewById(R.id.crime_solved);
        }

        public void bind(Crime crime, int position) {
            this.crime = crime;
            this.position = position;
            txtViewTitle.setText(crime.getTitle());
            txtViewDate.setText(DateFormat.format("dd/MM/yyyy", crime.getDate()));
            imgViewCrimeSolved.setVisibility(crime.isSolved() ? View.VISIBLE : View.GONE);
        }

        @Override
        public void onClick(View view) {
            CrimeListFragment.this.lastPositionClicked = position;
            startActivity(CrimeActivity.newIntent(getActivity(), crime.getId()));
        }
    }

    private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {

        private List<Crime> crimes;

        public CrimeAdapter(Collection<Crime> crimes) {
            this.crimes = new ArrayList<>(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) {
            holder.bind(crimes.get(position), position);
        }

        @Override
        public int getItemCount() {
            return crimes.size();
        }
    }

}