今天的任务为修改CrimeLab类,代码如下:
CrimeLab.java:
package com.bignerdranch.android.criminallntent; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import com.bignerdranch.android.criminallntent.database.CrimeDbSchema.CrimeDbSchema; import java.util.ArrayList; import java.util.List; import java.util.UUID; public class CrimeLab { private static CrimeLab sCrimeLab; private Context mContext; private SQLiteDatabase mDatabase; public static CrimeLab get(Context context){ if(sCrimeLab==null){ sCrimeLab=new CrimeLab(context); } return sCrimeLab; } private CrimeLab(Context context){ mContext=context.getApplicationContext(); mDatabase=new CrimeBaseHelper(mContext) .getWritableDatabase(); } public void addCrime(Crime c){ ContentValues values=getContentValues(c); mDatabase.insert(CrimeDbSchema.CrimeTable.NAME,null,values); } public List<Crime> getCrimes(){ return new ArrayList<>(); } public Crime getCrime(UUID id){ return null; } public void updateCrime(Crime crime){ String uuidString=crime.getId().toString(); ContentValues values=getContentValues(crime); mDatabase.update(CrimeDbSchema.CrimeTable.NAME,values, CrimeDbSchema.CrimeTable.Cols.UUID + "= ?", new String[] {uuidString}); } private static ContentValues getContentValues(Crime crime){ ContentValues values=new ContentValues(); values.put(CrimeDbSchema.CrimeTable.Cols.UUID,crime.getId().toString()); values.put(CrimeDbSchema.CrimeTable.Cols.TITLE,crime.getTitle()); values.put(CrimeDbSchema.CrimeTable.Cols.DATE,crime.getDate().getTime()); values.put(CrimeDbSchema.CrimeTable.Cols.SOLVED,crime.isSolved() ? 1:0); return values; } }
CrimeFragment.java:
package com.bignerdranch.android.criminallntent; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.DatePicker; import android.widget.EditText; import java.util.Date; import java.util.UUID; import static android.support.v4.provider.FontsContractCompat.FontRequestCallback.RESULT_OK; public class CrimeFragment extends Fragment{ private static final String ARG_CRIME_ID="crime_id"; private static final String DIALOG_DATE="DialogDate"; private static final int REQUEST_DATE=0; private Crime mCrime; private EditText mTitleField; private Button mDateButton; private CheckBox mSolvedCheckBox; public static CrimeFragment newInstance(UUID crimeId){ Bundle args=new Bundle(); args.putSerializable(ARG_CRIME_ID,crimeId); CrimeFragment fragment=new CrimeFragment(); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); UUID crimeId=(UUID)getArguments().getSerializable(ARG_CRIME_ID); //mCrime=new Crime(); //UUID crimeId=(UUID) getActivity().getIntent().getSerializableExtra(CrimeActivity.EXTRA_CRIME_ID); mCrime=CrimeLab.get(getActivity()).getCrime(crimeId); } @Override public void onPause(){ super.onPause(); CrimeLab.get(getActivity()) .updateCrime(mCrime); } @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) { //This space intentionally left bank } @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.setText(mCrime.getDate().toString()); //mDateButton.setEnabled(false); mDateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager manager=getFragmentManager(); //DatePickerFragment dialog=new DatePickerFragment(); DatePickerFragment dialog=DatePickerFragment.newInstance(mCrime.getDate()); dialog.setTargetFragment(CrimeFragment.this,REQUEST_DATE); dialog.show(manager,DIALOG_DATE); } }); mSolvedCheckBox=(CheckBox)v.findViewById(R.id.crime_solved); mSolvedCheckBox.setChecked(mCrime.isSolved()); mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mCrime.setSolved(isChecked); } }); return v; } private void updateDate() { mDateButton.setText(mCrime.getDate().toString()); } @Override public void onActivityResult(int requestCode,int resultCode,Intent data){ if(resultCode!=RESULT_OK){ return; } if(requestCode==REQUEST_DATE){ Date date=(Date)data.getSerializableExtra(DatePickerFragment.EXTRA_DATE); mCrime.setDate(date); updateDate(); } } }