@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); Intent i = new Intent(getActivity(), CrimeActivity.class); i.putExtra(CrimeFragment.EXTRA_CRIME_ID, crime.getId()); startActivityForResult(i, 0); 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 onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != Activity.RESULT_OK) { return; } if (requestCode == REQUEST_DATE) { Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE); mCrime.setDate(date); updateDate(); } else if (requestCode == REQUEST_CONTACT && data != null) { Uri contactUri = data.getData(); String[] queryFields = new String[] {ContactsContract.Contacts.DISPLAY_NAME}; Cursor c = getActivity().getContentResolver().query(contactUri, queryFields, null, null, null); try { if (c.getCount() == 0) { return; } c.moveToFirst(); String suspect = c.getString(0); mCrime.setSuspect(suspect); mSuspectButton.setText(suspect); } finally { c.close(); } } if (requestCode == REQUEST_TIME) { Date date = (Date) data.getSerializableExtra(TimePickerFragment.EXTRA_TIME); mCrime.setDate(date); updateTime(); } }
@Override public void onClick(View view) { Toast.makeText(getActivity(), mCrime.getTitle() + " clicked", Toast.LENGTH_SHORT).show(); Intent intent = CrimePagerActivity.newIntent(getActivity(), mCrime.getId()); CrimeListFragment.setCrimePosition(getAdapterPosition()); startActivity(intent); }
@Override public View getView(int position, View convertView, ViewGroup parent) { Crime crime = getItem(position); ViewHolder viewHolder = null; if (convertView == null) { convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null); viewHolder = new ViewHolder(); viewHolder.titleTextView = (TextView) convertView.findViewById(R.id.crime_list_item_titleTextView); viewHolder.dateTextView = (TextView) convertView.findViewById(R.id.crime_list_item_dateTextView); viewHolder.solvedCheckBox = (CheckBox) convertView.findViewById(R.id.crime_list_item_solvedCheckBox); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.titleTextView.setText(crime.getTitle()); viewHolder.dateTextView.setText(crime.getDate().toString()); viewHolder.solvedCheckBox.setChecked(crime.isSolved()); return convertView; }
@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); }
public Crime getCrime(UUID id) { for (Crime c : mCrimes) { if (c.getId().equals(id)) { return c; } } return null; }
public void onListItemClick(ListView l, View v, int position, long id) { // get the Crime from the adapter Crime c = ((CrimeAdapter) getListAdapter()).getItem(position); // start an instance of CrimePagerActivity Intent i = new Intent(getActivity(), CrimePagerActivity.class); i.putExtra(CrimeFragment.EXTRA_CRIME_ID, c.getId()); startActivityForResult(i, 0); }
private void createCrimes() { for (int i = 1; i <= 100; ++i) { Crime c = new Crime(); c.setTitle("Crime #" + i); c.setSolved(i % 2 == 0); mCrimes.add(c); } }
private static ContentValues getContentValues(Crime crime) { ContentValues values = new ContentValues(); values.put(CrimeTable.Cols.UUID, crime.getId().toString()); values.put(CrimeTable.Cols.TITLE, crime.getTitle()); values.put(CrimeTable.Cols.DATE, crime.getDate().getTime()); values.put(CrimeTable.Cols.SOLVED, crime.isSolved() ? 1 : 0); values.put(CrimeTable.Cols.SUSPECT, crime.getSuspect()); return values; }
@Override public void onListItemClick(ListView l, View v, int position, long id) { Crime c = (Crime) (getListAdapter()).getItem(position); Log.d(TAG, c.getTitle() + " was clicked."); // Start CrimePagerActivity - shows detailed info about the crime in a new activity Intent i = new Intent(getActivity(), CrimePagerActivity.class); i.putExtra(CrimeFragment.EXTRA_CRIME_ID, c.getId()); startActivity(i); }
private CrimeLab(Context appContext) { mAppContext = appContext; mCrimes = new ArrayList<Crime>(); for (int i = 0; i < 100; i++) { Crime c = new Crime(); c.setTitle("Crime #" + i); c.setSolved((i % 2) == 0); mCrimes.add(c); } }
// having a context allows the singleton to start activities access resources etc private CrimeLab(Context appContext) { mAppContext = appContext; mCrimes = new ArrayList<Crime>(); // creates 100 crimes for the arraylist model layer for (int i = 0; i < 100; i++) { Crime c = new Crime(); c.setTitle("Crime #" + i); c.setSolved(i % 2 == 0); // every other one mCrimes.add(c); } }
private MoneyLab(Context AppContext) { mAppContext = AppContext; mCrimes = new ArrayList<Crime>(); for (int i = 0; i < 5; i++) { Crime c = new Crime(); c.setTitle_istochnik_dohoda("Источник дохода" + i); c.setTitle_summa_dohoda("Сумма дохода" + i); mCrimes.add(c); } }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != Activity.RESULT_OK) { return; } else if (requestCode == REQUEST_DATE) { Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE); mCrime.setDate(date); mCallbacks.onCrimeUpdated(mCrime); updateDate(); } else if (requestCode == REQUEST_TIME) { Date date = (Date) data.getSerializableExtra(TimePickerFragment.EXTRA_TIME); mCrime.setDate(date); updateTime(); } else if (requestCode == REQUEST_PHOTO) { // Create a new Photo object and attach it to the crime String filename = data.getStringExtra(CrimeCameraFragment.EXTRA_PHOTO_FILENAME); if (filename != null) { Photo p = new Photo(filename); mCrime.setPhoto(p); mCallbacks.onCrimeUpdated(mCrime); showPhoto(); } } else if (requestCode == REQUEST_CONTACT) { Uri contactUri = data.getData(); // Specify which fields you want your quwery to return // values for. String[] queryFields = new String[] {ContactsContract.Contacts.DISPLAY_NAME}; // Perform your query - the contactUri is like a "where" // clause here Cursor c = getActivity().getContentResolver().query(contactUri, queryFields, null, null, null); // Double check that you actually got results if (c.getCount() == 0) { c.close(); return; } // Pull out the first column of the first row of data - // that is your suspect's name. c.moveToFirst(); String suspect = c.getString(0); mCrime.setSuspect(suspect); mCallbacks.onCrimeUpdated(mCrime); mSuspectButton.setText(suspect); c.close(); } }
private void updateTime() { // TODO KIO Is this really necessary to creat a calendar? // Calendar calendar = Calendar.getInstance(); // calendar.setTime(mCrime.getDate()); Log.d(TAG_KIO, "Inside updateTime() mDate time is: " + mCrime.getDate().getTime()); Log.d(TAG_KIO, "Inside updateTime() mDate is: " + mCrime.getDate()); Time time = new Time(); time.set(mCrime.getDate().getTime()); String timeFormat = time.format("%I:%M"); Log.d(TAG_KIO, "timeFormat is: " + timeFormat); mTimeButton.setText(timeFormat); }
// 读取Crime数组列表中的数据,进行数据格式的转换,然后写JSON文件 public void saveCrimes(ArrayList<Crime> crimes) throws JSONException, IOException { JSONArray array = new JSONArray(); for (Crime crime : crimes) { array.put(crime.toJSON()); } Writer writer = null; try { OutputStream out = mContext.openFileOutput(mFilename, Context.MODE_PRIVATE); writer = new OutputStreamWriter(out); writer.write(array.toString()); } finally { // TODO: handle exception if (writer != null) writer.close(); } }
public void saveCrimes(ArrayList<Crime> crimes) throws JSONException, IOException { // Build an array in JSON JSONArray array = new JSONArray(); for (Crime c : crimes) array.put(c.toJSON()); // Write the file to disk Writer writer = null; try { OutputStream out = mContext.openFileOutput(mFilename, Context.MODE_PRIVATE); writer = new OutputStreamWriter(out); writer.write(array.toString()); } finally { if (writer != null) writer.close(); } }
public void updateCrime(Crime crime) { String uuidString = crime.getId().toString(); ContentValues values = getContentValues(crime); mDatabase.update( CrimeTable.NAME, values, CrimeTable.Cols.UUID + " = ?", new String[] {uuidString}); }
public void onCrimeSelected(Crime crime) { if (findViewById(R.id.detailFragmentContainer) == null) { Intent i = new Intent(this, CrimePagerActivity.class); i.putExtra(CrimeFragment.EXTRA_CRIME_ID, crime.getmId()); startActivity(i); } else { FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); Fragment oldDetail = fm.findFragmentById(R.id.detailFragmentContainer); Fragment newDetail = CrimeFragment.newInstance(crime.getmId()); if (oldDetail != null) { ft.remove(oldDetail); } ft.add(R.id.detailFragmentContainer, newDetail); ft.commit(); } }
// This method must be implemented because we have this class // implementing the onclicklistener interface. This method will // do the work toasting the title of the crime that was clicked on. @Override public void onClick(View view) { // Ask CrimeActivity for an intent that will get the CrimeActivity started. The method // requires us to pass the Context, which we get from calling getActivity() and the id // of the crime we want to start the activity with. Once we have the intent, we call // startActivity to start it. Intent intent = CrimeActivity.newIntent(getActivity(), mCrime.getId()); startActivity(intent); }
/** * it verifies that there is external storage to save them to. if there is no external storage, * return null. it does not create any files on the filesystem. * * @param crime * @return */ public File getPhotoFile(Crime crime) { File externalFilesDir = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES); if (externalFilesDir == null) { return null; } return new File(externalFilesDir, crime.getPhotoFilename()); }
@Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null); } 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); dateTextView.setText(c.getDate().toString()); CheckBox solvedCheckBox = (CheckBox) convertView.findViewById(R.id.crime_list_item_solvedCheckBox); solvedCheckBox.setChecked(c.isSolved()); return convertView; }
private String getCrimeReport() { String solvedString = null; if (mCrime.isSolved()) { solvedString = getString(R.string.crime_report_solved); } else { solvedString = getString(R.string.crime_report_unsolved); } String dateFormat = "EEE, MMM dd"; String dateString = DateFormat.format(dateFormat, mCrime.getDate()).toString(); String suspect = mCrime.getSuspect(); if (suspect == null) { suspect = getString(R.string.crime_report_no_suspect); } else { suspect = getString(R.string.crime_report_suspect, suspect); } String report = getString(R.string.crime_report, mCrime.getTitle(), dateString, solvedString, suspect); return report; }
private void showPhoto() { // (Re)set the image button's image based on our photo Photo p = mCrime.getPhoto(); BitmapDrawable b = null; Log.d(TAG, "Inside showPhoto"); if (p != null) { String path = getActivity().getFileStreamPath(p.getFilename()).getAbsolutePath(); b = PictureUtils.getScaledDrawable(getActivity(), path); } mPhotoView.setImageDrawable(b); }
@Override public View getView(int position, View convertView, ViewGroup parent) { // if we weren't given a view, inflate one if (null == convertView) { 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); dateTextView.setText(c.getDate().toString()); CheckBox solvedCheckBox = (CheckBox) convertView.findViewById(R.id.crime_list_item_solvedCheckBox); solvedCheckBox.setChecked(c.isSolved()); return convertView; }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_crime, parent, false); Log.d("Mincho", mCrime.mTitle == null ? "Title is null" : "Title is not null"); // Log.d("Mincho", mCrime.mDate == null ? "Date is null" : "Date is not null"); // Log.d("Mincho", mCrime.mId == null ? "Id is null" : "Id is not null"); // Log.d("Mincho", mCrime.mSolved == null ? "solved is null" : "solved is not null"); mTitleField = (EditText) v.findViewById(R.id.crime_title); mTitleField.setText(mCrime.getmTitle()); mTitleField.addTextChangedListener( new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { mCrime.setmTitle(s.toString()); } @Override public void afterTextChanged(Editable s) {} }); // android.text.format.DateFormat.format("yyy-mm-dd", new java.util.Date()); mDateButton = (Button) v.findViewById(R.id.crime_date); mDateButton.setText(mCrime.getmDate().toString()); mDateButton.setEnabled(false); mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved); mSolvedCheckBox.setChecked(mCrime.ismSolved()); mSolvedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mCrime.setmSolved(isChecked); } }); return v; }
public Crime getCrime(UUID crimeId) { for (Crime c : mCrimes) if (c.getId().equals(crimeId)) return c; return new Crime(); }
@TargetApi(11) @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstance) { // Get view for fragment View v = inflater.inflate(R.layout.fragment_crime, parent, false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { if (NavUtils.getParentActivityName(getActivity()) != null) { getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); } } // Crime Title mTitleField = (EditText) v.findViewById(R.id.crime_title); mTitleField.setText(mCrime.getTitle()); mTitleField.addTextChangedListener( new TextWatcher() { public void onTextChanged(CharSequence c, int start, int before, int count) { mCrime.setTitle(c.toString()); mCallbacks.onCrimeUpdated(mCrime); } public void beforeTextChanged(CharSequence c, int start, int count, int after) { // Do something? } public void afterTextChanged(Editable c) { // Do something? } }); Log.d("KIO", "Crime title is: " + mTitleField); // Date Button mDateButton = (Button) v.findViewById(R.id.crime_date); updateDate(); mDateButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = getActivity().getSupportFragmentManager(); DatePickerFragment dialog = DatePickerFragment.newInstance(mCrime.getDate()); dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE); dialog.show(fm, DIALOG_DATE); } }); Log.d("KIO", "Date is: " + mDateButton); // Time Button mTimeButton = (Button) v.findViewById(R.id.crime_time); Log.d("KIO", "Time is: " + mTimeButton); updateTime(); mTimeButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = getActivity().getSupportFragmentManager(); TimePickerFragment dialog = TimePickerFragment.newInstance(mCrime.getDate()); dialog.setTargetFragment(CrimeFragment.this, REQUEST_TIME); dialog.show(fm, DIALOG_TIME); } }); Log.d("KIO", "Time is: " + mTimeButton); // "Solved" Check box mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved); mSolvedCheckBox.setChecked(mCrime.isSolved()); mSolvedCheckBox.setOnCheckedChangeListener( new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // Set the crime's solved property mCrime.setSolved(isChecked); mCallbacks.onCrimeUpdated(mCrime); } }); // Photo Button mPhotoButton = (ImageButton) v.findViewById(R.id.crime_imageButton); mPhotoButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(getActivity(), CrimeCameraActivity.class); startActivityForResult(i, REQUEST_PHOTO); } }); // If a camera is not available, disable the camera functionality PackageManager pm = getActivity().getPackageManager(); boolean hasACamera = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) || pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && Camera.getNumberOfCameras() > 0); if (!hasACamera) { mPhotoButton.setEnabled(false); } // Photographic Evidence mPhotoView = (ImageView) v.findViewById(R.id.crime_imageView); mPhotoView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Photo p = mCrime.getPhoto(); if (p == null) { return; } FragmentManager fm = getActivity().getSupportFragmentManager(); String path = getActivity().getFileStreamPath(p.getFilename()).getAbsolutePath(); ImageFragment.newInstance(path).show(fm, DIALOG_IMAGE); } }); // Crime Report Button reportButton = (Button) v.findViewById(R.id.crime_reportButton); reportButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(Intent.EXTRA_TEXT, getCrimeReport()); i.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.crime_report_subject)); // Always show the chooser, even if the user has set a default app i = Intent.createChooser(i, getString(R.string.send_report)); startActivity(i); } }); // Suspect mSuspectButton = (Button) v.findViewById(R.id.crime_suspectButton); mSuspectButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(i, REQUEST_CONTACT); } }); if (mCrime.getSuspect() != null) { mSuspectButton.setText(mCrime.getSuspect()); } return v; }
@Override public void onListItemClick(ListView l, View v, int position, long id) { // Crime c = (Crime)getListAdapter().getItem(position); Crime c = ((CrimeAdapter) getListAdapter()).getItem(position); Log.d(TAG, c.getmTilte() + "was clicked"); }
private void updateDate() { String formatDate = DateFormat.format("EEEE, MMM dd, yyyy", mCrime.getDate()).toString(); mDateButton.setText(formatDate); }