Merge pull request '1' (#2) from wxl_branch into develop
commit
6169c3a50a
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<bytecodeTargetLevel target="11" />
|
||||
<bytecodeTargetLevel target="17" />
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,73 @@
|
||||
package com.example.sleep;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
|
||||
import com.example.sleep.database.GetRecord;
|
||||
import com.example.sleep.database.RecordBean;
|
||||
import com.example.sleep.recyclerView.Trace;
|
||||
import com.example.sleep.recyclerView.TraceListAdapter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.example.sleep.database.GetRecord.getRecord;
|
||||
|
||||
/**
|
||||
* 睡眠记录页面
|
||||
*/
|
||||
public class Record extends Activity {
|
||||
|
||||
private RecyclerView rvTrace;
|
||||
private List<Trace> traceList = new ArrayList<>();//睡眠记录列表
|
||||
|
||||
private String date = "";//日期
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.records);
|
||||
rvTrace = findViewById(R.id.timelList);
|
||||
date = this.getIntent().getStringExtra("date");
|
||||
initData();
|
||||
}
|
||||
|
||||
//睡眠记录数据初始化
|
||||
private void initData() {
|
||||
GetRecord mGetRecord = getRecord();//获取数据库操作对象
|
||||
//List<RecordBean> records = mGetRecord.queryAllList();
|
||||
List<RecordBean> records = mGetRecord.queryByDate(date);// 根据日期查询睡眠记录
|
||||
if (records.size() == 1)
|
||||
{
|
||||
//如果只有一条记录,直接跳转到记录详情页面
|
||||
Intent i = new Intent(Record.this, SleepresultScreen.class);
|
||||
i.putExtra("date", date);
|
||||
i.putExtra("position", 0);
|
||||
Record.this.startActivity(i);
|
||||
Record.this.finish();
|
||||
} else {
|
||||
//如果有多条记录,将记录添加到列表中,并设置 RecyclerView 的适配器
|
||||
for (RecordBean e : records) {
|
||||
traceList.add(new Trace(e.getDate(), e.getStartTime() + "-" + e.getEndTime()
|
||||
+ " " + e.getTotalTime() / 60 + "时" + e.getTotalTime() % 60 + "分"));
|
||||
}
|
||||
TraceListAdapter adapter = new TraceListAdapter(this, traceList, date);
|
||||
rvTrace.setLayoutManager(new LinearLayoutManager(this));
|
||||
rvTrace.setAdapter(adapter);
|
||||
}
|
||||
}
|
||||
|
||||
//左上的返回
|
||||
public void ClickBack(View v) {
|
||||
Record.this.finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
@ -0,0 +1,158 @@
|
||||
package com.example.sleep;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.github.mikephil.charting.charts.LineChart;
|
||||
import com.github.mikephil.charting.charts.PieChart;
|
||||
import com.example.sleep.database.GetRecord;
|
||||
import com.example.sleep.database.RecordBean;
|
||||
import com.example.sleep.drawChart.DrawLineChart;
|
||||
import com.example.sleep.drawChart.DrawPieChart;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import static com.example.sleep.database.GetRecord.getRecord;
|
||||
|
||||
/**
|
||||
* 详细的记录页面
|
||||
*/
|
||||
public class SleepresultScreen extends Activity {
|
||||
|
||||
Button btn_left;
|
||||
Button btn_right;
|
||||
TextView mDate;
|
||||
TextView mStartTime;
|
||||
TextView mStopTime;
|
||||
TextView mSleepTime;
|
||||
TextView mDeep;
|
||||
TextView mSwallow;
|
||||
TextView mDream;
|
||||
LineChart mLineChart;
|
||||
PieChart mPieChart;
|
||||
DrawPieChart mDrawPieChart;
|
||||
DrawLineChart mDrawLineChart;
|
||||
|
||||
private int idx;
|
||||
private int max;
|
||||
private int month;
|
||||
private int day;
|
||||
|
||||
private boolean left_invisible;
|
||||
private boolean right_invisible;
|
||||
|
||||
private String date;
|
||||
|
||||
private List<RecordBean> records;
|
||||
private RecordBean mRecord;
|
||||
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);//设置布局文件
|
||||
//初始化视图部件
|
||||
setContentView(R.layout.record_details);
|
||||
mLineChart = findViewById(R.id.lineChart);
|
||||
mPieChart = findViewById(R.id.mPiechart);
|
||||
btn_left = findViewById(R.id.left);
|
||||
btn_right = findViewById(R.id.right);
|
||||
mDate = findViewById(R.id.date);
|
||||
mStartTime = findViewById(R.id.startTime);
|
||||
mStopTime = findViewById(R.id.stopTime);
|
||||
mSleepTime = findViewById(R.id.sleepTime);
|
||||
mDeep = findViewById(R.id.deep);
|
||||
mSwallow = findViewById(R.id.swallow);
|
||||
mDream = findViewById(R.id.dream);
|
||||
|
||||
idx = this.getIntent().getIntExtra("position", 0);
|
||||
date = this.getIntent().getStringExtra("date");
|
||||
readLog();//读取睡眠记录
|
||||
setText();//设置文本内容
|
||||
initView();//初始化视图
|
||||
}
|
||||
//视图
|
||||
private void initView()
|
||||
{
|
||||
mDrawPieChart = new DrawPieChart(mPieChart, mRecord, getResources());
|
||||
mDrawLineChart = new DrawLineChart(mLineChart, mRecord, getResources());
|
||||
if (idx >= max) {
|
||||
right_invisible = true;
|
||||
btn_right.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
if (idx == 0) {
|
||||
left_invisible = true;
|
||||
btn_left.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
}
|
||||
//设置文本
|
||||
private void setText() {
|
||||
mDate.setText(String.format(Locale.getDefault(), "%d月%d日", month, day));
|
||||
mStartTime.setText(String.format(getResources().getString(R.string.sleep_time), mRecord.getStartTime()));
|
||||
mStopTime.setText(String.format(getResources().getString(R.string.get_up_time), mRecord.getEndTime()));
|
||||
mSleepTime.setText(String.format(Locale.getDefault(), "时长 %02d:%02d",
|
||||
mRecord.getTotalTime() / 60, mRecord.getTotalTime() % 60));
|
||||
mDeep.setText(String.format(Locale.getDefault(), "深度睡眠 %02d:%02d",
|
||||
mRecord.getDeepTime() / 60, mRecord.getDeepTime() % 60));
|
||||
mSwallow.setText(String.format(Locale.getDefault(), "浅层睡眠 %02d:%02d",
|
||||
mRecord.getSwallowTime() / 60, mRecord.getSwallowTime() % 60));
|
||||
mDream.setText(String.format(Locale.getDefault(), "醒/梦 %02d:%02d",
|
||||
mRecord.getAwakeTime() / 60, mRecord.getAwakeTime() % 60));
|
||||
}
|
||||
|
||||
//左上的返回
|
||||
public void ClickBackDetails(View v) {
|
||||
SleepresultScreen.this.finish();
|
||||
}
|
||||
|
||||
//向左切换睡眠记录
|
||||
public void ClickLeft(View v) {
|
||||
mRecord = records.get(--idx);
|
||||
setText();
|
||||
mDrawPieChart = new DrawPieChart(mPieChart, mRecord, getResources());
|
||||
mDrawLineChart = new DrawLineChart(mLineChart, mRecord, getResources());
|
||||
if (idx == 0) {
|
||||
left_invisible = true;
|
||||
btn_left.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
if (right_invisible) {
|
||||
right_invisible = false;
|
||||
btn_right.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
//向右切换睡眠记录
|
||||
public void ClickRight(View v) {
|
||||
mRecord = records.get(++idx);
|
||||
setText();
|
||||
mDrawPieChart = new DrawPieChart(mPieChart, mRecord, getResources());
|
||||
mDrawLineChart = new DrawLineChart(mLineChart, mRecord, getResources());
|
||||
if (idx >= max) {
|
||||
right_invisible = true;
|
||||
btn_right.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
if (left_invisible) {
|
||||
left_invisible = false;
|
||||
btn_left.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取睡眠记录
|
||||
*/
|
||||
private void readLog() {
|
||||
GetRecord mGetRecord = getRecord();
|
||||
records = mGetRecord.queryByDate(date);
|
||||
mRecord = records.get(idx);
|
||||
max = records.size() - 1;
|
||||
String[] arr = mRecord.getDate().split("-");
|
||||
month = Integer.parseInt(arr[0]);
|
||||
day = Integer.parseInt(arr[1]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package com.example.sleep;
|
||||
|
||||
|
||||
import static android.content.Context.MODE_PRIVATE;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.example.sleep.database.RecordBean;
|
||||
import com.example.sleep.database.MyDataBaseHelper;
|
||||
import com.example.sleep.drawChart.SimpleLineChart;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class SuggestScreen extends Fragment {
|
||||
private RecordBean mRecord;//睡眠记录对象
|
||||
|
||||
private SimpleLineChart mSimpleLineChart;//折线图组件
|
||||
private TextView suggestion, gradeText;//文本显示组件
|
||||
//视图
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.chart_fragment, container, false); // 加载布局文件
|
||||
init(view);//初始化方法
|
||||
return view;//返回视图
|
||||
}
|
||||
|
||||
public void init(View view) {
|
||||
suggestion = (TextView) view.findViewById(R.id.suggest);//建议文本
|
||||
gradeText = (TextView) view.findViewById(R.id.grade);//评分文本
|
||||
//调用数据库
|
||||
MyDataBaseHelper dbHelp = new MyDataBaseHelper(getActivity());
|
||||
SQLiteDatabase sqLiteDatabase = dbHelp.getWritableDatabase();
|
||||
try {
|
||||
Cursor cursor = sqLiteDatabase.rawQuery("select * from grade_table", null);//从数据库中查询数据
|
||||
|
||||
int counts = Math.min(cursor.getCount(), 7);
|
||||
String[] time = new String[counts];//存储时间的数组
|
||||
double[] grade = new double[counts];//存储评分的数组
|
||||
if (cursor.moveToLast() == true) {
|
||||
for (int i = 0; i < counts; i++) {
|
||||
time[i] = cursor.getString(cursor.getColumnIndex("time"));//获取时间并存储到数组中
|
||||
grade[i] = cursor.getDouble(cursor.getColumnIndex("grade"));//获取评分并存储到数组中
|
||||
cursor.moveToPrevious();//移动到上一行
|
||||
}
|
||||
}
|
||||
if (time.length != 0) {
|
||||
mSimpleLineChart = (SimpleLineChart) view.findViewById(R.id.simpleLineChart);//获取折线图组件
|
||||
String[] yItem = {"100", "80", "60", "40", "20", "0"};
|
||||
mSimpleLineChart.setXItem(time);//设置x轴刻度标签
|
||||
mSimpleLineChart.setYItem(yItem);//设置y周刻度标签
|
||||
HashMap<Integer, Double> pointMap = new HashMap();//存储折线点的hashmap
|
||||
for (int i = 0; i < time.length; i++) {
|
||||
pointMap.put(i, grade[i] / 100);//将时间和评分的映射存储到hashmap中
|
||||
}
|
||||
mSimpleLineChart.setData(pointMap);//设置折线图的数据
|
||||
SharedPreferences sharedpref = getActivity().getSharedPreferences("info", MODE_PRIVATE);
|
||||
String suggest = sharedpref.getString("suggestion", "");//建议文本
|
||||
float gra = sharedpref.getFloat("grade", 0);//评分
|
||||
suggestion.setText("睡眠助手的建议:\n"+ suggest);
|
||||
long x = Math.round(gra);
|
||||
gradeText.setText(x + "");
|
||||
} else {
|
||||
suggestion.setText("睡眠助手的建议:\n体验一下我们的app吧~");//时间组为空时,显示
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.i("e", e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void getSleepTime(){
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,198 @@
|
||||
package com.example.sleep.database;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.example.sleep.App;
|
||||
import com.example.sleep.database.dao.DaoSession;
|
||||
import com.example.sleep.database.dao.RecordBeanDao;
|
||||
import com.example.sleep.database.dao.RemindBeanDao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* 处理睡眠记录数据库
|
||||
*/
|
||||
public class GetRecord {
|
||||
private RecordBeanDao recordBeanDao;
|
||||
private RemindBeanDao remindBeanDao;
|
||||
private static final String TAG = "GreenDao";
|
||||
private static GetRecord getRecord;
|
||||
|
||||
private GetRecord() {
|
||||
DaoSession mDaoSession = App.mDaoSession;
|
||||
recordBeanDao = mDaoSession.getRecordBeanDao();
|
||||
remindBeanDao = mDaoSession.getRemindBeanDao();
|
||||
}
|
||||
|
||||
public static GetRecord getRecord() {
|
||||
if (getRecord == null) {
|
||||
getRecord = new GetRecord();
|
||||
}
|
||||
return getRecord;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入睡眠记录
|
||||
*
|
||||
* @param date 日期
|
||||
* @param startTime 开始时间
|
||||
* @return 睡眠记录
|
||||
*/
|
||||
public RecordBean insertData(String date, String startTime) {
|
||||
RecordBean mRecord = new RecordBean(null, date, startTime, startTime, 0,
|
||||
false, 0, 0, 0, "", false);
|
||||
try {
|
||||
recordBeanDao.insert(mRecord);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "数据库插入失败");
|
||||
}
|
||||
return mRecord;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id删除睡眠记录
|
||||
*
|
||||
* @param id 记录id
|
||||
*/
|
||||
public void deleteById(Long id) {
|
||||
try {
|
||||
recordBeanDao.deleteByKey(id);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "数据库删除失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据记录删除
|
||||
*
|
||||
* @param mRecord 记录对象
|
||||
*/
|
||||
public void delete(RecordBean mRecord) {
|
||||
try {
|
||||
recordBeanDao.delete(mRecord);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "数据库删除失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新记录
|
||||
*
|
||||
* @param mRecord 记录对象
|
||||
* @param sleepDetail 要新增的信息
|
||||
*/
|
||||
public void update(RecordBean mRecord, String sleepDetail) {
|
||||
if (mRecord != null) {
|
||||
mRecord.setSleepDetail(mRecord.getSleepDetail() + sleepDetail);
|
||||
recordBeanDao.update(mRecord);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 完成一次睡眠记录时的最终更新
|
||||
*
|
||||
* @param mRecord 睡眠记录对象
|
||||
* @param endHour 结束时间
|
||||
* @param endMin 结束时间
|
||||
* @param totalTime 总时间
|
||||
* @param deepTime 深度睡眠时间
|
||||
* @param swallowTime 浅层睡眠时间
|
||||
* @param awakeTime 醒的时间
|
||||
*/
|
||||
public void finalUpdate(RecordBean mRecord, int endHour, int endMin, long totalTime,
|
||||
int deepTime, int swallowTime, int awakeTime) {
|
||||
totalTime /= 1000 * 60;
|
||||
if (totalTime > 2) {
|
||||
mRecord.setDrawChart(true);
|
||||
}
|
||||
mRecord.setEndTime(String.format(Locale.getDefault(), "%02d:%02d", endHour, endMin));
|
||||
mRecord.setTotalTime((int) totalTime);
|
||||
mRecord.setDeepTime(deepTime);
|
||||
mRecord.setSwallowTime(swallowTime);
|
||||
mRecord.setAwakeTime(awakeTime);
|
||||
mRecord.setValid(true);
|
||||
recordBeanDao.update(mRecord);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有记录
|
||||
*
|
||||
* @return 记录对象的列表
|
||||
*/
|
||||
public List queryAllList() {
|
||||
return recordBeanDao.queryBuilder().orderDesc(RecordBeanDao.Properties.Id).list();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按日期查找
|
||||
*
|
||||
* @param date 日期
|
||||
* @return 记录的对象
|
||||
*/
|
||||
public List queryByDate(String date) {
|
||||
return recordBeanDao.queryBuilder().where(RecordBeanDao.Properties.Date.eq(date)).orderAsc(RecordBeanDao.Properties.Id).list();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按月查找
|
||||
*
|
||||
* @param month 月份
|
||||
* @param days 对应的月有多少天
|
||||
* @return 每天是否有记录
|
||||
*/
|
||||
public boolean[] queryByMonth(String month, int days) {
|
||||
boolean[] result = new boolean[days + 1];
|
||||
String date;
|
||||
for (int i = 1; i <= days; ++i) {
|
||||
date = month + "-" + i;
|
||||
result[i] = !recordBeanDao.queryBuilder().where(RecordBeanDao.Properties.Date.eq(date)).build().list().isEmpty();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过id获取记录
|
||||
*/
|
||||
public RecordBean getRecordById(long id) {
|
||||
return recordBeanDao.queryBuilder().where(RecordBeanDao.Properties.Id.eq(id)).build().unique();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得最新的记录
|
||||
*/
|
||||
public RecordBean getLatestRecord() {
|
||||
List<RecordBean> records = recordBeanDao.queryBuilder().orderDesc(RecordBeanDao.Properties.Id).list();
|
||||
if (!records.isEmpty())
|
||||
return records.get(0);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改提醒时间
|
||||
*
|
||||
* @param time 时间
|
||||
*/
|
||||
public void updateRemind(String time) {
|
||||
if (remindBeanDao.queryBuilder().list().isEmpty()) {
|
||||
remindBeanDao.insert(new RemindBean(null, "22:00"));
|
||||
}
|
||||
RemindBean remindBean = remindBeanDao.queryBuilder().list().get(0);
|
||||
if (remindBean != null) {
|
||||
remindBean.setTime(time);
|
||||
remindBeanDao.update(remindBean);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取提醒时间
|
||||
*
|
||||
* @return 提醒的时间
|
||||
*/
|
||||
public String getRemind() {
|
||||
if (remindBeanDao.queryBuilder().list().isEmpty()) {
|
||||
remindBeanDao.insert(new RemindBean(null, "22:00"));
|
||||
}
|
||||
return remindBeanDao.queryBuilder().list().get(0).getTime();
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package com.example.sleep.database;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.util.Log;
|
||||
|
||||
public class MyDataBaseHelper extends SQLiteOpenHelper//用于创建和更新数据库
|
||||
{
|
||||
private static final String DB_NAME = "DB2.db";
|
||||
private static final String TABLE_NAME = "grade_table";
|
||||
private static final int DB_VERSION = 1;
|
||||
|
||||
|
||||
// 用户状态表
|
||||
public static final String TABLE_STATUS = "statusTable";
|
||||
public static final String STATUS_ID = "status_id";
|
||||
public static final String USER_NAME = "user_name";
|
||||
public static final String CONTENT = "content";
|
||||
public static final String IMAGE_URL = "image_url";
|
||||
public static final String TIMESTAMP = "timestamp";
|
||||
|
||||
// 评论表
|
||||
public static final String TABLE_COMMENTS = "commentsTable";
|
||||
public static final String COMMENT_ID = "comment_id";
|
||||
public static final String COMMENTER = "commenter";
|
||||
public static final String COMMENT = "comment";
|
||||
|
||||
public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
|
||||
super(context, name, factory, version);
|
||||
}
|
||||
|
||||
public MyDataBaseHelper(Context context) {
|
||||
this(context, DB_NAME, null, DB_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
try {
|
||||
//创建成绩表
|
||||
String CREATE_TABLE = "CREATE TABLE if not exists " + TABLE_NAME
|
||||
+ "(_id INTEGER PRIMARY KEY autoincrement,time TEXT," +
|
||||
"sumOfSleep INTEGER," +
|
||||
"timeOfSleep DOUBLE,grade DOUBLE)";
|
||||
db.execSQL(CREATE_TABLE);
|
||||
//创建用户状态表
|
||||
String createStatusTable = "CREATE TABLE " + TABLE_STATUS + "(" +
|
||||
STATUS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
|
||||
USER_NAME + " TEXT," +
|
||||
CONTENT + " TEXT," +
|
||||
IMAGE_URL + " TEXT," +
|
||||
TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP)";
|
||||
//创建评论表
|
||||
String createCommentsTable = "CREATE TABLE " + TABLE_COMMENTS + "(" +
|
||||
COMMENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
|
||||
STATUS_ID + " INTEGER," +
|
||||
COMMENTER + " TEXT," +
|
||||
COMMENT + " TEXT," +
|
||||
TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP," +
|
||||
"FOREIGN KEY(" + STATUS_ID + ") REFERENCES " + TABLE_STATUS + "(" + STATUS_ID + "))";
|
||||
|
||||
db.execSQL(createStatusTable);//执行创建用户状态表的SQL语句
|
||||
db.execSQL(createCommentsTable);//执行创建评论表的SQL语句
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.i("e", e.toString());//若出现异常,记录错误日志
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
System.out.println("database update!");//输出日志,表示数据库正在更新
|
||||
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATUS);//删除用户状态表
|
||||
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);//删除评论表
|
||||
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);//删除成绩表
|
||||
onCreate(db);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.example.sleep.database;
|
||||
|
||||
import org.greenrobot.greendao.annotation.Entity;
|
||||
import org.greenrobot.greendao.annotation.Generated;
|
||||
import org.greenrobot.greendao.annotation.Id;
|
||||
//声明实体类
|
||||
@Entity
|
||||
public class RemindBean
|
||||
{
|
||||
@Id(autoincrement = true)
|
||||
private Long id;//主键
|
||||
private String time;//time
|
||||
|
||||
@Generated(hash = 1395260710)
|
||||
public RemindBean(Long id, String time) {
|
||||
this.id = id;
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
@Generated(hash = 1914622572)
|
||||
public RemindBean() {
|
||||
}
|
||||
//对上述设置的值进行set和get方法
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTime() {
|
||||
return this.time;
|
||||
}
|
||||
|
||||
public void setTime(String time) {
|
||||
this.time = time;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.example.sleep.database;
|
||||
|
||||
import org.greenrobot.greendao.annotation.Entity;
|
||||
import org.greenrobot.greendao.annotation.Generated;
|
||||
import org.greenrobot.greendao.annotation.Id;
|
||||
//声明实体类
|
||||
@Entity
|
||||
public class UserBean {
|
||||
@Id(autoincrement = true)
|
||||
|
||||
private Long id;
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
@Generated(hash = 2052951463)
|
||||
public UserBean(Long id, String username, String password) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Generated(hash = 1203313951)
|
||||
public UserBean() {
|
||||
}
|
||||
//对设置的值进行set和get操作
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
package com.example.sleep.database.dao;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteDatabase.CursorFactory;
|
||||
import android.util.Log;
|
||||
|
||||
import org.greenrobot.greendao.AbstractDaoMaster;
|
||||
import org.greenrobot.greendao.database.StandardDatabase;
|
||||
import org.greenrobot.greendao.database.Database;
|
||||
import org.greenrobot.greendao.database.DatabaseOpenHelper;
|
||||
import org.greenrobot.greendao.identityscope.IdentityScopeType;
|
||||
|
||||
|
||||
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
|
||||
/**
|
||||
* Master of DAO (schema version 2): knows all DAOs.
|
||||
*/
|
||||
public class DaoMaster extends AbstractDaoMaster {
|
||||
public static final int SCHEMA_VERSION = 2;
|
||||
|
||||
/** Creates underlying database table using DAOs. */
|
||||
public static void createAllTables(Database db, boolean ifNotExists) {
|
||||
RecordBeanDao.createTable(db, ifNotExists);
|
||||
RemindBeanDao.createTable(db, ifNotExists);
|
||||
UserBeanDao.createTable(db, ifNotExists);
|
||||
}
|
||||
|
||||
/** Drops underlying database table using DAOs. */
|
||||
public static void dropAllTables(Database db, boolean ifExists) {
|
||||
RecordBeanDao.dropTable(db, ifExists);
|
||||
RemindBeanDao.dropTable(db, ifExists);
|
||||
UserBeanDao.dropTable(db, ifExists);
|
||||
}
|
||||
|
||||
/**
|
||||
* WARNING: Drops all table on Upgrade! Use only during development.
|
||||
* Convenience method using a {@link DevOpenHelper}.
|
||||
*/
|
||||
public static DaoSession newDevSession(Context context, String name) {
|
||||
Database db = new DevOpenHelper(context, name).getWritableDb();
|
||||
DaoMaster daoMaster = new DaoMaster(db);
|
||||
return daoMaster.newSession();
|
||||
}
|
||||
|
||||
public DaoMaster(SQLiteDatabase db) {
|
||||
this(new StandardDatabase(db));
|
||||
}
|
||||
|
||||
public DaoMaster(Database db) {
|
||||
super(db, SCHEMA_VERSION);
|
||||
registerDaoClass(RecordBeanDao.class);
|
||||
registerDaoClass(RemindBeanDao.class);
|
||||
registerDaoClass(UserBeanDao.class);
|
||||
}
|
||||
|
||||
public DaoSession newSession() {
|
||||
return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
|
||||
}
|
||||
|
||||
public DaoSession newSession(IdentityScopeType type) {
|
||||
return new DaoSession(db, type, daoConfigMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} -
|
||||
*/
|
||||
public static abstract class OpenHelper extends DatabaseOpenHelper {
|
||||
public OpenHelper(Context context, String name) {
|
||||
super(context, name, SCHEMA_VERSION);
|
||||
}
|
||||
|
||||
public OpenHelper(Context context, String name, CursorFactory factory) {
|
||||
super(context, name, factory, SCHEMA_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Database db) {
|
||||
Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
|
||||
createAllTables(db, false);
|
||||
}
|
||||
}
|
||||
|
||||
/** WARNING: Drops all table on Upgrade! Use only during development. */
|
||||
public static class DevOpenHelper extends OpenHelper {
|
||||
public DevOpenHelper(Context context, String name) {
|
||||
super(context, name);
|
||||
}
|
||||
|
||||
public DevOpenHelper(Context context, String name, CursorFactory factory) {
|
||||
super(context, name, factory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(Database db, int oldVersion, int newVersion) {
|
||||
Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
|
||||
dropAllTables(db, true);
|
||||
onCreate(db);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package com.example.sleep.database.dao;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.greenrobot.greendao.AbstractDao;
|
||||
import org.greenrobot.greendao.AbstractDaoSession;
|
||||
import org.greenrobot.greendao.database.Database;
|
||||
import org.greenrobot.greendao.identityscope.IdentityScopeType;
|
||||
import org.greenrobot.greendao.internal.DaoConfig;
|
||||
|
||||
import com.example.sleep.database.RecordBean;
|
||||
import com.example.sleep.database.RemindBean;
|
||||
import com.example.sleep.database.UserBean;
|
||||
|
||||
import com.example.sleep.database.dao.RecordBeanDao;
|
||||
import com.example.sleep.database.dao.RemindBeanDao;
|
||||
import com.example.sleep.database.dao.UserBeanDao;
|
||||
|
||||
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @see org.greenrobot.greendao.AbstractDaoSession
|
||||
*/
|
||||
public class DaoSession extends AbstractDaoSession {
|
||||
|
||||
private final DaoConfig recordBeanDaoConfig;
|
||||
private final DaoConfig remindBeanDaoConfig;
|
||||
private final DaoConfig userBeanDaoConfig;
|
||||
|
||||
private final RecordBeanDao recordBeanDao;
|
||||
private final RemindBeanDao remindBeanDao;
|
||||
private final UserBeanDao userBeanDao;
|
||||
|
||||
public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends AbstractDao<?, ?>>, DaoConfig>
|
||||
daoConfigMap) {
|
||||
super(db);
|
||||
|
||||
recordBeanDaoConfig = daoConfigMap.get(RecordBeanDao.class).clone();
|
||||
recordBeanDaoConfig.initIdentityScope(type);
|
||||
|
||||
remindBeanDaoConfig = daoConfigMap.get(RemindBeanDao.class).clone();
|
||||
remindBeanDaoConfig.initIdentityScope(type);
|
||||
|
||||
userBeanDaoConfig = daoConfigMap.get(UserBeanDao.class).clone();
|
||||
userBeanDaoConfig.initIdentityScope(type);
|
||||
|
||||
recordBeanDao = new RecordBeanDao(recordBeanDaoConfig, this);
|
||||
remindBeanDao = new RemindBeanDao(remindBeanDaoConfig, this);
|
||||
userBeanDao = new UserBeanDao(userBeanDaoConfig, this);
|
||||
|
||||
registerDao(RecordBean.class, recordBeanDao);
|
||||
registerDao(RemindBean.class, remindBeanDao);
|
||||
registerDao(UserBean.class, userBeanDao);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
recordBeanDaoConfig.clearIdentityScope();
|
||||
remindBeanDaoConfig.clearIdentityScope();
|
||||
userBeanDaoConfig.clearIdentityScope();
|
||||
}
|
||||
|
||||
public RecordBeanDao getRecordBeanDao() {
|
||||
return recordBeanDao;
|
||||
}
|
||||
|
||||
public RemindBeanDao getRemindBeanDao() {
|
||||
return remindBeanDao;
|
||||
}
|
||||
|
||||
public UserBeanDao getUserBeanDao() {
|
||||
return userBeanDao;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
package com.example.sleep.database.dao;
|
||||
|
||||
import static com.example.sleep.database.MyDataBaseHelper.CONTENT;
|
||||
import static com.example.sleep.database.MyDataBaseHelper.IMAGE_URL;
|
||||
import static com.example.sleep.database.MyDataBaseHelper.TIMESTAMP;
|
||||
import static com.example.sleep.database.MyDataBaseHelper.USER_NAME;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import com.example.sleep.ItemModel;
|
||||
import com.example.sleep.database.MyDataBaseHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DatabaseManager {
|
||||
private MyDataBaseHelper dbHelper;
|
||||
private SQLiteDatabase database;
|
||||
|
||||
public DatabaseManager(Context context) {
|
||||
dbHelper = new MyDataBaseHelper(context);
|
||||
}
|
||||
|
||||
public void open() {
|
||||
database = dbHelper.getWritableDatabase();
|
||||
}
|
||||
|
||||
public void close() {
|
||||
dbHelper.close();
|
||||
}
|
||||
|
||||
// A用户发表心情
|
||||
public long postMood(String userName, String content, String imageUrl) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(USER_NAME, userName);
|
||||
values.put(CONTENT, content);
|
||||
values.put(IMAGE_URL, imageUrl);
|
||||
|
||||
return database.insert(MyDataBaseHelper.TABLE_STATUS, null, values);
|
||||
}
|
||||
|
||||
public List<ItemModel> getUserNameEvent(String userName) {
|
||||
// 使用参数化查询以防止SQL注入
|
||||
String selection = USER_NAME + " = ?";
|
||||
String[] selectionArgs = {userName};
|
||||
List<ItemModel> dataList = new ArrayList<>();
|
||||
Cursor cursor = database.query(MyDataBaseHelper.TABLE_STATUS, null, selection, selectionArgs, null, null, TIMESTAMP + " DESC");
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
String text = cursor.getString(cursor.getColumnIndex(CONTENT));
|
||||
String imageUrl = cursor.getString(cursor.getColumnIndex(IMAGE_URL));
|
||||
|
||||
// 根据你的ItemModel的构造函数来适当地修改下一行
|
||||
dataList.add(new ItemModel(imageUrl, text));
|
||||
}
|
||||
|
||||
cursor.close();
|
||||
database.close();
|
||||
return dataList;
|
||||
}
|
||||
|
||||
// B用户评论A用户的心情
|
||||
public long commentMood(long statusId, String commenter, String comment) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(MyDataBaseHelper.STATUS_ID, statusId);
|
||||
values.put(MyDataBaseHelper.COMMENTER, commenter);
|
||||
values.put(MyDataBaseHelper.COMMENT, comment);
|
||||
|
||||
return database.insert(MyDataBaseHelper.TABLE_COMMENTS, null, values);
|
||||
}
|
||||
|
||||
// B用户查看A用户的心情和评论
|
||||
public Cursor viewMoodAndComments(long statusId) {
|
||||
String query = "SELECT * FROM " + MyDataBaseHelper.TABLE_STATUS + " s " +
|
||||
"LEFT JOIN " + MyDataBaseHelper.TABLE_COMMENTS + " c " +
|
||||
"ON s." + MyDataBaseHelper.STATUS_ID + " = c." + MyDataBaseHelper.STATUS_ID +
|
||||
" WHERE s." + MyDataBaseHelper.STATUS_ID + " = ?";
|
||||
|
||||
return database.rawQuery(query, new String[]{String.valueOf(statusId)});
|
||||
}
|
||||
|
||||
// A用户查看自己的心情和B用户的评论
|
||||
public Cursor viewOwnMoodAndComments(String userName) {
|
||||
String query = "SELECT * FROM " + MyDataBaseHelper.TABLE_STATUS + " s " +
|
||||
"LEFT JOIN " + MyDataBaseHelper.TABLE_COMMENTS + " c " +
|
||||
"ON s." + MyDataBaseHelper.STATUS_ID + " = c." + MyDataBaseHelper.STATUS_ID +
|
||||
" WHERE s." + USER_NAME + " = ?";
|
||||
|
||||
return database.rawQuery(query, new String[]{userName});
|
||||
}
|
||||
|
||||
|
||||
// 插入一条心情动态
|
||||
public long insertStatus(String userName, String content, String imageUrl) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(USER_NAME, userName);
|
||||
values.put(CONTENT, content);
|
||||
values.put(IMAGE_URL, imageUrl);
|
||||
|
||||
return database.insert(MyDataBaseHelper.TABLE_STATUS, null, values);
|
||||
}
|
||||
|
||||
// 插入一条评论
|
||||
public long insertComment(long statusId, String commenter, String comment) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(MyDataBaseHelper.STATUS_ID, statusId);
|
||||
values.put(MyDataBaseHelper.COMMENTER, commenter);
|
||||
values.put(MyDataBaseHelper.COMMENT, comment);
|
||||
|
||||
return database.insert(MyDataBaseHelper.TABLE_COMMENTS, null, values);
|
||||
}
|
||||
|
||||
public Cursor getStatusWithComments(long statusId) {
|
||||
String query = "SELECT * FROM " + MyDataBaseHelper.TABLE_STATUS + " s " +
|
||||
"LEFT JOIN " + MyDataBaseHelper.TABLE_COMMENTS + " c " +
|
||||
"ON s." + MyDataBaseHelper.STATUS_ID + " = c." + MyDataBaseHelper.STATUS_ID +
|
||||
" WHERE s." + MyDataBaseHelper.STATUS_ID + " = ?";
|
||||
|
||||
return database.rawQuery(query, new String[]{String.valueOf(statusId)});
|
||||
}
|
||||
}
|
@ -0,0 +1,207 @@
|
||||
package com.example.sleep.database.dao;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteStatement;
|
||||
|
||||
import org.greenrobot.greendao.AbstractDao;
|
||||
import org.greenrobot.greendao.Property;
|
||||
import org.greenrobot.greendao.internal.DaoConfig;
|
||||
import org.greenrobot.greendao.database.Database;
|
||||
import org.greenrobot.greendao.database.DatabaseStatement;
|
||||
|
||||
import com.example.sleep.database.RecordBean;
|
||||
|
||||
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
|
||||
/**
|
||||
* DAO for table "RECORD_BEAN".
|
||||
*/
|
||||
public class RecordBeanDao extends AbstractDao<RecordBean, Long> {
|
||||
|
||||
public static final String TABLENAME = "RECORD_BEAN";
|
||||
|
||||
/**
|
||||
* Properties of entity RecordBean.<br/>
|
||||
* Can be used for QueryBuilder and for referencing column names.
|
||||
*/
|
||||
public static class Properties {
|
||||
public final static Property Id = new Property(0, Long.class, "id", true, "_id");
|
||||
public final static Property Date = new Property(1, String.class, "date", false, "DATE");
|
||||
public final static Property StartTime = new Property(2, String.class, "startTime", false, "START_TIME");
|
||||
public final static Property EndTime = new Property(3, String.class, "endTime", false, "END_TIME");
|
||||
public final static Property TotalTime = new Property(4, int.class, "totalTime", false, "TOTAL_TIME");
|
||||
public final static Property DrawChart = new Property(5, boolean.class, "drawChart", false, "DRAW_CHART");
|
||||
public final static Property DeepTime = new Property(6, int.class, "deepTime", false, "DEEP_TIME");
|
||||
public final static Property SwallowTime = new Property(7, int.class, "swallowTime", false, "SWALLOW_TIME");
|
||||
public final static Property AwakeTime = new Property(8, int.class, "awakeTime", false, "AWAKE_TIME");
|
||||
public final static Property SleepDetail = new Property(9, String.class, "sleepDetail", false, "SLEEP_DETAIL");
|
||||
public final static Property Valid = new Property(10, boolean.class, "valid", false, "VALID");
|
||||
}
|
||||
|
||||
|
||||
public RecordBeanDao(DaoConfig config) {
|
||||
super(config);
|
||||
}
|
||||
|
||||
public RecordBeanDao(DaoConfig config, DaoSession daoSession) {
|
||||
super(config, daoSession);
|
||||
}
|
||||
|
||||
/** Creates the underlying database table. */
|
||||
public static void createTable(Database db, boolean ifNotExists) {
|
||||
String constraint = ifNotExists? "IF NOT EXISTS ": "";
|
||||
db.execSQL("CREATE TABLE " + constraint + "\"RECORD_BEAN\" (" + //
|
||||
"\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
|
||||
"\"DATE\" TEXT," + // 1: date
|
||||
"\"START_TIME\" TEXT," + // 2: startTime
|
||||
"\"END_TIME\" TEXT," + // 3: endTime
|
||||
"\"TOTAL_TIME\" INTEGER NOT NULL ," + // 4: totalTime
|
||||
"\"DRAW_CHART\" INTEGER NOT NULL ," + // 5: drawChart
|
||||
"\"DEEP_TIME\" INTEGER NOT NULL ," + // 6: deepTime
|
||||
"\"SWALLOW_TIME\" INTEGER NOT NULL ," + // 7: swallowTime
|
||||
"\"AWAKE_TIME\" INTEGER NOT NULL ," + // 8: awakeTime
|
||||
"\"SLEEP_DETAIL\" TEXT," + // 9: sleepDetail
|
||||
"\"VALID\" INTEGER NOT NULL );"); // 10: valid
|
||||
}
|
||||
|
||||
/** Drops the underlying database table. */
|
||||
public static void dropTable(Database db, boolean ifExists) {
|
||||
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"RECORD_BEAN\"";
|
||||
db.execSQL(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindValues(DatabaseStatement stmt, RecordBean entity) {
|
||||
stmt.clearBindings();
|
||||
|
||||
Long id = entity.getId();
|
||||
if (id != null) {
|
||||
stmt.bindLong(1, id);
|
||||
}
|
||||
|
||||
String date = entity.getDate();
|
||||
if (date != null) {
|
||||
stmt.bindString(2, date);
|
||||
}
|
||||
|
||||
String startTime = entity.getStartTime();
|
||||
if (startTime != null) {
|
||||
stmt.bindString(3, startTime);
|
||||
}
|
||||
|
||||
String endTime = entity.getEndTime();
|
||||
if (endTime != null) {
|
||||
stmt.bindString(4, endTime);
|
||||
}
|
||||
stmt.bindLong(5, entity.getTotalTime());
|
||||
stmt.bindLong(6, entity.getDrawChart() ? 1L: 0L);
|
||||
stmt.bindLong(7, entity.getDeepTime());
|
||||
stmt.bindLong(8, entity.getSwallowTime());
|
||||
stmt.bindLong(9, entity.getAwakeTime());
|
||||
|
||||
String sleepDetail = entity.getSleepDetail();
|
||||
if (sleepDetail != null) {
|
||||
stmt.bindString(10, sleepDetail);
|
||||
}
|
||||
stmt.bindLong(11, entity.getValid() ? 1L: 0L);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindValues(SQLiteStatement stmt, RecordBean entity) {
|
||||
stmt.clearBindings();
|
||||
|
||||
Long id = entity.getId();
|
||||
if (id != null) {
|
||||
stmt.bindLong(1, id);
|
||||
}
|
||||
|
||||
String date = entity.getDate();
|
||||
if (date != null) {
|
||||
stmt.bindString(2, date);
|
||||
}
|
||||
|
||||
String startTime = entity.getStartTime();
|
||||
if (startTime != null) {
|
||||
stmt.bindString(3, startTime);
|
||||
}
|
||||
|
||||
String endTime = entity.getEndTime();
|
||||
if (endTime != null) {
|
||||
stmt.bindString(4, endTime);
|
||||
}
|
||||
stmt.bindLong(5, entity.getTotalTime());
|
||||
stmt.bindLong(6, entity.getDrawChart() ? 1L: 0L);
|
||||
stmt.bindLong(7, entity.getDeepTime());
|
||||
stmt.bindLong(8, entity.getSwallowTime());
|
||||
stmt.bindLong(9, entity.getAwakeTime());
|
||||
|
||||
String sleepDetail = entity.getSleepDetail();
|
||||
if (sleepDetail != null) {
|
||||
stmt.bindString(10, sleepDetail);
|
||||
}
|
||||
stmt.bindLong(11, entity.getValid() ? 1L: 0L);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long readKey(Cursor cursor, int offset) {
|
||||
return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecordBean readEntity(Cursor cursor, int offset) {
|
||||
RecordBean entity = new RecordBean( //
|
||||
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
|
||||
cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // date
|
||||
cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // startTime
|
||||
cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // endTime
|
||||
cursor.getInt(offset + 4), // totalTime
|
||||
cursor.getShort(offset + 5) != 0, // drawChart
|
||||
cursor.getInt(offset + 6), // deepTime
|
||||
cursor.getInt(offset + 7), // swallowTime
|
||||
cursor.getInt(offset + 8), // awakeTime
|
||||
cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // sleepDetail
|
||||
cursor.getShort(offset + 10) != 0 // valid
|
||||
);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readEntity(Cursor cursor, RecordBean entity, int offset) {
|
||||
entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
|
||||
entity.setDate(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
|
||||
entity.setStartTime(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
|
||||
entity.setEndTime(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
|
||||
entity.setTotalTime(cursor.getInt(offset + 4));
|
||||
entity.setDrawChart(cursor.getShort(offset + 5) != 0);
|
||||
entity.setDeepTime(cursor.getInt(offset + 6));
|
||||
entity.setSwallowTime(cursor.getInt(offset + 7));
|
||||
entity.setAwakeTime(cursor.getInt(offset + 8));
|
||||
entity.setSleepDetail(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
|
||||
entity.setValid(cursor.getShort(offset + 10) != 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Long updateKeyAfterInsert(RecordBean entity, long rowId) {
|
||||
entity.setId(rowId);
|
||||
return rowId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getKey(RecordBean entity) {
|
||||
if(entity != null) {
|
||||
return entity.getId();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasKey(RecordBean entity) {
|
||||
return entity.getId() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final boolean isEntityUpdateable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
package com.example.sleep.database.dao;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteStatement;
|
||||
|
||||
import org.greenrobot.greendao.AbstractDao;
|
||||
import org.greenrobot.greendao.Property;
|
||||
import org.greenrobot.greendao.internal.DaoConfig;
|
||||
import org.greenrobot.greendao.database.Database;
|
||||
import org.greenrobot.greendao.database.DatabaseStatement;
|
||||
|
||||
import com.example.sleep.database.RemindBean;
|
||||
|
||||
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
|
||||
/**
|
||||
* DAO for table "REMIND_BEAN".
|
||||
*/
|
||||
public class RemindBeanDao extends AbstractDao<RemindBean, Long> {
|
||||
|
||||
public static final String TABLENAME = "REMIND_BEAN";
|
||||
|
||||
/**
|
||||
* Properties of entity RemindBean.<br/>
|
||||
* Can be used for QueryBuilder and for referencing column names.
|
||||
*/
|
||||
public static class Properties {
|
||||
public final static Property Id = new Property(0, Long.class, "id", true, "_id");
|
||||
public final static Property Time = new Property(1, String.class, "time", false, "TIME");
|
||||
}
|
||||
|
||||
|
||||
public RemindBeanDao(DaoConfig config) {
|
||||
super(config);
|
||||
}
|
||||
|
||||
public RemindBeanDao(DaoConfig config, DaoSession daoSession) {
|
||||
super(config, daoSession);
|
||||
}
|
||||
|
||||
/** Creates the underlying database table. */
|
||||
public static void createTable(Database db, boolean ifNotExists) {
|
||||
String constraint = ifNotExists? "IF NOT EXISTS ": "";
|
||||
db.execSQL("CREATE TABLE " + constraint + "\"REMIND_BEAN\" (" + //
|
||||
"\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
|
||||
"\"TIME\" TEXT);"); // 1: time
|
||||
}
|
||||
|
||||
/** Drops the underlying database table. */
|
||||
public static void dropTable(Database db, boolean ifExists) {
|
||||
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"REMIND_BEAN\"";
|
||||
db.execSQL(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindValues(DatabaseStatement stmt, RemindBean entity) {
|
||||
stmt.clearBindings();
|
||||
|
||||
Long id = entity.getId();
|
||||
if (id != null) {
|
||||
stmt.bindLong(1, id);
|
||||
}
|
||||
|
||||
String time = entity.getTime();
|
||||
if (time != null) {
|
||||
stmt.bindString(2, time);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindValues(SQLiteStatement stmt, RemindBean entity) {
|
||||
stmt.clearBindings();
|
||||
|
||||
Long id = entity.getId();
|
||||
if (id != null) {
|
||||
stmt.bindLong(1, id);
|
||||
}
|
||||
|
||||
String time = entity.getTime();
|
||||
if (time != null) {
|
||||
stmt.bindString(2, time);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long readKey(Cursor cursor, int offset) {
|
||||
return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemindBean readEntity(Cursor cursor, int offset) {
|
||||
RemindBean entity = new RemindBean( //
|
||||
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
|
||||
cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1) // time
|
||||
);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readEntity(Cursor cursor, RemindBean entity, int offset) {
|
||||
entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
|
||||
entity.setTime(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Long updateKeyAfterInsert(RemindBean entity, long rowId) {
|
||||
entity.setId(rowId);
|
||||
return rowId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getKey(RemindBean entity) {
|
||||
if(entity != null) {
|
||||
return entity.getId();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasKey(RemindBean entity) {
|
||||
return entity.getId() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final boolean isEntityUpdateable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
package com.example.sleep.database.dao;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteStatement;
|
||||
|
||||
import org.greenrobot.greendao.AbstractDao;
|
||||
import org.greenrobot.greendao.Property;
|
||||
import org.greenrobot.greendao.internal.DaoConfig;
|
||||
import org.greenrobot.greendao.database.Database;
|
||||
import org.greenrobot.greendao.database.DatabaseStatement;
|
||||
|
||||
import com.example.sleep.database.UserBean;
|
||||
|
||||
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
|
||||
/**
|
||||
* DAO for table "USER_BEAN".
|
||||
*/
|
||||
public class UserBeanDao extends AbstractDao<UserBean, Long> {
|
||||
|
||||
public static final String TABLENAME = "USER_BEAN";
|
||||
|
||||
/**
|
||||
* Properties of entity UserBean.<br/>
|
||||
* Can be used for QueryBuilder and for referencing column names.
|
||||
*/
|
||||
public static class Properties {
|
||||
public final static Property Id = new Property(0, Long.class, "id", true, "_id");
|
||||
public final static Property Username = new Property(1, String.class, "username", false, "USERNAME");
|
||||
public final static Property Password = new Property(2, String.class, "password", false, "PASSWORD");
|
||||
}
|
||||
|
||||
|
||||
public UserBeanDao(DaoConfig config) {
|
||||
super(config);
|
||||
}
|
||||
|
||||
public UserBeanDao(DaoConfig config, DaoSession daoSession) {
|
||||
super(config, daoSession);
|
||||
}
|
||||
|
||||
/** Creates the underlying database table. */
|
||||
public static void createTable(Database db, boolean ifNotExists) {
|
||||
String constraint = ifNotExists? "IF NOT EXISTS ": "";
|
||||
db.execSQL("CREATE TABLE " + constraint + "\"USER_BEAN\" (" + //
|
||||
"\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
|
||||
"\"USERNAME\" TEXT," + // 1: username
|
||||
"\"PASSWORD\" TEXT);"); // 2: password
|
||||
}
|
||||
|
||||
/** Drops the underlying database table. */
|
||||
public static void dropTable(Database db, boolean ifExists) {
|
||||
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"USER_BEAN\"";
|
||||
db.execSQL(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindValues(DatabaseStatement stmt, UserBean entity) {
|
||||
stmt.clearBindings();
|
||||
|
||||
Long id = entity.getId();
|
||||
if (id != null) {
|
||||
stmt.bindLong(1, id);
|
||||
}
|
||||
|
||||
String username = entity.getUsername();
|
||||
if (username != null) {
|
||||
stmt.bindString(2, username);
|
||||
}
|
||||
|
||||
String password = entity.getPassword();
|
||||
if (password != null) {
|
||||
stmt.bindString(3, password);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindValues(SQLiteStatement stmt, UserBean entity) {
|
||||
stmt.clearBindings();
|
||||
|
||||
Long id = entity.getId();
|
||||
if (id != null) {
|
||||
stmt.bindLong(1, id);
|
||||
}
|
||||
|
||||
String username = entity.getUsername();
|
||||
if (username != null) {
|
||||
stmt.bindString(2, username);
|
||||
}
|
||||
|
||||
String password = entity.getPassword();
|
||||
if (password != null) {
|
||||
stmt.bindString(3, password);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long readKey(Cursor cursor, int offset) {
|
||||
return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserBean readEntity(Cursor cursor, int offset) {
|
||||
UserBean entity = new UserBean( //
|
||||
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
|
||||
cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // username
|
||||
cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2) // password
|
||||
);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readEntity(Cursor cursor, UserBean entity, int offset) {
|
||||
entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
|
||||
entity.setUsername(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
|
||||
entity.setPassword(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Long updateKeyAfterInsert(UserBean entity, long rowId) {
|
||||
entity.setId(rowId);
|
||||
return rowId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getKey(UserBean entity) {
|
||||
if(entity != null) {
|
||||
return entity.getId();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasKey(UserBean entity) {
|
||||
return entity.getId() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final boolean isEntityUpdateable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue