Android开发初体验-CriminalIntent
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发初体验-CriminalIntent相关的知识,希望对你有一定的参考价值。
这次我们继续完善Criminal Intent应用,使其功能更加完善。
首先我们为Criminal Intent添加对话框,对话框既能引起用户的注意也可以接收用户的输入。在提示重要信息或提供用户选择方面,都非常有用。
1:创建DialogFragment,并给AlterDialog添加DatePicker
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = LayoutInflater.from(getActivity())
.inflate(R.layout.dialog_date, null);
return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.date_picker_title)
.setPositiveButton(android.R.string.ok,null)
.create();
2:显示DialogFragment
private static final String DIALOG_DATE = "DialogDate";
mDateButton.setOnClickListener(new 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);
}
});
3:响应DatePicker对话框
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);
updateCrime();
updateDate();
}
其次是工具栏设计。工具栏可放置菜单选项、提供应用导航、还能帮助统一设计风格、塑造品牌形象。
1:响应菜单选项
需在CrimeListFragment中
实现onOptionsItemSelected(MenuItem)方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_crime:
Crime crime = new Crime();
CrimeLab.get(getActivity()).addCrime(crime);
updateUI();
mCallbacks.onCrimeSelected(crime);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
在CrimeListFragment中
响应SHOWSUBTITLE菜单项单击事件
case R.id.show_subtitle:
mSubtitleVisible = !mSubtitleVisible;
getActivity().invalidateOptionsMenu();
updateSubtitle();
return true;
最后创建数据库
1:创建舒适数据库
创建CrimeBaseHelper类
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) {
}
}
2:写入数据库
使用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; }
3:读取数据库
调用query(...)方法查询记录
private CrimeCursorWrapper queryCrimes(String whereClause, String[] whereArgs) {
Cursor cursor = mDatabase.query(
CrimeTable.NAME,
null, // Columns - null selects all columns
whereClause,
whereArgs,
null, // groupBy
null, // having
null // orderBy
);
return new CrimeCursorWrapper(cursor);
}
以上是关于Android开发初体验-CriminalIntent的主要内容,如果未能解决你的问题,请参考以下文章