Skip to content

Commit

Permalink
hzuapps#6 hzuapps#7 hzuapps#119 实验6、7未用到,做了课上的例子
Browse files Browse the repository at this point in the history
  • Loading branch information
wicky-yang committed Jan 14, 2021
1 parent 44ce7cd commit 504a55b
Show file tree
Hide file tree
Showing 8 changed files with 758 additions and 0 deletions.
121 changes: 121 additions & 0 deletions students/net1814080903122/report/lab5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# 实验五

## 一、实验目标

1.了解Andoid的存储手段;
2.掌握Android的文件存储;
3.掌握Android的数据库存储.

## 二、实验内容

1. 将应用产生的数据存储到数据库
2. 使用数据库存储

## 三、实验步骤

代码如下:

```
package edu.hzuapps.androidlabs.model;
import android.app.Application;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.lang.reflect.Method;
import java.util.ArrayList;
import edu.hzuapps.androidlabs.bean.ScheduleItem;
public class SchedulesModel {
private static ArrayList<ScheduleItem> itemArrayList = new ArrayList<>();
private static int count = 0;
private static SQLiteDatabase db = null;
private static SchedulesModel schedulesModel = new SchedulesModel();
private SchedulesModel(){
String dbPath = ApplicationGetter.getCurApplication().getFilesDir().getAbsolutePath() + "schedule.db";
db = SQLiteDatabase.openOrCreateDatabase(dbPath,null);
seekorCreateTable();
}
private void seekorCreateTable(){
StringBuilder sql = new StringBuilder("create table if not exists schedule(");
sql.append("_id integer primary key autoincrement,");
sql.append("date text NOT NULL,");
sql.append("message text NOT NULL)");
db.execSQL(sql.toString());
count = count();
initItemArray();
}
public static SchedulesModel getInstance(){
return schedulesModel;
};
public int getCount() { return count; }
private int count() {
Cursor cursor = db.query("schedule", null, null, null, null, null, null);
int count = cursor.getCount();
cursor.close();
return count;
}
public static Object getItem(int position) {
return itemArrayList.get(position);
}
private void initItemArray(){
Cursor cursor = db.query("schedule", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
int i = 0;
ScheduleItem item;
do {
item = new ScheduleItem(cursor.getInt(0),cursor.getString(1),cursor.getString(2));
itemArrayList.add(item);
}while(cursor.moveToNext());
}
}
private static class ApplicationGetter{
public static Application getCurApplication(){
Application application = null;
try{
Class atClass = Class.forName("android.app.ActivityThread");
Method currentApplicationMethod = atClass.getDeclaredMethod("currentApplication");
currentApplicationMethod.setAccessible(true);
application = (Application) currentApplicationMethod.invoke(null);
Log.d("fw_create","curApp class1:"+application);
}catch (Exception e){
Log.d("fw_create","e:"+e.toString());
}
if(application != null)
return application;
try{
Class atClass = Class.forName("android.app.AppGlobals");
Method currentApplicationMethod = atClass.getDeclaredMethod("getInitialApplication");
currentApplicationMethod.setAccessible(true);
application = (Application) currentApplicationMethod.invoke(null);
Log.d("fw_create","curApp class2:"+application);
}catch (Exception e){
Log.d("fw_create","e:"+e.toString());
}
return application;
}
}
}
```

## 四、实验结果截图

可以新增运动计划

![lab5](lab5.png)

## 五、实验心得

通过这次实验学会了Andorid中的存储手段,这次使用了数据库存储,并掌握数据库存储。
Binary file added students/net1814080903122/report/lab5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added students/net1814080903122/report/lab6(1).jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added students/net1814080903122/report/lab6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 504a55b

Please sign in to comment.