第6章 Android數(shù)據(jù)存儲(chǔ) 理論_第1頁(yè)
第6章 Android數(shù)據(jù)存儲(chǔ) 理論_第2頁(yè)
第6章 Android數(shù)據(jù)存儲(chǔ) 理論_第3頁(yè)
第6章 Android數(shù)據(jù)存儲(chǔ) 理論_第4頁(yè)
第6章 Android數(shù)據(jù)存儲(chǔ) 理論_第5頁(yè)
已閱讀5頁(yè),還剩20頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、第6章Android數(shù)據(jù)存儲(chǔ),本章內(nèi)容,Android的SharedPreferencesAndroid的文件讀寫Android的SQLite數(shù)據(jù)庫(kù),本章目標(biāo),了解Android數(shù)據(jù)存儲(chǔ)方式掌握SharedPreferences的使用掌握文件讀寫的使用掌握SQLite數(shù)據(jù)庫(kù)的使用,1.Android數(shù)據(jù)存儲(chǔ),Android為應(yīng)用軟件提供了一種向其他應(yīng)用軟件開(kāi)放私有數(shù)據(jù)的標(biāo)準(zhǔn)方式。,Android數(shù)據(jù)存儲(chǔ)描述了應(yīng)用軟件存儲(chǔ)和獲取數(shù)據(jù)、向其他應(yīng)用軟件開(kāi)放數(shù)據(jù)、從其他應(yīng)用軟件請(qǐng)求數(shù)據(jù)并將其開(kāi)放的方式,1.Android數(shù)據(jù)存儲(chǔ),Android數(shù)據(jù)存儲(chǔ)分為以下4種方式:1.SharedPreferen

2、ces存儲(chǔ)2.文件(Files)存儲(chǔ)3.數(shù)據(jù)庫(kù)(SQLiteDatabases)存儲(chǔ)4.網(wǎng)絡(luò)存儲(chǔ),2.SharedPreferences數(shù)據(jù)儲(chǔ)存,SharedPreferences是Android提供的一種用于存儲(chǔ)部分簡(jiǎn)單配置信息(如默認(rèn)的歡迎語(yǔ)、登錄用戶名和密碼等)的機(jī)制,以鍵值對(duì)方式存儲(chǔ),便于讀取和存入。,2.SharedPreferences數(shù)據(jù)儲(chǔ)存,使用SharedPreferences存儲(chǔ)登錄的用戶名和密碼:,用戶登錄的布局文件,publicclassSharedPreferencesActivityextendsActivityimplementsOnClickListenerpr

3、ivateSharedPreferencesmySharedPreferences;/定義SharedPreferences對(duì)象privateEditTextuserName;privateEditTextpassword;OverridepublicvoidonCreate(BundlesavedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.main);ButtonbtnSave=(Button)findViewById(R.id.btnSave);userName=(EditText)fin

