Android数据存储和数据访问要点Word文档格式.docx
- 文档编号:20749665
- 上传时间:2023-01-25
- 格式:DOCX
- 页数:17
- 大小:29.20KB
Android数据存储和数据访问要点Word文档格式.docx
《Android数据存储和数据访问要点Word文档格式.docx》由会员分享,可在线阅读,更多相关《Android数据存储和数据访问要点Word文档格式.docx(17页珍藏版)》请在冰豆网上搜索。
数据类型
说明
_id
integer
主键
name
text
姓名
sex
性别
department
所在部门
salary
float
工资实验目的
工资
4.建立一个ContentProvider,用来共享第3题所建立的数据库;
四:
实验代码
Internal
publicclassInternalextendsActivity{
privatefinalString="
"
;
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){
fos=null;
try{
if(appendBox.isChecked()){
fos=open(,Context.MODE_APPEND);
}
else{
fos=open(,Context.MODE_PRIVATE);
Stringtext=entryText.getText().toString();
fos.write(text.getBytes());
labelView.setText("
文件写入成功,写入长度:
+text.length());
entryText.setText("
);
}catch(e){
e.printStackTrace();
}
catch(IOExceptione){
finally{
if(fos!
=null){
try{
fos.flush();
fos.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
};
OnClickListenerreadButtonListener=newOnClickListener(){
@Override
publicvoidonClick(Viewv){
displayView.setText("
fis=null;
try{
fis=open();
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);
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);
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;
publicStringtoString(){
Stringresult="
result+="
ID:
+this.ID+"
,"
姓名:
+this.Name+"
年龄:
+this.Age+"
,"
身高:
+this.Height+"
returnresult;
publicclassSQLiteDemoextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
privateDBAdapterdbAdepter;
privateEditTextidEntry;
idEntry=(EditText)findViewById(R.id.id_entry);
ButtonaddButton=(Button)findViewById(R.id.add);
ButtonqueryAllButton=(Button)findViewById(R.id.query_all);
ButtonclearButton=(Button)findViewById(R.id.clear);
ButtondeleteAllButton=(Button)findViewById(R.id.delete_all);
ButtonqueryButton=(Button)findViewById(R.id.query);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 数据 存储 访问 要点