码迷,mamicode.com
首页 > 数据库 > 详细

SQLite数据库使用

时间:2017-10-15 19:28:22      阅读:339      评论:0      收藏:0      [点我收藏+]

标签: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);
}

 


 


 


 


 









SQLite数据库使用

标签:wrap   ida   selected   int   ase   base   nts   androi   导入   

原文地址:http://www.cnblogs.com/JieG/p/7660088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!