




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、南昌航空大學(xué)實(shí)驗(yàn)報(bào)告二0一 4 年 11月14 日課程名稱:Android 實(shí)驗(yàn)名稱: Android 數(shù)據(jù)存儲(chǔ)和數(shù)據(jù)訪問 班級:姓名:_同組人:指導(dǎo)教師評定:簽名:一:實(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ù)類型說明_idinteger主鍵nametext姓名sextext性別departmenttext所在部門salaryfloat工資實(shí)驗(yàn)?zāi)康恼莆?SharedPreferences 的使用方法;掌握各種文件存儲(chǔ)的區(qū)別與適 用情況;了解SQLite數(shù)據(jù)庫
3、的特點(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)建 名為tes
4、t.db的數(shù)據(jù)庫,并建立staff 數(shù)據(jù)表,表內(nèi)的屬性值如下表所示:數(shù)據(jù)類型說明主鍵integernametext姓名sextext性別departmenttext所在部門salaryfloat工資4.建 立 一 個(gè)ContentProvider ,用來共學(xué)第 3題所建立的數(shù)據(jù)庫;4.建立一個(gè)ContentProvider ,用來共享第3題所建立的數(shù)據(jù)庫;四:實(shí)驗(yàn)代碼Internalpublic classInternalextends Activity privatefinal String =""privateTextViewlabelView ;privateText
5、ViewdisplayView ;privateCheckBoxappendBox ;privateOverrideEditTextentryText ;public 、void onCreate(Bundle savedInstanceState) super .onCreate(savedlnstanceState);setContentView(R.layout.main );display );append );entry );labelView = (TextView)findViewById(R.id.label );displayView = (TextView)findView
6、ById(R.id.appendBox= (CheckBox)findViewById(R.id.entryText= (EditText)findViewById(R.id.write );read );writeButtonListener);readButtonListener );Button writeButton = (Button)findViewById(R.id. Button readButton = (Button)findViewById(R.id. writeButton.setOnClickListener( readButton.setOnClickListene
7、r(entryText.selectAll();entryText.findFocus();OnClickListenerwriteButtonListenernew OnClickListener() public void onClick(View v) Overridefos =try nullif ( appendBox .isChecked()fos = open(,Context.MODE_APPEND);else fos = open(,Context.MODE_PRIVATE );String text =entryText .getText().toString();fos.
8、write(text.getBytes();labelView.setText(" 文件寫入成功,寫入長度:" +text.length();entryText.setText("" ); catch ( e) e.printStackTrace();catch (IOException e) e.printStackTrace();finally if (fos != null ) try fos.flush();fos.close(); catch (IOException e) e.printStackTrace();= new OnClickLi
9、stener() OnClickListenerreadButtonListenerOverridepublic void onClick(View v) displayView .setText( "" );fis = null try fis = open();if (fis.available() = 0) return ;byte readBytes = new byte fis.available();while (fis.read(readBytes) != -1)new String(readBytes);String text = displayView .
10、setText(text); labelView .setText( "文件讀取成功,文件長度:"+text.length(); catch ( e) e.printStackTrace();catch (IOException e) e.printStackTrace();SimplePreferenceDemopublic class SimplePreferenceDemoextends Activity privateEditTextnameText ;privateEditTextageText ;privateEditTextheightText ;public
11、staticfinal StringPREFERENCE_NAME = "SaveSetting"publicstaticintMODE = Context.MODE WORLDREADABLE +Context.MODE WORLD WRITEABLE;Overridepublic void onCreate(Bundle savedInstanceState) super .onCreate(savedInstanceState);setContentView(R.layout.main );nameText = (EditText)findViewById(R.id.
12、name);ageText = (EditText)findViewById(R.id.age );heightText = (EditText)findViewById(R.id.height );Overridepublic void onStart()super .onStart();l oadSharedPreferences();Overridepublic void onStop()super .onStop(); saveSharedPreferences();private void loadSharedPreferences()SharedPreferences shared
13、Preferences = getSharedPreferences( PREFERENCE_NAME, MODE);String name = sharedPreferences.getString("Name" , "Tom");int age = sharedPreferences.getInt("Age" , 20);float height = sharedPreferences.getFloat("Height" ,1.81f);nameText .setText(name);ageText .setT
14、ext(String.valueOf (age);heightText .setText(String.valueOf (height);private void saveSharedPreferences()SharedPreferences sharedPreferences = getSharedPreferences( PREFERENCE_NAME, MODE);SharedPreferences.Editor editor = sharedPreferences.edit();editor.putString("Name" , nameText .getText
15、().toString();editor.putInt("Age",Integer. parseInt (ageText .getText().toString(); editor.putFloat("Height" ,Float. parseFloat (heightText .getText().toString(); mit();SQLiteDemoDBAdapter.java public class DBAdapter privatestaticfinalStringDB_NAME ="people.db"privatest
16、aticfinalStringDB_TABLE ="peopleinfo"privatestaticfinalintDB_VERSION = 11;publicstaticfinalStringKEY_ID = "_id"publicstaticfinalStringKEY_NAME ="name"public publicstaticfinalstaticfinalStringKEY_AGE =StringKEY_HEIGHT"age" ;= "height"privateSQLiteData
17、basedb ;privatefinal Contextcontext ;privateDBOpenHelperdbOpenHelper ;publicDBAdapter(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 dbOpenHelper = new DBOp
18、enHelper( 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);newValues.put( KEY_AGE , p
19、eople. Age );newValues.put( KEY_HEIGHT , people. Height );return db .insert( DB_TABLE , null , newValues); public People queryAllData() Cursor results =db .query( DB_TABLE , new String KEY_IDKEY_NAME, KEY_AGE, KEY_HEIGHT ,null , null , null , null , null );return ConvertToPeople(results);public Peop
20、le queryOneData(long id) Cursor results =db .query(KEY_NAME, KEY_AGE, KEY_HEIGHT , KEY_ID + "=" + id,DB_TABLE , new String null , null , null , null );KEY_IDreturn ConvertToPeople(results); private People ConvertToPeople(Cursor cursor) int resultCounts = cursor.getCount();if (resultCounts
21、= 0 | !cursor.moveToFirst() return null ;People peoples =new PeopleresultCounts;for ( int i = 0 ; i<resultCounts; i+)peoplesi =new People();peoplesi. ID = cursor.getInt(0);peoplesi. Name = cursor.getString(cursor.getColumnIndex( peoplesi.Age =cursor.getInt(cursor.getColumnIndex(peoplesi.Height=cu
22、rsor.getFloat(cursor.getColumnIndex(KEY_NAME);KEY_AGE );KEY_HEIGHT );cursor.moveToNext();return peoples;publiclong deleteAllData() return db .delete( DB_TABLE , null , null ); public long deleteOneData( long id) return db .delete( DB_TABLE , KEY_ID + "=" + id,null );public long updateOneDa
23、ta(long id , People people)ContentValues updateValues =new ContentValues();updateValues.put(updateValues.put(updateValues.put(KEY_NAME, people.KEY_AGE, people.KEY_HEIGHT , people.Name);Age);Height );return db.update(DB_TABLE , updateValues,KEY_ID + "=" + id,null ); /* 靜態(tài)Helper類,用于建立、更新和打開數(shù)
24、據(jù)庫private static class DBOpenHelper extends*/SQLiteOpenHelper public DBOpenHelper(Contextcontext, String name, CursorFactoryfactory,int version) super (context, name, factory, version); private static final StringDBCREATE = "create table "DB_TABLE + " (" + KEY_ID +"integer p
25、rimary key autoincrement,KEY_NAME+ " text not null,"+ KEY_AGE+ " integer,"+ KEYHEIGHTOverride public void _db.execSQL(+ " float);"onCreate(SQLiteDatabase _db) DB_CREATE);Override public void_newVersion) _db.execSQL( onCreate(_db);onUpgrade(SQLiteDatabase"DROP TABLE
26、 IF EXISTS "_db, int _oldVersion,+ DB_TABLE );intPeople.javapublic class People publicint ID = -1;publicpublicString Name;int Age;public floatHeightOverridepublic String toString()String result =""result +="ID : " + thisresult +="姓名:"+thisresult +="年齡:"+t
27、hisresult +="身高:"+thisreturn result;ID + ",".Name + ",".Age + ",".Height + ","61SQLiteDemo.javapublic class SQLiteDemo extends Activity /* Called when the activity is first created. */ private DBAdapter dbAdepter ;private EditTextnameText ;privateEdi
28、tTextageText ;privateEditTextheightTextprivateEditTextidEntry ;privateTextViewlabelView;privateTextViewdisplayView;Overridepublic void onCreate(Bundle savedInstanceState) super .onCreate(savedInstanceState);setContentView(R.layout.main );nameText=(EditText)findViewById(R.);ageText =(EditText)
29、findViewById(R.id.age );heightText=(EditText)findViewById(R.id.heightidEntry =(EditText)findViewById(R.id.id_entry )labelView=(TextView)findViewById(R.id.label );displayView=(TextView)findViewById(R.id.display);add );query_all );clear );delete_all );query );delete );update );Button addButton = (Butt
30、on)findViewById(R.id.Button queryAllButton = (Button)findViewById(R.id.Button clearButton = (Button)findViewById(R.id.Button deleteAllButton = (Button)findViewById(R.id.Button queryButton = (Button)findViewById(R.id.Button deleteButton = (Button)findViewById(R.id.Button updateButton = (Button)findVi
31、ewById(R.id.addButton.setOnClickListener( queryAllButton.setOnClickListener( clearButton.setOnClickListener( deleteAllButton.setOnClickListener(queryButton.setOnClickListener( deleteButton.setOnClickListener( updateButton.setOnClickListener(dbAdepter = new DBAdapter( dbAdepter .open();OnClickListene
32、raddButtonListeneraddButtonListener );queryAllButtonListener);clearButtonListener);deleteAllButtonListener);queryButtonListener);deleteButtonListener);updateButtonListener);this );= new OnClickListener() Overridepublic void onClick(View v) People people = new People();people. Name = nameText .getTex
33、t().toString();people. Age =Integer.parseInt ( ageText .getText().toString();people. Height =Float. parseFloat( heightText .getText().toString();long colunm = dbAdepter .insert(people);if (colunm = -1 )labelView.setText(" 添加過程錯(cuò)誤!" ); else labelView.setText(" 成功添加數(shù)據(jù),ID :+String. valueO
34、f (colunm);OnClickListenerqueryAllButtonListener= new OnClickListener() Override public void onClick(View v) People peoples =dbAdepter .queryAllData();if (peoples = null )labelView .setText( " 數(shù)據(jù)庫中沒有數(shù)據(jù)" );return ;labelView .setText( " 數(shù)據(jù)庫:" );String msg ="" ;for ( int i
35、 = 0 ; i<peoples.length ; i+)msg += peoplesi.toString()+"n" ; displayView .setText(msg); ;OnClickListenerclearButtonListener= new OnClickListener() Overridepublic void onClick(View v) displayView .setText( "" );= new OnClickListener() OnClickListenerdeleteAllButtonListenerOverridepublic void onClick(View v) dbAdepter .deleteAllData();String msg =" 數(shù)據(jù)全部刪除labelView .setText(msg);OnClickListenerqueryButtonListener= new OnClickListener() Override public void onClick(View v) int id = I
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年福建省事業(yè)單位招聘考試綜合類專業(yè)能力測試試卷(建筑類)試題
- 2025年電子商務(wù)師(中級)電子商務(wù)法律法規(guī)與政策案例分析試卷
- 2025年統(tǒng)計(jì)學(xué)專業(yè)期末考試:抽樣調(diào)查方法與統(tǒng)計(jì)推斷綜合案例分析試題
- 2025年鋼筋工(高級)考試試卷:鋼筋工程施工質(zhì)量事故分析及預(yù)防
- 2025年南京市事業(yè)單位招聘考試教師招聘化學(xué)學(xué)科專業(yè)知識試題(初中)
- 2025年非心源性胸痛診療試題
- 2025年建筑行業(yè)農(nóng)民工權(quán)益保障與用工模式變革下的行業(yè)風(fēng)險(xiǎn)管理與創(chuàng)新實(shí)踐案例報(bào)告
- 2025年國際化教育中跨文化交流能力培養(yǎng)的師資培訓(xùn)策略報(bào)告
- 化工工藝安全操作與管理要點(diǎn)測試題
- 綠色建筑材料市場推廣政策與綠色建筑市場需求匹配度分析報(bào)告
- 2025新七年級語文下冊期末字音字形專題復(fù)習(xí)課件
- 妊娠合并貧血護(hù)理課件
- 左美馬嗪行業(yè)深度研究分析報(bào)告(2024-2030版)
- 荊州中學(xué)2024-2025學(xué)年高二下學(xué)期6月月考語文試題(定)
- 腦機(jī)接口硬件優(yōu)化-洞察及研究
- 預(yù)算與績效管理制度
- 理論聯(lián)系實(shí)際談一談如何傳承發(fā)展中華優(yōu)-秀傳統(tǒng)文化?參考答案
- T/SFABA 2-2016食品安全團(tuán)體標(biāo)準(zhǔn)食品配料焙烤食品預(yù)拌粉
- T/CI 307-2024用于疾病治療的間充質(zhì)干細(xì)胞質(zhì)量要求
- 探索Python數(shù)據(jù)科學(xué)的考試試題及答案
- 《新生兒高膽紅素血癥診治指南(2025)》解讀課件
評論
0/150
提交評論