版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)用標(biāo)準(zhǔn)文案南昌航空大學(xué)實(shí)驗(yàn)報(bào)告二0 一 4 年 11月14 日課程名稱:An droid實(shí)驗(yàn)名稱: An droid 數(shù)據(jù)存儲(chǔ)和數(shù)據(jù)訪問 班級(jí):姓名:_同組人:指導(dǎo)教師評(píng)定:簽名:一:實(shí)驗(yàn)?zāi)康恼莆誗haredPreferences的使用方法;掌握各種文件存儲(chǔ)的區(qū)別與適用情況;了解SQLite數(shù)據(jù)庫的特點(diǎn)和體系結(jié)構(gòu);掌握SQLite數(shù)據(jù)庫的建立和操作方法;理解ContentProvider的用途和原理;掌握ContentProvider的創(chuàng)建與使用方法二:實(shí)驗(yàn)工具Eclipse( MyEclipse)+ ADT + Android2.2 SDK ;三:實(shí)驗(yàn)題目1. 應(yīng)用程序一般允許用戶自己定義
2、配置信息,如界面背景顏色、字體大小和字體顏色等,嘗試使用SharedPreferences保存用戶的自定義配置信息,并在程序啟動(dòng)時(shí)自動(dòng)加載這些自定義的配置信息。2. 嘗試把第1題的用戶自己定義配置信息,以INI文件的形式保存在內(nèi)部存儲(chǔ)器上。3. 使用代碼建庫的方式,創(chuàng)建名為test.db的數(shù)據(jù)庫,并建立 staff數(shù)據(jù)表,表內(nèi)的屬性值如下表所示:屬性數(shù)據(jù)類型說明din teger主鍵n ametext姓名sextext性別departme nttext所在部門salaryfloat工資實(shí)驗(yàn)?zāi)康恼莆?SharedPreferences 的使用方法;掌握各種文件存儲(chǔ)的區(qū)別與適用情況;了解SQLit
3、e數(shù)據(jù)庫的特點(diǎn)和 體系結(jié)構(gòu);掌握SQLite數(shù)據(jù)庫的建立和 操作方法;理解ContentProvider的用途和 原理;掌握ContentProvider的創(chuàng)建與 使用方法 實(shí)驗(yàn)工具Eclipse ( MyEclipse) + ADT + Android2.2 SDK ;實(shí)驗(yàn)題目1. 應(yīng)用程序一般允許用戶自己 定義配置信息,如界面背景顏色、 字體大小和字體顏色等,嘗試使用 SharedPreferences保存用戶的自定 義配置信息,并在程序啟動(dòng)時(shí)自動(dòng) 加載這些自定義的配置信息。2. 嘗試把第1題的用戶自己定 義配置信息,以INI文件的形式保 存在內(nèi)部存儲(chǔ)器上。3. 使用代碼建庫的方式,創(chuàng)建
4、名為test.db的數(shù)據(jù)庫,并建立staff 數(shù)據(jù)表,表內(nèi)的屬性值如下表所示:數(shù)據(jù)類型說明din teger主鍵n ametext姓名sextext性別departme nttext所在部門salaryfloat工資4.建立一個(gè)Co nte ntProvider,用來共享第3題所建立的數(shù)據(jù)庫;4. 建立一個(gè)ContentProvider ,用來共享第3題所建立的數(shù)據(jù)庫; 四:實(shí)驗(yàn)代碼In ternalFileDemopublic class Intern alFileDemoexte ndsActivity fileDemo.txtprivate final Stri ng FILE_NAME
5、 =privateTextViewlabelViewprivateTextViewdisplayViewprivateCheckBoxappendBoxprivateEditTexten tryTextOverridepublic voidon Create(Bu ndle saved In sta nceState) super .onCreate(savedInstanceState);setContentView(R.layout.main );labelView =仃 extView)fi ndViewByld(R.id.label );displayView = (TextView)
6、fi ndViewByld(R.id.display );appendBox = (CheckBox)findViewByld(R.id.append );entry );write );read );en tryText= (EditText)fi ndViewByld(R.id.Button writeButton = (Butt on)fin dViewByld(R.id.Button readButton = (Butto n)fin dViewById(R.id.writeButton .setOnClickListener(writeButt on Liste ner);readB
7、utton .setOnClickListener(readButt on Liste ner);精彩文檔en tryText.selectAll();en tryText.fin dFocus();On ClickListe nerwriteButt on Liste ner=new On ClickListe ner() Overridepublic void onClick(View v) FileOutputStream fos = null ; try if ( appendBox .isChecked()fos = openFileOutput( FILE_NAME ,Contex
8、t. MODE_APPEND); else fos =openFileOutput( FILE_NAME ,Context. MODE_PRIVATE );String text =entryText .getText().toString();fos.write(text.getBytes();labelView .setText( 文件寫入成功,寫入長度: +text.length();entryText .setText( ); catch (FileNotFoundException e) e.printStackTrace();catch (IOException e) e.prin
9、tStackTrace();finally if (fos != null ) try fos.flush();fos.close(); catch (IOException e) e.printStackTrace();OnClickListenerreadButtonListenernew OnClickListener() Overridepublic void onClick(View v) displayView .setText( );FileInputStream fis = nulltry fis = openFileInput(FILE_NAME );if (fis.avai
10、lable() = 0)returnbyte 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(FileNotFoundException e) e.pri ntStackTrace();catch(IOExcepti on e) e.pri ntStackTrace();S
11、implePrefere nceDemopublic classSimplePrefere nceDemoexte ndsActivity privateEditTextn ameText ;privateEditTextageText ;privateEditTextheightTextJpublicstaticfinalStri ngPREFERENCE_NAME = SaveSetti ngpublicstaticintMODE= Context.MODE WORLD READABLE +Con text.MODE_WORLD_WRITEABLE;Overridepublic void
12、on Create(Bu ndle saved In sta nceState) super .onCreate(savedInstanceState);setContentView(R.layout.main );nameText= (EditText)findViewByld(R. );ageText= (EditText)findViewByld(R.id.age );heightText = (EditText)fi ndViewByld(R.id.height );Overridepublic void on Start()super .onStart();l oadS
13、haredPreferences();Overridepublic voidon Stop()super .onStop(); saveSharedPrefere nces();private void loadSharedPrefere nces()SharedPrefere nces sharedPrefere nces =getSharedPrefere nces(PREFERENCE_NAME, MODE);Stri ng n ame = sharedPrefere nces.getStri ng(Nameint age = sharedPreferences.getlnt(Age ,
14、 20);float height = sharedPrefere nces.getFloat(Heightn 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
15、nces.Editor editor = sharedPrefere nces.edit();Tom);,1.81f);In teger.editor.putStri ng(Name , nameText .getText().toString();editor.putI nt(AgeparseInt ( ageText.getText().toString();editor.putFloat(HeightFloat. parseFloat(heightText.getText().toString();mit();SQLiteDemoDBAdapter.javapubli
16、c class DBAdapter privatestaticfinalStri ngDB_NAME =people.dbprivatestaticfinalStri ngDB_TABLE =people infoprivatestaticfinalintDB_VERSION = 1JpublicstaticfinalStringKEY_ID = _id ;publicstaticfinalStringKEY_NAME = name ;publicstaticfinalStringKEY_AGE = age ;publicstaticfinalStringKEY_HEIGHT = height
17、private SQLiteDatabase db;privatefinal Contextcontext ;private DBOpenHelper dbOpenHelper ;public DBAdapter(Context _context) context = _context;/* Close the database */ public void close() if ( db != null ) db .close(); db = null ;/* Open the database */nullpublic void open() throws SQLiteException
18、dbOpenHelper = new DBOpenHelper( context , DB_NAME, DB_VERSION );try db = dbOpenHelper .getWritableDatabase();catch (SQLiteException ex) db = dbOpenHelper .getReadableDatabase();public long insert(People people) ContentValues newValues =new ContentValues();newValues.put(KEY_NAME, people. Name );newV
19、alues.put( KEY_AGE , people. Age );newValues.put(KEY_HEIGHT , people. Height );return db .insert( DB_TABLE , null , newValues);publicPeople queryAllData() DB_TABLE , new String KEY_IDCursor results =db .query(KEY_NAME, KEY_AGE, KEY_HEIGHT ,null , null , null , null , null ); return ConvertToPeople(r
20、esults);public People queryOneData( long id) Cursor results = db .query(DB_TABLE , new String KEY_IDKEY_NAME, KEY_AGE, KEY_HEIGHT ,KEY_ID + = + id,null , nullnull , null );return ConvertToPeople(results); private People ConvertToPeople(Cursor cursor)int resultCounts = cursor.getCount();if (resultCou
21、nts = 0 | !cursor.moveToFirst()return null ;People peoples =new PeopleresultCounts;for ( int i = 0 ; iresultCounts; i+)peoplesi = new People();peoplesi. ID = cursor.getInt(0);peoplesi. Name =KEY_NAME);KEY_AGE );KEY_HEIGHT );cursor.getString(cursor.getColumnIndex(peoplesi.Age =cursor.getInt(cursor.ge
22、tColumnIndex(peoplesi.Height =cursor.getFloat(cursor.getColumnIndex(cursor.moveToNext();return peoples;public longdeleteAllData() returndb .delete( DBTABLE ,null , null );public longdeleteOneData(longid) returndb .delete( DBTABLE ,KEY_ID + = + id,null);long id , People people)new Conten tValues();up
23、dateValues.put(KEY_NAME, people. Name);updateValues.put(KEY_AGE, people. Age);updateValues.put(KEY_HEIGHT , people. Height );returndb .update(null );DB_TABLE , updateValues,KEY_ID += + id,/* 靜態(tài)Helper類,用于建立、更新和打開數(shù)據(jù)庫*/private static class DBOpe nH elperexte ndsSQLiteOpe nH elper publicDBOpe nH elper(C
24、 on textcon text,Stri ngn ame, CursorFactoryfactory,int versi on) super (con text, n ame, factory, vers ion);private static final StringDB_CREATE = create table I!+ float);DB_TABLE + (+ KEY_ID + integerprimary key autoincrement.KEY_NAME+ text not null, + KEY_AGE+ integer, + KEY_HEIGHTOverride public
25、 voidon Create(SQLiteDatabase _db) _db.execSQL(DB_CREATE);Override public voidon Upgrade(SQLiteDatabase_n ewVers ion) _db.execSQL(on Create(_db);DROP TABLE IF EXISTS _db, int _oldVers ion,+ DB_TABLE );int9People.javapublic classPeople public long update On eData(Conten tValues updateValues =publicin
26、tID = -1;publicStri ngNamepublicintAge ;publicfloatHeightOverridepublicStri ng toStri ng()result +=ID :+thisID +,result +=姓名:+this.Name +result +=年齡:+this.Age +result +=身高:+this.HeightStri ng result =retur nresult;SQLiteDemo.javapublic class SQLiteDemoexte nds Activity /* Called when the activity is
27、 first created. */privateDBAdapterdbAdepterprivateEditTextn ameTextprivateEditTextageText ;privateEditTextheightTextprivateEditTextidE ntry;privateTextViewlabelViewprivateTextViewdisplayViewOverridepublic void on Create(Bu ndle saved In sta nceState) super .onCreate(savedInstanceState);setContentVie
28、w(R.layout.main );name);age );height ); id_entry );n ameText = (EditText)fi ndViewById(R.id.ageText = (EditText)fi ndViewByld(R.id. heightText= (EditText)fi ndViewByld(R.id.idE ntry = (EditText)fi ndViewByld(R.id.label );display );labelView = (TextView)findViewById(R.id. displayView= (TextView)findV
29、iewById(R.id.addButton.setOnClickListener(queryAllButton.setOnClickListener(clearButton.setOnClickListener(deleteAllButton.setOnClickListener(queryButton.setOnClickListener(deleteButton.setOnClickListener(updateButton.setOnClickListener(dbAdepter = new DBAdapter(dbAdepter .open();OnClickListener add
30、ButtonListenerOverridepublic void onClick(View v) addButtonListener );queryAllButtonListenerclearButtonListener);deleteAllButtonListenerqueryButtonListener);deleteButtonListener)updateButtonListener)this );= new OnClickListener() ););People people =new People();Button addButton = (Button)findViewByI
31、d(R.id.Button queryAllButton = (Button)findViewById(R.id.Button clearButton = (Button)findViewById(R.id.Button deleteAllButton = (Button)findViewById(R.id.add );query_all clear );delete_allButton queryButton = (Button)findViewById(R.id.Button deleteButton = (Button)findViewById(R.id.Button updateButton = (Button)findViewById(R.id.query ); delete ); update );people. Name = nameText .getText().toString();people. Age =Integer.parseInt( ag
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 鐵路隧道照明與通風(fēng)設(shè)計(jì)考核試卷
- 化學(xué)實(shí)驗(yàn)室數(shù)據(jù)記錄與分析考核試卷
- 蔬果罐頭生產(chǎn)過程中的工藝參數(shù)調(diào)整與優(yōu)化考核試卷
- 鄉(xiāng)村旅游消費(fèi)者行為分析
- 銻冶煉產(chǎn)品質(zhì)量檢測(cè)考核試卷
- 1.2 成長不僅僅是身體 (解析版)
- 非公開募集基金參與網(wǎng)絡(luò)安全項(xiàng)目投資考核試卷
- 糕點(diǎn)店顧客關(guān)系管理與忠誠度計(jì)劃考核試卷
- 中國城市群一體化報(bào)告
- 2024-2025中國市場酒店運(yùn)營商信心指數(shù)調(diào)研報(bào)告-2024
- 如何當(dāng)好教研組長:中小學(xué)教研組長專業(yè)素養(yǎng)與行動(dòng)
- 中醫(yī)水腫教案講稿
- 成都建筑設(shè)計(jì)行業(yè)現(xiàn)狀分析
- 如何培養(yǎng)中學(xué)生學(xué)習(xí)英語的興趣
- 挖掘機(jī)常見故障診斷與排除課件
- 醫(yī)養(yǎng)結(jié)合養(yǎng)老院(養(yǎng)老中心)項(xiàng)目投資計(jì)劃書
- 計(jì)算機(jī)科學(xué)與技術(shù)生涯人物訪談報(bào)告
- 應(yīng)急救援隊(duì)伍的建設(shè)與管理
- 人教版小學(xué)英語五年級(jí)下冊(cè)Unit5現(xiàn)在進(jìn)行時(shí)
- 四年級(jí)上冊(cè)《為中華之崛起而讀書》說課PPT
- 江蘇徐州PPT介紹,徐州幻燈片模板
評(píng)論
0/150
提交評(píng)論