资源预览内容
第1页 / 共21页
第2页 / 共21页
第3页 / 共21页
第4页 / 共21页
第5页 / 共21页
第6页 / 共21页
第7页 / 共21页
第8页 / 共21页
第9页 / 共21页
第10页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
南昌航空大学实验报告二。一 4年11月14日课程名称:An droid 实验名称: An droid数据存储和数据访问 班级:姓名:.同组人:指导教师评定:签名:一:实验目的掌握SharedPreferences的使用方法;掌握各种文件存储的区别与适用情况;了解SQLite数据库的特点和体系结构;掌握SQLite数据库的建立和操作方法;理解ContentProvider的用途和原理;掌握ContentProvider的创建与使用方法二:实验工具Eclipse( MyEclipse)+ ADT + Android2.2 SDK ;三:实验题目1 .应用程序一般允许用户自己定义配置信息,如界面背景颜色、字体大小和字体颜色等,尝试使用SharedPreferences保存用户的自定义配置信息,并在程序启劫时自动加载这些自定义的配置信息。2 .尝试把第1题的用户自己定义配置信息,以INI文件的形式保存在内部存储器上。3 .使用代码建库的方式,创建名为test.db的数据库,并建立staff数据表,表内的属性 值如下表所示:属性数据类型说明j din teger主键n ametext姓名sextext性别departme nttext所在部门salaryfloat工资实验目的掌握 SharedPreferences 的使用方法;掌握各种文件存储的区别与适用 情况;了解SQLite数据库的特点和体 系结构;掌握SQLite数据库的建立和操 作方法;理由军ContentProvider的用途和 原理;掌握ContentProvider的创建与 使用方法实验工具Eclipse ( MyEclipse) + ADT +Android2.2 SDK ;实验题目1 .应用程序一般允许用户自己 定 义配置信息,如界面背景颜色、字体 大小和字体颜色等,尝试使用 SharedPreferences保存用户的自定 义配置信息,并在程序启动时自动加 载这些自定义的配置信息。2 .尝试把第1题的用户自己定义 配置信息,以INI文件的形式保 存在 内部存储器上。3 .使用代码建库的方式,创建名 为test.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) privateprivateprivateprivateprivatefinal 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.layout.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 .setOnClickListener(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.getBytes();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() 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 .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 PREFERENCE_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.ageText = (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( 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);)private 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 editor.commit();
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号