private void updateUI() { // Get the collection of data from the crimelab // singleton. The get method constructor requires that // a context is passed in, so we send it the hosting // activity of this fragment. CrimeLab crimeLab = CrimeLab.get(getActivity()); // Get the actaul list of crimes from the CrimeLab class List<Crime> crimes = crimeLab.getCrimes(); // If the adapter hasn't been created yet, we want to create it and set the Adapter for the // Recycler view. if (mAdapter == null) { // Create a new crimeAdapter and send it over the list // of crimes. Crime adapter needs the list of crimes so // that it can work with the recyclerview to display them. mAdapter = new CrimeAdapter(crimes); // Take the adapter that we just created, and set it as the // adapter that the recycler view is going to use. mCrimeRecyclerView.setAdapter(mAdapter); // else, the adapter already exists so we just need to notify that the data set might have // changed. This will automatically update any data changes for us. } else { mAdapter.notifyDataSetChanged(); } }
@Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); int position = info.position; CrimeAdapter adapter = (CrimeAdapter) getListAdapter(); Crime crime = adapter.getItem(position); switch (item.getItemId()) { case R.id.menu_item_delete_crime: CrimeLab.get(getActivity()).deleteCrime(crime); adapter.notifyDataSetChanged(); return true; } return super.onContextItemSelected(item); }
/** Update the list. */ private void updateUI() { CrimeLab crimeLab = CrimeLab.getInstance(getActivity()); List<Crime> crimes = crimeLab.getCrimes(); if (mAdapter == null) { mAdapter = new CrimeAdapter(crimes); mRecyclerViewCrime.setAdapter(mAdapter); } else { // mCrimes is gone. So the List<Crime> returned by getCrimes() // is a snapshot of the Crimes at one point in time. // we need to update the List<Crime> in the adapter in case user has edited a crime mAdapter.setCrimes(crimes); mAdapter.notifyDataSetChanged(); } // update subtitle after rotation updateSubtitle(); }
/** Updates the Interface with all the crimes in recycler views */ private void updateUI() { CrimeLab crimelab = CrimeLab.get(getActivity()); List<Crime> crimes = crimelab.getCrimes(); if (mAdapter == null) { mAdapter = new CrimeAdapter(crimes); mCrimeRecyclerView.setAdapter(mAdapter); } else { mAdapter.setCrimes(crimes); if (mPreviousAdapterSelected < 0) mAdapter.notifyDataSetChanged(); else { mAdapter.notifyItemChanged(mPreviousAdapterSelected); mPreviousAdapterSelected = -1; } } // make sure number of crimes stays up to date updateSubtitle(); }