CriminalIntent项目开发

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CriminalIntent项目开发相关的知识,希望对你有一定的参考价值。

显示对话框的步骤:
创建DatePickerFragment类;
public class DatePickerFragment extends DialogFragment {
public static final String EXTRA_DATE =
"com.bignerdranch.android.criminalintent.date";
private static final String ARG_DATE = "date";
private DatePicker mDatePicker;
public static DatePickerFragment newInstance(Date date) {
Bundle args = new Bundle();
args.putSerializable(ARG_DATE, date);
DatePickerFragment fragment = new DatePickerFragment();
fragment.setArguments(args);
return fragment;}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Date date = (Date) getArguments().getSerializable(ARG_DATE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
View v = LayoutInflater.from(getActivity())
.inflate(R.layout.dialog_date, null);
mDatePicker = (DatePicker) v.findViewById(R.id.dialog_date_picker);
mDatePicker.init(year, month, day, null);
return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.data_picker_title)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int year = mDatePicker.getYear();
int month = mDatePicker.getMonth();
int day = mDatePicker.getDayOfMonth();
Date date = new GregorianCalendar(year, month, day).getTime();
sendResult(Activity.RESULT_OK, date);}})
.create();}
private void sendResult(int resultCode, Date date) {
if (getTargetFragment() == null) {
return;}
Intent intent = new Intent();
intent.putExtra(EXTRA_DATE, date);
getTargetFragment()
.onActivityResult(getTargetRequestCode(), resultCode, intent);}}
创建AlertDialog;
public Dialog onCreateDialog(Bundle saveInstanceState){
Date date = (Date) getArguments().getSerializable(ARG_DATE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_date,null);
mDatePicker = (DatePicker) v.findViewById(R.id.dialog_date_picker);
mDatePicker.init(year,month,day,null);
return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.date_picker_title)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int year = mDatePicker.getYear();
int month = mDatePicker.getMonth();
int day = mDatePicker.getDayOfMonth();
Date date = new GregorianCalendar(year,month,day).getTime();
sendResult(Activity.RESULT_OK,date);}})
.create();}

接下来实现同一activity托管的两个fragment间的数据传递
把数据传递给DataPickerFragment

把crime的日期给Fragment,需新建一个newInstance(Date)方法,然后将Date作为argument附件给Fragment。

为了返回新的日期给Fragment并更新模型层以及对应的视图,需要将日期打包为extra并附加到Intent上,

然后准备好传入的Intent参数调用Fragment.onActivityResult()的方法。
private static final String ARG_DATE = "date";
private DatePicker mDatePicker;
public static DatePickerFragment newInstance(Date date) {
Bundle args = new Bundle();
args.putSerializable(ARG_DATE, date);

DatePickerFragment fragment = new DatePickerFragment();
fragment.setArguments(args);
return fragment;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//CrimeFragment向DatePickerFragment传递日期
Date date = (Date) getArguments().getSerializable(ARG_DATE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
SQLite数据库的使用
定义schema

定义 CrimeTable 内部类(CrimeTable 内部类唯一的用途就是定义描述数据表元素的 String 常量。首先要定义的是数据库表名(CrimeTable.NAME))
public class CrimeDbSchema {
public static final class CrimeTable {
public static final String NAME = "crimes";}}

定义数据表字段(有了这些数据表元素就可以在Java代码中安全的引用)
public static final class Cols {
public static final String UUID = "uuid";
public static final String TITLE = "title";
public static final String DATE = "date";
public static final String SOLVED = "solved";}

创建初始数据库

创建CrimeBaseHelper 类(用于简单打开SQLiteDatabase)并打开SQLiteDatabase
public class CrimeBaseHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;
private static final String DATABASE_NAME = "crimeBase.db";
public CrimeBaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);}
@Override
public void onCreate(SQLiteDatabase db) {}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}
public class CrimeLab {
private static CrimeLab sCrimeLab;
private List<Crime> mCrimes;
private Context mContext;
private SQLiteDatabase mDatabase;
...
private CrimeLab(Context context) {
mContext = context.getApplicationContext();
mDatabase = new CrimeBaseHelper(mContext)
.getWritableDatabase();
mCrimes = new ArrayList<>();}
写入数据库

使用ContentValues
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);
return values;}

插入和更新记录并刷新
public void addCrime(Crime c) {
ContentValues values = getContentValues(c);
mDatabase.insert(CrimeTable.NAME, null, values);}
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 });}
@Override
public void onPause() {
super.onPause();
CrimeLab.get(getActivity())
.updateCrime(mCrime);}

以上是关于CriminalIntent项目开发的主要内容,如果未能解决你的问题,请参考以下文章

CriminalIntent项目开发--后篇

《Android编程权威指南》CriminalIntent项目梳理

Android开发初体验-CriminalIntent

Android开发之CriminalIntent项目开发(其一)

CriminalIntent项目的开发

CriminalIntent项目开发