@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); crime.setDate(date); updateDate(); } }
@Nullable @Override @TargetApi(11) public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_crime, container, false); titleField = (EditText) v.findViewById(R.id.crime_title); titleField.setText(crime.getTitle()); titleField.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) { crime.setTitle(s.toString()); } @Override public void afterTextChanged(Editable s) {} }); dateButton = (Button) v.findViewById(R.id.crime_date); updateDate(); dateButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = getActivity().getSupportFragmentManager(); DatePickerFragment dialog = DatePickerFragment.newInstance(crime.getDate()); dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE); dialog.show(fm, DIALOG_DATE); } }); solvedCheckbox = (CheckBox) v.findViewById(R.id.crime_solved); solvedCheckbox.setChecked(crime.isSolved()); solvedCheckbox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { crime.setSolved(isChecked); } }); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { if (NavUtils.getParentActivityName(getActivity()) != null) { ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); } } return v; }
@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.setmDate(date); updateCrime(); updateDate(); } else if (requestCode == REQUEST_CONTACT && data != null) { Uri contactUri = data.getData(); // Specify which fields you want your query to return // values for. String[] queryFields = new String[] {ContactsContract.Contacts.DISPLAY_NAME}; // Perform your query - the contactUri is like a "where" // clause here. Log.i(TAG, "onActivityResult(): "); Log.i(TAG, "contactUri = " + contactUri + ", " + "queryFields = " + queryFields[0]); Cursor c = getActivity().getContentResolver().query(contactUri, queryFields, null, null, null); try { // Double-check that you actually got the results if (c.getCount() == 0) { 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); updateCrime(); mSuspectButton.setText(suspect); } finally { c.close(); } } else if (requestCode == REQUEST_PHOTO) { updateCrime(); updatePhotoView(); } }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_crime, container, false); mTitleField = ((EditText) v.findViewById(R.id.crime_title)); mTitleField.setText(mCrime.getTitle()); 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.setTitle(s.toString()); } @Override public void afterTextChanged(Editable s) {} }); mDateButton = ((Button) v.findViewById(R.id.crime_date)); updateDate(); mDateButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); DatePickerFragment dialog = DatePickerFragment.newInstance(mCrime.getDate()); dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE); dialog.show(fragmentManager, DIALOG_DATE); } }); mSolvedCheckBox = ((CheckBox) v.findViewById(R.id.crime_solved)); mSolvedCheckBox.setChecked(mCrime.isSolved()); mSolvedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mCrime.setSolved(isChecked); } }); return v; }
@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_TIME) { Date date = (Date) data.getSerializableExtra(TimePickerFragment.EXTRA_TIME); mCrime.setTime(date); updateTime(); } 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(); } } else if (requestCode == REQUEST_PHOTO) { updatePhotoView(); } }
@Override @TargetApi(11) public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_crime, parent, false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); } 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()); } public void beforeTextChanged(CharSequence c, int start, int count, int after) { // this space intentionally left blank } public void afterTextChanged(Editable c) { // this one too } }); mDateButton = (Button) v.findViewById(R.id.crime_date); updateDate(); mDateButton.setOnClickListener( new View.OnClickListener() { 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); } }); 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); } }); mPhotoButton = (ImageButton) v.findViewById(R.id.crime_imageButton); mPhotoButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { // launch the camera activity Intent i = new Intent(getActivity(), CrimeCameraActivity.class); startActivity(i); } }); // if camera is not available, disable camera functionality PackageManager pm = getActivity().getPackageManager(); if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) && !pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) { mPhotoButton.setEnabled(false); } return v; }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_crime, container, false); mTitleField = (EditText) v.findViewById(R.id.crime_title); mTitleField.setText(mCrime.getTitle()); mTitleField.addTextChangedListener( new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // intentional blank } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { mCrime.setTitle(s.toString()); } @Override public void afterTextChanged(Editable s) { // this one too } }); mDateButton = (Button) v.findViewById(R.id.crime_date); updateDate(); mDateButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager manager = getFragmentManager(); DatePickerFragment dialog = DatePickerFragment.newInstance(mCrime.getDate()); dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE); dialog.show(manager, DIALOG_DATE); } }); mTimeButton = (Button) v.findViewById(R.id.crime_time); updateTime(); mTimeButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager manager = getFragmentManager(); TimePickerFragment dialog = TimePickerFragment.newInstance(mCrime.getTime()); dialog.setTargetFragment(CrimeFragment.this, REQUEST_TIME); dialog.show(manager, DIALOG_TIME); } }); mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved); mSolvedCheckBox.setChecked(mCrime.isSolved()); mSolvedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // set the crime's solved property mCrime.setSolved(isChecked); } }); mDeleteButton = (Button) v.findViewById(R.id.delete_crime); mDeleteButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // CrimeLab.get(getActivity()).removeCrime(mCrime); getActivity().finish(); } }); mReportButton = (Button) v.findViewById(R.id.crime_report); mReportButton.setOnClickListener( new View.OnClickListener() { 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)); i = Intent.createChooser(i, getString(R.string.send_report)); startActivity(i); } }); final Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); mSuspectButton = (Button) v.findViewById(R.id.crime_suspect); mSuspectButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { startActivityForResult(pickContact, REQUEST_CONTACT); } }); if (mCrime.getSuspect() != null) { mSuspectButton.setText(mCrime.getSuspect()); } PackageManager packageManager = getActivity().getPackageManager(); if (packageManager.resolveActivity(pickContact, PackageManager.MATCH_DEFAULT_ONLY) == null) { mSuspectButton.setEnabled(false); } mPhotoButton = (ImageButton) v.findViewById(R.id.crime_camera); final Intent captureImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); boolean canTakePhoto = mPhotoFile != null && captureImage.resolveActivity(packageManager) != null; mPhotoButton.setEnabled(canTakePhoto); if (canTakePhoto) { Uri uri = Uri.fromFile(mPhotoFile); captureImage.putExtra(MediaStore.EXTRA_OUTPUT, uri); } mPhotoButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { startActivityForResult(captureImage, REQUEST_PHOTO); } }); mPhotoView = (ImageView) v.findViewById(R.id.crime_photo); updatePhotoView(); return v; }