Android数据存储和数据访问Word文件下载.doc

上传人:wj 文档编号:467002 上传时间:2023-04-29 格式:DOC 页数:14 大小:116.52KB
下载 相关 举报
Android数据存储和数据访问Word文件下载.doc_第1页
第1页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第2页
第2页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第3页
第3页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第4页
第4页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第5页
第5页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第6页
第6页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第7页
第7页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第8页
第8页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第9页
第9页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第10页
第10页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第11页
第11页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第12页
第12页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第13页
第13页 / 共14页
Android数据存储和数据访问Word文件下载.doc_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Android数据存储和数据访问Word文件下载.doc

《Android数据存储和数据访问Word文件下载.doc》由会员分享,可在线阅读,更多相关《Android数据存储和数据访问Word文件下载.doc(14页珍藏版)》请在冰点文库上搜索。

Android数据存储和数据访问Word文件下载.doc

说明

_id

integer

主键

name

text

姓名

sex

性别

department

所在部门

salary

float

工资实验目的

工资

4.建立一个ContentProvider,用来共享第3题所建立的数据库;

四:

实验代码

InternalFileDemo

publicclassInternalFileDemoextendsActivity{

privatefinalStringFILE_NAME="

fileDemo.txt"

;

privateTextViewlabelView;

privateTextViewdisplayView;

privateCheckBoxappendBox;

privateEditTextentryText;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

labelView=(TextView)findViewById(R.id.label);

displayView=(TextView)findViewById(R.id.display);

appendBox=(CheckBox)findViewById(R.id.append);

entryText=(EditText)findViewById(R.id.entry);

ButtonwriteButton=(Button)findViewById(R.id.write);

ButtonreadButton=(Button)findViewById(R.id.read);

writeButton.setOnClickListener(writeButtonListener);

readButton.setOnClickListener(readButtonListener);

entryText.selectAll();

entryText.findFocus();

}

