實(shí)驗(yàn)10 Android數(shù)據(jù)存儲(chǔ)和IO_第1頁
實(shí)驗(yàn)10 Android數(shù)據(jù)存儲(chǔ)和IO_第2頁
實(shí)驗(yàn)10 Android數(shù)據(jù)存儲(chǔ)和IO_第3頁
實(shí)驗(yàn)10 Android數(shù)據(jù)存儲(chǔ)和IO_第4頁
實(shí)驗(yàn)10 Android數(shù)據(jù)存儲(chǔ)和IO_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、 實(shí)驗(yàn)報(bào)告封面課程名稱: Android平臺(tái)開發(fā)與應(yīng)用 課程代碼: SM3004 任課老師: 梁郁君 實(shí)驗(yàn)指導(dǎo)老師: 梁郁君 實(shí)驗(yàn)報(bào)告名稱:實(shí)驗(yàn)10 Android數(shù)據(jù)存儲(chǔ)與IO 學(xué)生姓名: 學(xué)號(hào): 教學(xué)班: 遞交日期: 簽收人: 我申明,本報(bào)告內(nèi)的實(shí)驗(yàn)已按要求完成,報(bào)告完全是由我個(gè)人完成,并沒有抄襲行為。我已經(jīng)保留了這份實(shí)驗(yàn)報(bào)告的副本。 申明人(簽名):實(shí)驗(yàn)報(bào)告評(píng)語與評(píng)分:評(píng)閱老師簽名:一、實(shí)驗(yàn)名稱:Android數(shù)據(jù)存儲(chǔ)與IO二、實(shí)驗(yàn)日期:2014/11/13三、實(shí)驗(yàn)?zāi)康模?、掌握SharedPreferences的存儲(chǔ)數(shù)據(jù)的格式及位置,能夠讀寫其他應(yīng)用程序的SharedPreferenc

2、es。2、File存儲(chǔ)數(shù)據(jù)3、掌握SQLite存儲(chǔ)數(shù)據(jù)方法。4、會(huì)使用SQLiteOpenHelper輔助類,進(jìn)行操作數(shù)據(jù)庫(kù)。四、實(shí)驗(yàn)用的儀器和材料:PC+Eclipse+ADT五、實(shí)驗(yàn)的步驟和方法:1、讀寫其他應(yīng)用程序SharedPreferences。讀寫其他應(yīng)用程序的SharedPreferences,步驟如下:Ø 創(chuàng)建應(yīng)用App1 和應(yīng)用App2,App2嘗試讀取App1的SharedPreferences內(nèi)容Ø 在App2 需要?jiǎng)?chuàng)建App1對(duì)應(yīng)的Context。Ø 調(diào)用App1的Context的getSharedPreferences(String na

3、me,int mode) 即可獲取相應(yīng)的SharedPreferences對(duì)象。Ø 如果需要向App1的SharedPreferences數(shù)據(jù)寫入數(shù)據(jù),調(diào)用SharedPreferences的edit()方法獲取相應(yīng)的Editor即可。根據(jù)上述說明和下面截圖,以及代碼注釋,完成相關(guān)代碼片段填空,并思考問題:SharedPreferences何時(shí)會(huì)丟失? 圖1 App1運(yùn)行的界面 圖2 App2 運(yùn)行結(jié)果App1:記錄應(yīng)用程序的使用次數(shù),/com.Test/UseCount.java程序如下,補(bǔ)充程序中所缺代碼:import android.app.Activity;import an

4、droid.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.widget.Toast;public class UseCount extends ActivitySharedPreferences preferences;Overridepublic void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setConten

5、tView(R.layout.main);preferences = getSharedPreferences("count", MODE_WORLD_READABLE);/讀取SharedPreferences里的count數(shù)據(jù)int count = ("count" , 0);/顯示程序以前使用的次數(shù)Toast.makeText(this , "程序以前被使用了" + count + "次。", 10000).show();Editor editor = ;/存入數(shù)據(jù)editor.putInt("co

6、unt" , +count);/提交修改editor. ;App2:ReadOtherPreferences.java代碼如下,補(bǔ)充程序所缺代碼:import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.pm.PackageManager.NameNotFoundException;import android.os.Bundle;import android.widget.TextView;pub

7、lic class ReadOtherPreferences extends ActivityContext useCount;Overridepublic void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.main);/Context useCount = null;try/ 獲取其他程序所對(duì)應(yīng)的ContextuseCount = createPackageContext( ,Context.CONTEXT_IGNORE_SECURITY);ca

