CriminalIntent项目开发--后篇

CriminalIntent项目开发--后篇

为Criminal Intent应用添加对话框

  • 创建DiaologFragment,使用FragmentManager管理对话框,可以灵活的显示对话框。

要显示对话框我们需要完成一些步骤:

  • 创建DatePickerFragment类;
public class DatePickerFragment extends DialogFragment {
       @Override
        public Dialog onCreateDialog(Bundle saveInstanceState){
        return new AlertDialog.Builder(getActivity())
                .setView(v)
                .setTitle(R.string.date_picker_title)
                .setPositiveButton(android.R.string.ok,
                .create();
    }
  • 创建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();
    }
  • 借助FragmentManager在屏幕上显示对话框
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);
            }

Fragment间的数据传递

  • 前面我们实现了activity间以及fragment的activity间的数据传递。现在需要实现同一activity托管的两个fragment间的数据传递
传递数据给DataPickerFragment
  • 传递crime的日期给Fragment,需新建一个newInstance(Date)方法,然后将Date作为argument附件给Fragment。为了返回新的日期给Fragment并更新模型层以及对应的视图,需要将日期打包为extra并附加到Intent上,然后调用Fragment.onActivityResult(...)的方法,并传入准备好的Intent参数。
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) { //创建了AlertDialog的实例
//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);
返回数据给CrimeFragment
  • 设置目标Fragment
private static final int REQUEST_DATE = 0;
  • 传递数据给目标Fragment
public static final String EXTRA_DATE ="com.example.criminalintent.date";
​ 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);

}
  • 响应对话框
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_DATE) {
Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
mCrime.setDate(date);
mDateButton.setText(mCrime.getDate().toString());
//updateDate();
}

SQLite数据库的使用

  • SQLite是类似于MySQL和Postgresql的开源关系型数据库。不同于其他数据库的是,SQLite使用单个文件存储数据,使用SQLite库读取数据。
    Android标准库包含SQLite库以及配套的一些Java辅助类。

定义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);
}    

完成后我们便可以储存数据在APP中

posted @ 2017-10-15 14:14  机智的鹏  阅读(238)  评论(0编辑  收藏  举报