Back to project page Android-CriminalIntent.
The source code is released under:
MIT License
If you think the Android project Android-CriminalIntent listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.bignerdranch.android.criminalintent; /* w w w . jav a 2 s.c om*/ import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; import android.hardware.SensorManager; import android.os.Build; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.util.Log; import android.view.ActionMode; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.squareup.seismic.ShakeDetector; import java.util.ArrayList; import java.util.Collections; /** * A fragment representing a list of Items. * <p /> * <p /> * Activities containing this fragment MUST implement the {@link Callbacks} * interface. */ public class CrimeListFragment extends ListFragment implements ShakeDetector.Listener { private static final String TAG = CrimeListFragment.class.getSimpleName(); private ArrayList<Crime> mCrimes; private boolean mSubtitleVisible; /** * Mandatory empty constructor for the fragment manager to instantiate the * fragment (e.g. upon screen orientation changes). */ public CrimeListFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SensorManager sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE); ShakeDetector shakeDetector = new ShakeDetector(this); shakeDetector.start(sensorManager); setHasOptionsMenu(true); getActivity().setTitle(R.string.crimes_title); mCrimes = CrimeLab.get(getActivity()).getCrimes(); CrimeAdapter adapter = new CrimeAdapter(mCrimes); setListAdapter(adapter); setRetainInstance(true); mSubtitleVisible = false; } @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_crime_list, container, false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { if (mSubtitleVisible) { getActivity().getActionBar().setSubtitle(R.string.subtitle); } } ListView listView = (ListView) view.findViewById(android.R.id.list); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { // pre-Honeycomb gets the floating context menu registerForContextMenu(listView); } else { // Honeycomb+ gets the contextual actionBar listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { // Required, but not used in this implementation } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.crime_list_item_context, menu); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { // Required, but not used in this implementation return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case R.id.menu_item_delete_crime: CrimeAdapter adapter = (CrimeAdapter) getListAdapter(); CrimeLab crimeLab = CrimeLab.get(getActivity()); for (int i = adapter.getCount() - 1; i >= 0; i--) { if (getListView().isItemChecked(i)) { crimeLab.delete(adapter.getItem(i)); } } mode.finish(); adapter.notifyDataSetChanged(); return true; default: return false; } } @Override public void onDestroyActionMode(ActionMode mode) { // Required, but not used in this implementation } }); } return view; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.fragment_crime_list, menu); MenuItem showSubtitle = menu.findItem(R.id.menu_item_show_subtitle); if (mSubtitleVisible && showSubtitle != null) { showSubtitle.setTitle(R.string.hide_subtitle); } } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { getActivity().getMenuInflater().inflate(R.menu.crime_list_item_context, menu); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info = (AdapterView.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()).delete(crime); adapter.notifyDataSetChanged(); return true; } return super.onContextItemSelected(item); } @Override public void onResume() { super.onResume(); ((CrimeAdapter)getListAdapter()).notifyDataSetChanged(); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_item_new_crime: Crime crime = new Crime(); CrimeLab.get(getActivity()).add(crime); Intent intent = new Intent(getActivity(), CrimePagerActivity.class); intent.putExtra(CrimeFragment.EXTRA_CRIME_ID, crime.getId()); startActivity(intent); return true; case R.id.menu_item_show_subtitle: if (getActivity().getActionBar().getSubtitle() == null) { getActivity().getActionBar().setSubtitle(R.string.subtitle); mSubtitleVisible = true; item.setTitle(R.string.hide_subtitle); } else { getActivity().getActionBar().setSubtitle(null); mSubtitleVisible = false; item.setTitle(R.string.show_subtitle); } return true; default: return super.onOptionsItemSelected(item); } } @Override public void onListItemClick(ListView l, View v, int position, long id) { Crime c = ((CrimeAdapter)getListAdapter()).getItem(position); Intent i = new Intent(getActivity(), CrimePagerActivity.class); i.putExtra(CrimeFragment.EXTRA_CRIME_ID, c.getId()); startActivity(i); } @Override public void hearShake() { Collections.shuffle(mCrimes); ((CrimeAdapter)getListAdapter()).notifyDataSetChanged(); Context context = ((CrimeAdapter) getListAdapter()).getContext(); Toast.makeText(context, R.string.shake_confirmation, Toast.LENGTH_SHORT).show(); } private class CrimeAdapter extends ArrayAdapter<Crime> { public CrimeAdapter(ArrayList<Crime> crimes) { super(getActivity(), 0, crimes); } @Override public View getView(int position, View convertView, ViewGroup parent) { // if we aren't passed a view to recycle, inflate a new view if (convertView == null) { convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null); } // Configure the view for this Crime Crime c = getItem(position); TextView titleTextView = (TextView) convertView.findViewById(R.id.crime_list_item_titleTextView); TextView dateTextView = (TextView) convertView.findViewById(R.id.crime_list_item_dateTextView); CheckBox solvedCheckBox = (CheckBox) convertView.findViewById(R.id.crime_list_item_solvedCheckBox); titleTextView.setText(c.getTitle()); dateTextView.setText(c.getDate().toString()); solvedCheckBox.setChecked(c.isSolved()); return convertView; } } }