8、tch (NameNotFoundException e)e.printStackTrace();/ 使用其他程序的Context獲取對(duì)應(yīng)的SharedPreferencesSharedPreferences prefs = ("count",Context.MODE_WORLD_READABLE);/ 讀取數(shù)據(jù)int count = prefs. ("count", 0);TextView show = (TextView) findViewById(R.id.show);/ 顯示讀取的數(shù)據(jù)內(nèi)容show.setText("UseCount應(yīng)用

9、程序以前被使用了" + count + "次。");2、使用SQL語句操作SQLite數(shù)據(jù)庫(kù),調(diào)試以下程序,分析代碼的實(shí)現(xiàn)過程。使用SQLiteDatabase進(jìn)行數(shù)據(jù)庫(kù)操作的步驟如下:Ø 獲取SQLiteDatabase對(duì)象,進(jìn)行與數(shù)據(jù)庫(kù)連接。Ø 調(diào)用SQLiteDatabase的方法來執(zhí)行SQL語句。Ø 操作SQL語句的執(zhí)行結(jié)果,比如用SimpleCursorAdapter封裝成Cursor。Ø 調(diào)用close()方法,關(guān)閉SQLiteDatabase數(shù)據(jù)庫(kù),回收資源。根據(jù)應(yīng)用截圖和要求,完成相關(guān)代碼填空,并回答問題。問

10、題:應(yīng)用中創(chuàng)建的SQLite數(shù)據(jù)文件保存在什么位置?卸載應(yīng)用會(huì)不會(huì)刪除該文件? 圖1 讀取歷史數(shù)據(jù)圖2 插入新數(shù)據(jù)public class DBTest extends ActivitySQLiteDatabase db;Button bn = null;ListView listView;Overridepublic void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.main); /創(chuàng)建或打開數(shù)據(jù)庫(kù)(此處需要使用絕對(duì)路徑)db = SQLiteD

11、atabase.openOrCreateDatabase(this.getFilesDir().toString() + "/my.db3" , null);listView = (ListView)findViewById(R.id.show);bn = (Button)findViewById(R.id.ok);bn.setOnClickListener(new OnClickListener()Overridepublic void onClick(View source)/獲取用戶輸入String title = (EditText)findViewById(R.i

12、d.title).getText().toString();String content = (EditText)findViewById(R.id.content).getText().toString();try insertData(db , title , content);/查詢news_info表的所有記錄Cursor cursor = ("select * from news_inf", null);inflateList(cursor);catch(SQLiteException se)/執(zhí)行DDL創(chuàng)建數(shù)據(jù)表db.execSQL("create t

13、able news_inf(_id integer primary key autoincrement,"+ " news_title varchar(50),"+ " news_content varchar(255)");/執(zhí)行insert語句插入數(shù)據(jù)insertData(db , title , content);/查詢news_info表的所有記錄Cursor cursor = ("select * from news_inf", null);inflateList(cursor););private void in

14、sertData(SQLiteDatabase db, String title , String content)/執(zhí)行插入語句 ("insert into news_inf values(null , ? , ?)", new Stringtitle , content);private void inflateList(Cursor cursor)/填充SimpleCursorAdapter,注意控件的對(duì)應(yīng)關(guān)系SimpleCursorAdapter adapter = new SimpleCursorAdapter(DBTest.this , R.layout.lin

15、e, cursor , new String"news_title" , "news_content", );/顯示數(shù)據(jù)listView.setAdapter(adapter);Overridepublic void onDestroy()super.onDestroy();/退出程序時(shí)關(guān)閉SQLiteDatabaseif (db != null && db.isOpen()db.close();res/layout/line.xml<?xml version="1.0" encoding="utf-8

16、"?><LinearLayout xmlns:android="android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:id="+id/my_title"android:layout_width="wrap_content" android:layout_height="wrap_content" android

溫馨提示

  • 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. 人人文庫(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)論