Android Open Source - CriminalIntent Crime List Fragment






From Project

Back to project page CriminalIntent.

License

The source code is released under:

MIT License

If you think the Android project 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.

Java Source Code

package com.ambergleam.criminalintent.controller;
//  ww w. j av  a 2s  .  c  o  m
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.ambergleam.criminalintent.R;
import com.ambergleam.criminalintent.model.Crime;
import com.ambergleam.criminalintent.model.CrimeLab;

public class CrimeListFragment extends ListFragment {

  private static final int REQUEST_CRIME = 1;

  private Callbacks mCallbacks;

  /*
   * Required interface for hosting activities
   */
  public interface Callbacks {
    void onCrimeSelected(Crime crime);
  }

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    mCallbacks = (Callbacks) activity;
  }

  @Override
  public void onDetach() {
    super.onDetach();
    mCallbacks = null;
  }

  public void updateUI() {
    ((CrimeAdapter) getListAdapter()).notifyDataSetChanged();
  }

  private boolean mSubtitleVisible;

  private ArrayList<Crime> mCrimes;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    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(11)
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_crime_list, container, false);

    Button button = (Button) v.findViewById(R.id.addCrimeButton);
    if (button != null) {
      button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          Crime crime = new Crime();
          CrimeLab.get(getActivity()).addCrime(crime);
          Intent i = new Intent(getActivity(), CrimePagerActivity.class);
          i.putExtra(CrimeFragment.EXTRA_CRIME_ID, crime.getId());
          startActivityForResult(i, 0);
        }
      });
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      if (mSubtitleVisible) {
        getActivity().getActionBar().setSubtitle(R.string.subtitle);
      }
    }

    ListView listView = (ListView) v.findViewById(android.R.id.list);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
      // Use floating context menus on Froyo and Gingerbread
      registerForContextMenu(listView);
    } else {
      // Use contextual action bar on Honeycomb and higher
      listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
      listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
          // Required, but not used in this implementation
        }

        // ActionMode.Callback methods

        @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 void onDestroyActionMode(ActionMode mode) {
          // Required, but not used in this implementation
        }

        @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.deleteCrime(adapter.getItem(i));
              }
            }
            mode.finish();
            adapter.notifyDataSetChanged();
            return true;
          default:
            return false;
          }
        }

      });
    }

    return v;
  }

  @Override
  public void onResume() {
    super.onResume();
    ((CrimeAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
    // Get the Crime from the adapter
    Crime c = ((CrimeAdapter) getListAdapter()).getItem(position);
    mCallbacks.onCrimeSelected(c);
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CRIME) {
      // Handle result
    }
  }

  @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);
    }
  }

  @TargetApi(11)
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_item_new_crime:
      Crime crime = new Crime();
      CrimeLab.get(getActivity()).addCrime(crime);
      mCallbacks.onCrimeSelected(crime);
      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);
    }
  }

  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 weren't given a view, inflate one
      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);
      titleTextView.setText(c.getTitle());

      TextView dateTextView = (TextView) convertView.findViewById(R.id.crime_list_item_dateTextView);
      SimpleDateFormat sdf = new SimpleDateFormat(getActivity().getString(R.string.date_formatting), Locale.US);
      String date = sdf.format(c.getDate());
      dateTextView.setText(date);

      CheckBox solvedCheckBox = (CheckBox) convertView.findViewById(R.id.crime_list_item_solvedCheckBox);
      solvedCheckBox.setChecked(c.isSolved());

      return convertView;
    }

  }

  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    getActivity().getMenuInflater().inflate(R.menu.crime_list_item_context, menu);
  }

  @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);
  }

}




Java Source Code List

com.ambergleam.criminalintent.controller.CrimeFragment.java
com.ambergleam.criminalintent.controller.CrimeListActivity.java
com.ambergleam.criminalintent.controller.CrimeListFragment.java
com.ambergleam.criminalintent.controller.CrimePagerActivity.java
com.ambergleam.criminalintent.controller.DatePickerFragment.java
com.ambergleam.criminalintent.helper.CriminalIntentJSONSerializer.java
com.ambergleam.criminalintent.helper.SingleFragmentActivity.java
com.ambergleam.criminalintent.model.CrimeLab.java
com.ambergleam.criminalintent.model.Crime.java