标签:wrap ida selected int ase base nts androi 导入
1、对话框的添加
为之前的应用添加对话框
需要完成的任务:
1)创建DatePickerFragment
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.date_picker_title)
.setPositiveButton(android.R.string.ok,null)
.create();
}
2)借助FragmentManager显示对话框
要传递crime给DatePickerFragment需要新建一个
newInstance(Date)方法:
public class DatePickerFragment extends DialogFragment {
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;
}
同时在string.xml中添加字符串:
<string name="date_picker_title">Date of crime:</string>
对话框的响应:
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();
}
2、工具栏及菜单
1)生成图标:
右键单击drawable目录,选择new→Image Asset 如图
点击next 进入预览画面:
2)响应菜单项选择
需在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;
3、SQLite数据库
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)编写SQL创建初始代码
db.execSQL("create table " + CrimeTable.NAME + "(" +
" _id integer primary key autoincrement, " +
CrimeTable.Cols.UUID + ", " +
CrimeTable.Cols.TITLE + ", " +
CrimeTable.Cols.DATE + ", " +
CrimeTable.Cols.SOLVED + ", " +
CrimeTable.Cols.SUSPECT +
")"
);
代码中需要导入CrimeTable,在CrimeTable一词上,
按alt+回车,选中提示选项
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);
}
标签:wrap ida selected int ase base nts androi 导入
原文地址:http://www.cnblogs.com/JieG/p/7660088.html