




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、南昌航空大學(xué)實驗報告二0一 4 年 11月14 日課程名稱:Android 實驗名稱: Android 數(shù)據(jù)存儲和數(shù)據(jù)訪問 班級:姓名:_同組人:指導(dǎo)教師評定:簽名:一:實驗?zāi)康恼莆誗haredPreferences的使用方法;掌握各種文件存儲的區(qū)別與適用情況;了解SQLite數(shù)據(jù)庫的特點和體系結(jié)構(gòu);掌握SQLite數(shù)據(jù)庫的建立和操作方法;理解ContentProvider的用途和原理;掌握ContentProvider的創(chuàng)建與使用方法二:實驗工具Eclipse ( MyEclipse ) + ADT + Android2.2 SDK ;三:實驗題目1 .應(yīng)用程序一般允許用戶自己定義配置信息,
2、如界面背景顏色、字體大小和字體顏色等,嘗試使用SharedPreferences保存用戶的自定義配置信息,并在程序啟動時自動加載這些自定義的配置信息。2 .嘗試把第1題的用戶自己定義配置信息,以 INI文件的形式保存在內(nèi)部存儲器上。3 .使用代碼建庫的方式,創(chuàng)建名為test.db的數(shù)據(jù)庫,并建立 staff數(shù)據(jù)表,表內(nèi)的屬性值如下表所示:屬性數(shù)據(jù)類型說明_idinteger主鍵nametext姓名sextext性別departmenttext所在部門salaryfloat工資實驗?zāi)康恼莆?SharedPreferences 的使用方法;掌握各種文件存儲的區(qū)別與適 用情況;了解SQLite數(shù)據(jù)庫
3、的特點和 體系結(jié)構(gòu);掌握SQLite數(shù)據(jù)庫的建立和 操作方法;理解ContentProvider的用途和 原理;掌握ContentProvider的創(chuàng)建與 使用方法實驗工具Eclipse (MyEclipse ) + ADT + Android2.2 SDK ;實驗題目1 .應(yīng)用程序一般允許用戶自己 定義配置信息,如界面背景顏色、 字體大小和字體顏色等,嘗試使用 SharedPreferences保存用戶的自定 義配置信息,并在程序啟動時自動 加載這些自定義的配置信息。2 .嘗試把第1題的用戶自己定 義配置信息,以INI文件的形式保 存在內(nèi)部存儲器上。3 .使用代碼建庫的方式,創(chuàng)建 名為tes
4、t.db的數(shù)據(jù)庫,并建立staff 數(shù)據(jù)表,表內(nèi)的屬性值如下表所示:數(shù)據(jù)類型說明主鍵integernametext姓名sextext性別departmenttext所在部門salaryfloat工資4.建 立 一 個ContentProvider ,用來共學(xué)第 3題所建立的數(shù)據(jù)庫;4.建立一個ContentProvider ,用來共享第3題所建立的數(shù)據(jù)庫;四:實驗代碼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(" 添加過程錯誤!" ); 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)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 中式面點培訓(xùn)課件
- 論文事實論據(jù)
- 如何上好一節(jié)小學(xué)數(shù)學(xué)課心得體會
- 西餐面包禮儀培訓(xùn)課件
- 護理不良事件上報
- 基金產(chǎn)品銷售培訓(xùn)課件
- 幼兒園數(shù)學(xué)教學(xué)培訓(xùn)
- 腫瘤綜合防治體系解析
- 公寓員工培訓(xùn)課件
- 家校攜手共筑孩子美好未來
- 皮膚病的臨床取材及送檢指南-修訂版
- 機型理論-4c172實用類重量平衡
- 校企合作項目立項申請表(模板)
- 管道工廠化預(yù)制推廣應(yīng)用課件
- 海水的淡化精品課件
- 河流改道施工方案
- 項目工程移交生產(chǎn)驗收報告
- 清華大學(xué)美術(shù)學(xué)院陶瓷藝術(shù)設(shè)計系研究生導(dǎo)師及研究課題
- 計算機控制實驗報告初稿(共31頁)
- 抗磷脂抗體與抗磷脂綜合征.ppt
- 光伏發(fā)電項目工程質(zhì)量管理方案計劃策劃書
評論
0/150
提交評論