4、dViewById(R.id.userName);password=(EditText)findViewById(R.id.password);/獲得一個(gè)SharedPreferences對(duì)象mySharedPreferences=getSharedPreferences(SETTING_INFOS,0);StringuserNameStr=mySharedPreferences.getString(userName,);StringpasswordStr=mySharedPreferences.getString(password,);userName.setText(userNameStr

5、);password.setText(passwordStr);btnSave.setOnClickListener(this);OverridepublicvoidonClick(Viewv)mySharedPreferences=getSharedPreferences(SETTING_INFOS,0);SharedPreferences.Editoreditor=mySharedPreferences.edit();editor.putString(userName,userName.getText().toString().putString(password,password.get

6、Text().toString();mit();newAlertDialog.Builder(this).setTitle(登錄成功.).setPositiveButton(確定,null).show();,2.SharedPreferences數(shù)據(jù)儲(chǔ)存,在Eclipse中切換到DDMS視圖的FileExplorer標(biāo)簽,找到/data/data目錄中對(duì)應(yīng)項(xiàng)目下的shared_prefs文件夾,SharedPreferences存儲(chǔ)的數(shù)據(jù)即存放在此文件夾中。,3.文件數(shù)據(jù)存儲(chǔ),SharedPreferences存儲(chǔ)方式簡(jiǎn)單易行,只適合存儲(chǔ)較簡(jiǎn)單的數(shù)據(jù),需要存儲(chǔ)更多的數(shù)據(jù)時(shí)可以選擇文件存儲(chǔ)方式。

7、文件存儲(chǔ)是一種較常用的方法,Android中讀取/寫入文件的方法與Java中實(shí)現(xiàn)I/O的程序相同,使用openFileInput()方法和openFileOutput()方法讀取設(shè)備中的文件。,3.文件數(shù)據(jù)存儲(chǔ),使用SharedPreferences存儲(chǔ)登錄的用戶名和密碼:,搜索產(chǎn)品的布局文件,publicclassFileActivityextendsActivityimplementsOnClickListenerprivateEditTextproductName;OverridepublicvoidonCreate(BundlesavedInstanceState)super.onCr

8、eate(savedInstanceState);setContentView(R.layout.main);ButtonbtnSave=(Button)findViewById(R.id.btnSave);productName=(EditText)findViewById(R.ductName);tryInputStreamis=openFileInput(file.txt);bytebuffer=newbyte100;intbyteCount=is.read(buffer);StringproductNameStr=newString(buffer,0,byteCount,u

9、tf-8);productName.setText(productNameStr);is.close();catch(Exceptione)e.printStackTrace();btnSave.setOnClickListener(this);OverridepublicvoidonClick(Viewarg0)tryOutputStreamos=openFileOutput(file.txt,Activity.MODE_PRIVATE);StringproductNameStr=productName.getText().toString();os.write(productNameStr

10、.getBytes(utf-8);os.close();catch(Exceptione)e.printStackTrace();newAlertDialog.Builder(this).setTitle(搜索成功.).setPositiveButton(確定,null).show();,重新啟動(dòng)模擬器,3.文件數(shù)據(jù)存儲(chǔ),使用openFileOutput方法打開(kāi)文件的模式,3.文件數(shù)據(jù)存儲(chǔ),通過(guò)SharedPreferences操作的數(shù)據(jù)存儲(chǔ)在shared_prefs文件夾中,而文件操作的數(shù)據(jù)存儲(chǔ)在與shared_prefs同級(jí)的files文件夾中。,4.SQLite數(shù)據(jù)存儲(chǔ),Android平

11、臺(tái)中集成了嵌入式的關(guān)系型數(shù)據(jù)庫(kù)SQLite,它是一款開(kāi)源的嵌入式數(shù)據(jù)庫(kù)引擎,支持多數(shù)SQL92標(biāo)準(zhǔn)。與其他數(shù)據(jù)庫(kù)相比,SQLite非常適合作為移動(dòng)存儲(chǔ)設(shè)備的數(shù)據(jù)存儲(chǔ),其具有以下4個(gè)特點(diǎn):1)處理速度快2)占用資源少3)SQLite中所有數(shù)據(jù)庫(kù)信息都存放在一個(gè)文件中,SQLite支持事務(wù)4)SQLite支持Windows、Linux等操作系統(tǒng),可以采用多種語(yǔ)言進(jìn)行操作,4.SQLite數(shù)據(jù)存儲(chǔ),應(yīng)用中需要操作數(shù)據(jù)庫(kù)時(shí),必須先獲得SQLiteDatabase對(duì)象。SQLiteDatabase提供了創(chuàng)建和打開(kāi)數(shù)據(jù)庫(kù)的方法,常用方法如下:1)openDatabase(Stringpath,Cursor

12、Factoryfactory,intflags)2)openOrCreateDatabase(Filefilepath,CursorFactoryfactory,intflags)3)openOrCreateDatabase(Stringpath,CursorFactoryfactory,intflags),創(chuàng)建數(shù)據(jù)庫(kù)對(duì)象,4.SQLite數(shù)據(jù)存儲(chǔ),文件的打開(kāi)模式及說(shuō)明,創(chuàng)建數(shù)據(jù)庫(kù)對(duì)象,4.SQLite數(shù)據(jù)存儲(chǔ),操作數(shù)據(jù),通常情況下,數(shù)據(jù)庫(kù)操作包括增、刪、改、查。SQLiteDatabase類提供了許多操作數(shù)據(jù)庫(kù)的方法,包括直接使用SQL語(yǔ)句作為參數(shù)的方法和專用于增、刪、改、查的方法。在SQL

