CriminalIntent項目開發--後篇
阿新 • • 發佈:2017-10-15
dst tac postgre nal ... rip scrip ram 內部類
為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中
CriminalIntent項目開發--後篇