Android数据存储和数据访问要点

上传人:ni****g 文档编号:487972424 上传时间:2023-09-14 格式:DOCX 页数:21 大小:44.77KB
返回 下载 相关 举报
Android数据存储和数据访问要点_第1页
第1页 / 共21页
Android数据存储和数据访问要点_第2页
第2页 / 共21页
Android数据存储和数据访问要点_第3页
第3页 / 共21页
Android数据存储和数据访问要点_第4页
第4页 / 共21页
Android数据存储和数据访问要点_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《Android数据存储和数据访问要点》由会员分享,可在线阅读,更多相关《Android数据存储和数据访问要点(21页珍藏版)》请在金锄头文库上搜索。

1、南昌航空大学实验报告二。一 4年11月14日课程名称:An droid 实验名称: An droid数据存储和数据访问 班级:姓名:.同组人:指导教师评定:签名:一:实验目的掌握SharedPreferences的使用方法;掌握各种文件存储的区别与适用情况;了解SQLite数据库的特点和体系结构;掌握SQLite数据库的建立和操作方法;理解ContentProvider的用途和原理;掌握ContentProvider的创建与使用方法二:实验工具Eclipse( MyEclipse)+ ADT + Android2.2 SDK ;三:实验题目1 .应用程序一般允许用户自己定义配置信息,如界面背景

2、颜色、字体大小和字体颜色等,尝试使用SharedPreferences保存用户的自定义配置信息,并在程序启劫时自动加载这些自定义的配置信息。2 .尝试把第1题的用户自己定义配置信息,以INI文件的形式保存在内部存储器上。3 .使用代码建库的方式,创建名为test.db的数据库,并建立staff数据表,表内的属性 值如下表所示:属性数据类型说明j din teger主键n ametext姓名sextext性别departme nttext所在部门salaryfloat工资实验目的掌握 SharedPreferences 的使用方法;掌握各种文件存储的区别与适用 情况;了解SQLite数据库的特点

3、和体 系结构;掌握SQLite数据库的建立和操 作方法;理由军ContentProvider的用途和 原理;掌握ContentProvider的创建与 使用方法实验工具Eclipse ( MyEclipse) + ADT +Android2.2 SDK ;实验题目1 .应用程序一般允许用户自己 定 义配置信息,如界面背景颜色、字体 大小和字体颜色等,尝试使用 SharedPreferences保存用户的自定 义配置信息,并在程序启动时自动加 载这些自定义的配置信息。2 .尝试把第1题的用户自己定义 配置信息,以INI文件的形式保 存在 内部存储器上。3 .使用代码建库的方式,创建名 为test

4、.db的数据库,并建立staff数据 表,表内的属性值如下表所示:主键数据类型说明in tegern ametext姓名sextext性别departme nttext所在部门salaryfloat工资4.建 立 一 个Co nte ntProvider,用来共享第3题所建立的数据库;4.建立一个Contentprovider,用来共享第3题所建立的数据库;四:实验代码In ternalpublic class Internalexte nds Activity public void on Click(View v) privateprivateprivateprivateprivatefi

5、nal Stri ng =TextViewTextViewCheckBoxEditTextlabelView displayViewappendBox ;en tryTextlabel);display);append );entry);write);read ); writeButt on Liste ner); read Butt on Liste ner );Overridepublic void on Create(Bu ndle saved In sta nceState) fsuper .onCreate(savedlnstanceState);setContentView(R.l

6、ayout.main );labelView =仃 extView)fi ndViewByld(R.id.displayView =仃 extView)fi ndViewByld(R.id.appe ndBox = (CheckBox)fi ndViewByld(R.id.en tryText= (EditText)fi ndViewByld(R.id.Button writeButton = (Butt on)fin dViewByld(R.id.Button readButton = (Butto n)findViewByld(R.id.writeButton .setOnClickLis

7、tener(en tryText.selectAII();en tryText.fin dFocus();writeButt on Liste ner=new On ClickListe ner() On ClickListe nerOverridefos = null try if ( appendBox JsChecked() fos =open(,Context.MODE_APPEND);else fos =open(,Context.MODE_PRIVATE );String text =entryText .getText().toString();fos. write(text.g

8、etBytes();labelView .setText( 文件写入成功,写入长度:+text.length();entryText .setText( catch ( e) e.printStackTrace()catch (lOException e) e.printStack I race();finally if (fos != null)try(fos.flush();fos.close(); catch (lOException e) e.printStackTrace();OnClickListenerreadButtonListener=new OnClickListener(

9、) public void on Stop()Overridepublic void onClick(View v) displayView .setText(IHI); fis = null; try fis = open();if (fis.available() = 0) return ; byte readBytes = new byte fis.available。; while (fis.read(readBytes) != -1) Stri ng text = new Str in g(readBytes); displayView.setText(text);labelView

10、 .setText(文件读取成功,文件长度:,+text.length();) catch ( e) e.pri ntStackTrace();catch (lOExcepti on e) e.pri ntStackTrace();) );)SimplePrefere nceDemoexte nds Activity public class SimplePrefere nceDemon ameText; ageText;heightTextprivateEditTextprivateEditTextprivateEditTextpublic static final Stri ng PREF

11、ERENCE_NAME = SaveSetti ng public static int MODE= Context.MODE WORLD READABLE +Con text.MODE WORLD WRITEABLE;Overridepublic void on Create(Bu ndle saved In sta nceState) super .onCreate(savedlnstanceState);setContentView(R.layout.main );name );age);height);nameText = (EditText)findViewByld(R.id.age

12、Text = (EditText)findViewByld(R.id.heightText = (EditText)fi ndViewByld(R.id.Overridepublic void on Start() super .onStart();I oadSharedPreferences();)Overridesuper .onStop();saveSharedPrefere nces();)private void loadSharedPrefere nces()SharedPrefere nces sharedPrefere nces =getSharedPrefere nces(

13、PREFERENCE_NAME, MODE);Stri ng n ame = sharedPrefere nces.getStri ng(NameHTomH);int age = sharedPreferences.getlnt(“Age”,20);float height = sharedPrefere nces.getFloat(Height”,1.81f);n ameText .setText (n ame);ageText .setText(Str ing.valueOf (age);heightText .setText(Stri ng.valueOf (height);)priva

14、te void saveSharedPrefere nces()SharedPrefere nces sharedPrefere nces =getSharedPrefere nces( PREFERENCE_NAME, MODE);SharedPrefere nces.Editor editor = sharedPrefere nces.edit();editor.putStri ng( Name, nameText .getText().toString();editor.putl nt(AgeIn teger. parselnt ( ageText .getText().toString();“Height”.getText().toStri ng();editor.putFloat(Float. parseFloat (heightText mit();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 营销创新

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号