13、iteDatabase中,execSQL()和rawQuery()是兩個(gè)非常重要的方法。execSQL()方法可以執(zhí)行insert、delete、update和CREATETABLE等具有更改行為的SQL語(yǔ)句;rawQuery()方法可以執(zhí)行select語(yǔ)句。,4.SQLite數(shù)據(jù)存儲(chǔ),查詢數(shù)據(jù),SQLiteDatabase中提供了直接解析SQL語(yǔ)句的查詢方法和專用于查詢的方法。,4.SQLite數(shù)據(jù)存儲(chǔ),SQLite進(jìn)行數(shù)據(jù)儲(chǔ)存示例,調(diào)用數(shù)據(jù)庫(kù)操作方法時(shí)創(chuàng)建數(shù)據(jù)庫(kù)的輔助類的onCreate(SQLiteDatabasedb)方法:輔助類的onUpgrade(SQLiteDatabasedb,

14、intoldVersion,intnewVersion)方法:,publicvoidonCreate(SQLiteDatabasedb)/創(chuàng)建數(shù)據(jù)庫(kù)表Stringsql=CREATETABLEPRODUCT(+_idAUTOINC,+PRODUCT_NAMEVARCHAR(20)NOTNULLONCONFLICTFAIL,+PRICEVARCHAR(20)NOTNULLONCONFLICTFAIL,+ADDRESSVARCHAR(20)NOTNULLONCONFLICTFAIL,+CONSTRAINTsqlite_autoindex_t_contacts_1PRIMARYKEY(_id);db

15、.execSQL(sql);,publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion)Stringsql=droptableifexistsPRODUCT;db.execSQL(sql);/新的SQL語(yǔ)句sql=CREATETABLEPRODUCT(+_idAUTOINC,+PRODUCT_NAMEVARCHAR(20)NOTNULLONCONFLICTFAIL,+PRICEVARCHAR(20)NOTNULLONCONFLICTFAIL,+ADDRESSVARCHAR(20)NOTNULLONCONFLICTFAIL,

16、+CONSTRAINTsqlite_autoindex_t_contacts_1PRIMARYKEY(_id);db.execSQL(sql);,4.SQLite數(shù)據(jù)存儲(chǔ),查詢顯示數(shù)據(jù)的布局文件實(shí)現(xiàn)該查詢的Activity類,publicvoidonCreate(BundlesavedInstanceState)super.onCreate(savedInstanceState);DBServicedbService=newDBService(this);Stringsql=select_id,PRODUCT_NAME,PRICE,ADDRESSfromPRODUCTorderby_id;Cu

17、rsorcursor=dbService.query(sql,null);contactAdapter=newContactAdapter(this,cursor,true);setListAdapter(contactAdapter);publicclassContactAdapterextendsCursorAdapterprivateLayoutInflaterlayoutInflater;privatevoidsetChildView(Viewview,Cursorcursor)TextViewproductName=(TextView)view.findViewById(R.id.p

18、roductName);TextViewprice=(TextView)view.findViewById(R.id.price);TextViewaddress=(TextView)view.findViewById(R.id.address);productName.setText(cursor.getString(cursor.getColumnIndex(PRODUCT_NAME);price.setText(cursor.getString(cursor.getColumnIndex(PRICE);address.setText(cursor.getString(cursor.get

19、ColumnIndex(ADDRESS);OverridepublicvoidbindView(Viewview,Contextcontext,Cursorcursor)setChildView(view,cursor);OverridepublicViewnewView(Contextcontext,Cursorcursor,ViewGroupparent)Viewview=layoutInflater.inflate(R.layout.main,null);setChildView(view,cursor);returnview;publicContactAdapter(Contextco

20、ntext,Cursorc,booleanautoRequery)super(context,c,autoRequery);layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);,4.SQLite數(shù)據(jù)存儲(chǔ),SQLite的數(shù)據(jù)庫(kù)文件存儲(chǔ)在data/data/package-name/databases目錄下,5.Network數(shù)據(jù)存儲(chǔ),Android可以通過(guò)網(wǎng)絡(luò)獲取并保存數(shù)據(jù)信息,由于信息來(lái)自網(wǎng)絡(luò),因此需要保持網(wǎng)絡(luò)的連接狀態(tài)。使用Android的Network實(shí)現(xiàn)網(wǎng)絡(luò)文件數(shù)

21、據(jù)的獲取,需要有網(wǎng)絡(luò)服務(wù)器提供網(wǎng)絡(luò)數(shù)據(jù)。,5.Network數(shù)據(jù)存儲(chǔ),搭建Web應(yīng)用,先在Tomcat的Root目錄下創(chuàng)建Network.txt文件,并在其中編輯信息。啟動(dòng)Tomcat服務(wù)器后,通過(guò)http:/localhost:8989/Network.txt訪問(wèn)web訪問(wèn)Web信息。,5.Network數(shù)據(jù)存儲(chǔ),使用Android讀取網(wǎng)絡(luò)資源信息,publicclassNetworkActivityextendsActivityOverridepublicvoidonCreate(BundlesavedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.main);TextViewmyTextView=newTextView(this);StringmyTe

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論