OnClickListenerwriteButtonListener=newOnClickListener(){

@Override

publicvoidonClick(Viewv){

FileOutputStreamfos=null;

try{

if(appendBox.isChecked()){

fos=openFileOutput(FILE_NAME,Context.MODE_APPEND);

}

else{

fos=openFileOutput(FILE_NAME,Context.MODE_PRIVATE);

Stringtext=entryText.getText().toString();

fos.write(text.getBytes());

labelView.setText("

文件写入成功,写入长度:

"

+text.length());

entryText.setText("

);

}catch(FileNotFoundExceptione){

e.printStackTrace();

}

catch(IOExceptione){

finally{

if(fos!

=null){

try{

fos.flush();

fos.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

};

OnClickListenerreadButtonListener=newOnClickListener(){

@Override

publicvoidonClick(Viewv){

displayView.setText("

FileInputStreamfis=null;

try{

fis=openFileInput(FILE_NAME);

if(fis.available()==0){

return;

byte[]readBytes=newbyte[fis.available()];

while(fis.read(readBytes)!

=-1){

Stringtext=newString(readBytes);

displayView.setText(text);

labelView.setText("

文件读取成功,文件长度:

}

};

}

SimplePreferenceDemo

publicclassSimplePreferenceDemoextendsActivity{

privateEditTextnameText;

privateEditTextageText;

privateEditTextheightText;

publicstaticfinalStringPREFERENCE_NAME="

SaveSetting"

publicstaticintMODE=Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE;

nameText=(EditText)findViewById(R.id.name);

ageText=(EditText)findViewById(R.id.age);

heightText=(EditText)findViewById(R.id.height);

publicvoidonStart(){

super.onStart();

loadSharedPreferences();

publicvoidonStop(){

super.onStop();

saveSharedPreferences();

privatevoidloadSharedPreferences(){

SharedPreferencessharedPreferences=getSharedPreferences(PREFERENCE_NAME,MODE);

Stringname=sharedPreferences.getString("

Name"

"

Tom"

intage=sharedPreferences.getInt("

Age"

20);

floatheight=sharedPreferences.getFloat("

Height"

1.81f);

nameText.setText(name);

ageText.setText(String.valueOf(age));

heightText.setText(String.valueOf(height));

privatevoidsaveSharedPreferences(){

SharedPreferences.Editoreditor=sharedPreferences.edit();

editor.putString("

nameText.getText().toString());

editor.putInt("

Integer.parseInt(ageText.getText().toString()));

editor.putFloat("

Float.parseFloat(heightText.getText().toString()));

mit();

SQLiteDemo

publicclassDBAdapter{

privatestaticfinalStringDB_NAME="

people.db"

privatestaticfinalStringDB_TABLE="

peopleinfo"

privatestaticfinalintDB_VERSION=1;

publicstaticfinalStringKEY_ID="

_id"

publicstaticfinalStringKEY_NAME="

name"

publicstaticfinalStringKEY_AGE="

age"

publicstaticfinalStringKEY_HEIGHT="

height"

privateSQLiteDatabasedb;

privatefinalContextcontext;

privateDBOpenHelperdbOpenHelper;

publicDBAdapter(Context_context){

context=_context;

}

/**Closethedatabase*/

publicvoidclose(){

if(db!

db.close();

db=null;

}

}

/**Openthedatabase*/

publicvoidopen()throwsSQLiteException{

dbOpenHelper=newDBOpenHelper(context,DB_NAME,null,DB_VERSION);

try{

db=dbOpenHelper.getWritableDatabase();

catch(SQLiteExceptionex){

db=dbOpenHelper.getReadableDatabase();

}

publiclonginsert(Peoplepeople){

ContentValuesnewValues=newContentValues();

newValues.put(KEY_NAME,people.Name);

newValues.put(KEY_AGE,people.Age);

newValues.put(KEY_HEIGHT,people.Height);

returndb.insert(DB_TABLE,null,newValues);

publicPeople[]queryAllData(){

Cursorresults=db.query(DB_TABLE,newString[]{KEY_ID,KEY_NAME,KEY_AGE,KEY_HEIGHT},

null,null,null,null,null);

returnConvertToPeople(results);

publicPeople[]queryOneData(longid){

KEY_ID+"

="

+id,null,null,null,null);

privatePeople[]ConvertToPeople(Cursorcursor){

intresultCounts=cursor.getCount();

if(resultCounts==0||!

cursor.moveToFirst()){

returnnull;

People[]peoples=newPeople[resultCounts];

for(inti=0;

i<

resultCounts;

i++){

peoples[i]=newPeople();

peoples[i].ID=cursor.getInt(0);

peoples[i].Name=cursor.getString(cursor.getColumnIndex(KEY_NAME));

peoples[i].Age=cursor.getInt(cursor.getColumnIndex(KEY_AGE));

peoples[i].Height=cursor.getFloat(cursor.getColumnIndex(KEY_HEIGHT));

cursor.moveToNext();

returnpeoples;

publiclongdeleteAllData(){

returndb.delete(DB_TABLE,null,null);

publiclongdeleteOneData(longid){

returndb.delete(DB_TABLE,KEY_ID+"

+id,null);

publiclongupdateOneData(longid,Peoplepeople){

ContentValuesupdateValues=newContentValues();

updateValues.put(KEY_NAME,people.Name);

updateValues.put(KEY_AGE,people.Age);

updateValues.put(KEY_HEIGHT,people.Height);

returndb.update(DB_TABLE,updateValues,KEY_ID+"

/**静态Helper类,用于建立、更新和打开数据库*/

privatestaticclassDBOpenHelperextendsSQLiteOpenHelper{

publicDBOpenHelper(Contextcontext,Stringname,CursorFactoryfactory,intversion){

super(context,name,factory,version);

privatestaticfinalStringDB_CREATE="

createtable"

+

DB_TABLE+"

("

+KEY_ID+"

integerprimarykeyautoincrement,"

+

KEY_NAME+"

textnotnull,"

+KEY_AGE+"

integer,"

+KEY_HEIGHT+"

float);

@Override

publicvoidonCreate(SQLiteDatabase_db){

_db.execSQL(DB_CREATE);

publicvoidonUpgrade(SQLiteDatabase_db,int_oldVersion,int_newVersion){

_db.execSQL("

DROPTABLEIFEXISTS"

+DB_TABLE);

onCreate(_db);

}

publicclassPeople{

publicintID=-1;

publicStringName;

publicintAge;

publicfloatHeight;

@Override

publicStringtoString(){

Stringresult="

result+="

ID:

+this.ID+"

,"

姓名:

+this.Name+"

年龄:

+this.Age+"

,"

身高:

+this.Height+"

returnresult;

publicclassSQLiteDemoextendsActivity{

/**Calledwhentheactivityisfirstcreated.*/

privateDBAdapterdbAdepter;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2