![Android数据存储和数据访问_第1页](http://file3.renrendoc.com/fileroot_temp3/2022-2/23/be46e249-8342-420c-b135-ca86631148c8/be46e249-8342-420c-b135-ca86631148c81.gif)
![Android数据存储和数据访问_第2页](http://file3.renrendoc.com/fileroot_temp3/2022-2/23/be46e249-8342-420c-b135-ca86631148c8/be46e249-8342-420c-b135-ca86631148c82.gif)
![Android数据存储和数据访问_第3页](http://file3.renrendoc.com/fileroot_temp3/2022-2/23/be46e249-8342-420c-b135-ca86631148c8/be46e249-8342-420c-b135-ca86631148c83.gif)
![Android数据存储和数据访问_第4页](http://file3.renrendoc.com/fileroot_temp3/2022-2/23/be46e249-8342-420c-b135-ca86631148c8/be46e249-8342-420c-b135-ca86631148c84.gif)
![Android数据存储和数据访问_第5页](http://file3.renrendoc.com/fileroot_temp3/2022-2/23/be46e249-8342-420c-b135-ca86631148c8/be46e249-8342-420c-b135-ca86631148c85.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、南昌航空大学实验报告二0一4 年 11 月 14 日课程名称: Android 实验名称: Android数据存储和数据访问 班级: 姓名: 同组人: 指导教师评定: 签名: 一:实验目的掌握SharedPreferences的使用方法;掌握各种文件存储的区别与适用情况;了解SQLite数据库的特点和体系结构;掌握SQLite数据库的建立和操作方法;理解ContentProvider的用途和原理;掌握ContentProvider的创建与使用方法二:实验工具Eclipse(MyEclipse)+ ADT + Android2.2 SDK;三:实验题目1.应用程序一般允许用户自己定义配置信息,如
2、界面背景颜色、字体大小和字体颜色等,尝试使用SharedPreferences保存用户的自定义配置信息,并在程序启动时自动加载这些自定义的配置信息。2.尝试把第1题的用户自己定义配置信息,以INI文件的形式保存在内部存储器上。3.使用代码建库的方式,创建名为test.db的数据库,并建立staff数据表,表内的属性值如下表所示:属性数据类型说明_idinteger主键nametext姓名sextext性别departmenttext所在部门salaryfloat工资实验目的掌握SharedPreferences的使用方法;掌握各种文件存储的区别与适用情况;了解SQLite数据库的特点和体系结构
3、;掌握SQLite数据库的建立和操作方法;理解ContentProvider的用途和原理;掌握ContentProvider的创建与使用方法实验工具Eclipse(MyEclipse)+ ADT + Android2.2 SDK;实验题目1.应用程序一般允许用户自己定义配置信息,如界面背景颜色、字体大小和字体颜色等,尝试使用SharedPreferences保存用户的自定义配置信息,并在程序启动时自动加载这些自定义的配置信息。2.尝试把第1题的用户自己定义配置信息,以INI文件的形式保存在内部存储器上。3.使用代码建库的方式,创建名为test.db的数据库,并建立staff数据表,表内的属性值
4、如下表所示:属性数据类型说明_idinteger主键nametext姓名sextext性别departmenttext所在部门salaryfloat工资4. 建立一个ContentProvider,用来共享第3题所建立的数据库;4. 建立一个ContentProvider,用来共享第3题所建立的数据库;四:实验代码InternalFileDemopublic class InternalFileDemo extends Activity private final String FILE_NAME = "fileDemo.txt"private TextView label
5、View;private TextView displayView;private CheckBox appendBox ;private EditText entryText; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); labelView = (TextView)findViewById(R.id.label); displayView = (TextView)findViewById(R
6、.id.display); appendBox = (CheckBox)findViewById(R.id.append); entryText = (EditText)findViewById(R.id.entry); Button writeButton = (Button)findViewById(R.id.write); Button readButton = (Button)findViewById(R.id.read); writeButton.setOnClickListener(writeButtonListener); readButton.setOnClickListene
7、r(readButtonListener); entryText.selectAll(); entryText.findFocus(); OnClickListener writeButtonListener = new OnClickListener() Overridepublic void onClick(View v) FileOutputStream fos = null; try if (appendBox.isChecked() fos = openFileOutput(FILE_NAME,Context.MODE_APPEND); else fos = openFileOutp
8、ut(FILE_NAME,Context.MODE_PRIVATE); String text = entryText.getText().toString(); fos.write(text.getBytes(); labelView.setText("文件写入成功,写入长度:"+text.length(); entryText.setText(""); catch (FileNotFoundException e) e.printStackTrace();catch (IOException e) e.printStackTrace();finall
9、yif (fos != null)try fos.flush();fos.close(); catch (IOException e) e.printStackTrace(); ; OnClickListener readButtonListener = new OnClickListener() Override public void onClick(View v) displayView.setText(""); FileInputStream fis = null; try fis = openFileInput(FILE_NAME);if (fis.availab
10、le() = 0)return;byte readBytes = new bytefis.available();while(fis.read(readBytes) != -1)String text = new String(readBytes);displayView.setText(text);labelView.setText("文件读取成功,文件长度:"+text.length(); catch (FileNotFoundException e) e.printStackTrace();catch (IOException e) e.printStackTrace
11、(); ; SimplePreferenceDemopublic class SimplePreferenceDemo extends Activity private EditText nameText;private EditText ageText;private EditText heightText; public static final String PREFERENCE_NAME = "SaveSetting"public static int MODE = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_W
12、RITEABLE; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); nameText = (EditText)findViewById(R.); ageText = (EditText)findViewById(R.id.age); heightText = (EditText)findViewById(R.id.height); Override public void onSta
13、rt() super.onStart(); loadSharedPreferences(); Override public void onStop() super.onStop(); saveSharedPreferences(); private void loadSharedPreferences() SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE); String name = sharedPreferences.getString("Name",&qu
14、ot;Tom"); int age = sharedPreferences.getInt("Age", 20); float height = sharedPreferences.getFloat("Height",1.81f); nameText.setText(name); ageText.setText(String.valueOf(age); heightText.setText(String.valueOf(height); private void saveSharedPreferences() SharedPreferences
15、sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("Name", nameText.getText().toString(); editor.putInt("Age", Integer.parseInt(ageText.getText().toString(); editor.putFloat("Height&qu
16、ot;, Float.parseFloat(heightText.getText().toString(); mit(); SQLiteDemopublic class DBAdapter private static final String DB_NAME = "people.db"private static final String DB_TABLE = "peopleinfo"private static final int DB_VERSION = 1; public static final String KEY_ID = "_i
17、d"public static final String KEY_NAME = "name"public static final String KEY_AGE = "age"public static final String KEY_HEIGHT = "height"private SQLiteDatabase db;private final Context context;private DBOpenHelper dbOpenHelper;public DBAdapter(Context _context) cont
18、ext = _context; /* Close the database */ public void close() if (db != null) db.close(); db = null; /* Open the database */ public void open() throws SQLiteException dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION); try db = dbOpenHelper.getWritableDatabase(); catch (SQLiteExcepti
19、on ex) db = dbOpenHelper.getReadableDatabase(); public long insert(People people) ContentValues newValues = new ContentValues(); newValues.put(KEY_NAME, people.Name); newValues.put(KEY_AGE, people.Age); newValues.put(KEY_HEIGHT, people.Height); return db.insert(DB_TABLE, null, newValues); public Peo
20、ple queryAllData() Cursor results = db.query(DB_TABLE, new String KEY_ID, KEY_NAME, KEY_AGE, KEY_HEIGHT, null, null, null, null, null); return ConvertToPeople(results); public People queryOneData(long id) Cursor results = db.query(DB_TABLE, new String KEY_ID, KEY_NAME, KEY_AGE, KEY_HEIGHT, KEY_ID +
21、"=" + id, null, null, null, null); return ConvertToPeople(results); private People ConvertToPeople(Cursor cursor) int resultCounts = cursor.getCount(); if (resultCounts = 0 | !cursor.moveToFirst() return null; People peoples = new PeopleresultCounts; for (int i = 0 ; i<resultCounts; i+)
22、 peoplesi = new People(); peoplesi.ID = cursor.getInt(0); peoplesi.Name = cursor.getString(cursor.getColumnIndex(KEY_NAME); peoplesi.Age = cursor.getInt(cursor.getColumnIndex(KEY_AGE); peoplesi.Height = cursor.getFloat(cursor.getColumnIndex(KEY_HEIGHT); cursor.moveToNext(); return peoples; public lo
23、ng deleteAllData() return db.delete(DB_TABLE, null, null); public long deleteOneData(long id) return db.delete(DB_TABLE, KEY_ID + "=" + id, null); public long updateOneData(long id , People people) ContentValues updateValues = new ContentValues(); updateValues.put(KEY_NAME, people.Name); u
24、pdateValues.put(KEY_AGE, people.Age); updateValues.put(KEY_HEIGHT, people.Height); return db.update(DB_TABLE, updateValues, KEY_ID + "=" + id, null); /* 静态Helper类,用于建立、更新和打开数据库*/ private static class DBOpenHelper extends SQLiteOpenHelper public DBOpenHelper(Context context, String name, Cu
25、rsorFactory factory, int version) super(context, name, factory, version); private static final String DB_CREATE = "create table " + DB_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_NAME+ " text not null, " + KEY_AGE+ " integer,"
26、; + KEY_HEIGHT + " float);" Override public void onCreate(SQLiteDatabase _db) _db.execSQL(DB_CREATE); Override public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) _db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE); onCreate(_db); public class People public in
27、t ID = -1;public String Name;public int Age;public float Height;Overridepublic String toString()String result = ""result += "ID:" + this.ID + ","result += "姓名:" + this.Name + ","result += "年龄:" + this.Age + ", "result += "身高:
28、" + this.Height + ","return result;public class SQLiteDemo extends Activity /* Called when the activity is first created. */private DBAdapter dbAdepter ;private EditText nameText;private EditText ageText;private EditText heightText;private EditText idEntry;private TextView labelView;p
29、rivate TextView displayView; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); nameText = (EditText)findViewById(R.); ageText = (EditText)findViewById(R.id.age); heightText = (EditText)findViewById(R.id.height); idEntry
30、 = (EditText)findViewById(R.id.id_entry); labelView = (TextView)findViewById(R.id.label); displayView = (TextView)findViewById(R.id.display); Button addButton = (Button)findViewById(R.id.add); Button queryAllButton = (Button)findViewById(R.id.query_all); Button clearButton = (Button)findViewById(R.i
31、d.clear); Button deleteAllButton = (Button)findViewById(R.id.delete_all); Button queryButton = (Button)findViewById(R.id.query); Button deleteButton = (Button)findViewById(R.id.delete); Button updateButton = (Button)findViewById(R.id.update); addButton.setOnClickListener(addButtonListener); queryAll
32、Button.setOnClickListener(queryAllButtonListener); clearButton.setOnClickListener(clearButtonListener); deleteAllButton.setOnClickListener(deleteAllButtonListener); queryButton.setOnClickListener(queryButtonListener); deleteButton.setOnClickListener(deleteButtonListener); updateButton.setOnClickList
33、ener(updateButtonListener); dbAdepter = new DBAdapter(this); dbAdepter.open(); OnClickListener addButtonListener = new OnClickListener() Overridepublic void onClick(View v) People people = new People();people.Name = nameText.getText().toString();people.Age = Integer.parseInt(ageText.getText().toStri
34、ng();people.Height = Float.parseFloat(heightText.getText().toString();long colunm = dbAdepter.insert(people);if (colunm = -1 )labelView.setText("添加过程错误!"); else labelView.setText("成功添加数据,ID:"+String.valueOf(colunm); ; OnClickListener queryAllButtonListener = new OnClickListener()
35、 Overridepublic void onClick(View v) People peoples = dbAdepter.queryAllData();if (peoples = null)labelView.setText("数据库中没有数据");return;labelView.setText("数据库:");String msg = ""for (int i = 0 ; i<peoples.length; i+)msg += peoplesi.toString()+"n"displayView.s
36、etText(msg); ; OnClickListener clearButtonListener = new OnClickListener() Overridepublic void onClick(View v) displayView.setText(""); ; OnClickListener deleteAllButtonListener = new OnClickListener() Overridepublic void onClick(View v) dbAdepter.deleteAllData();String msg = "数据全部删除"labelView.setText(msg); ; OnClickListener queryButtonListener = new OnClic
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年全球及中国香料封装技术行业头部企业市场占有率及排名调研报告
- 2025年全球及中国联合收割机皮带行业头部企业市场占有率及排名调研报告
- 2025年中国单立柱滚动灯箱行业市场发展前景及发展趋势与投资战略研究报告
- 2025年感冒清热解毒颗粒项目可行性研究报告
- 2025年参麝活络丸项目投资可行性研究分析报告
- 豆制品设备项目可行性研究报告
- 中西医结合医院行业分析报告
- 2025年度新型节能锅炉工程承包合同书
- 2025年度农业科技项目公司个人承包合同样本
- 2025年内部控制风险管理体系合同规范
- 2025年南京信息职业技术学院高职单招职业技能测试近5年常考版参考题库含答案解析
- 2025-2030年中国硫酸钾行业深度调研及投资战略研究报告
- 课题申报参考:社会网络视角下村改居社区公共空间优化与“土客关系”重构研究
- 乡镇卫生院2025年工作计划
- 2024年山东省泰安市初中学业水平生物试题含答案
- 机械工程类基础知识单选题100道及答案解析
- 关于水浒传的题目单选题100道及答案解析
- 冠心病课件完整版本
- 2024年卫生资格(中初级)-中医外科学主治医师考试近5年真题集锦(频考类试题)带答案
- 中国大百科全书(第二版全32册)08
- 霍尼韦尔Honeywell温控器UDC2500中文手册
评论
0/150
提交评论