Merge pull request '1' (#3) from zzw_branch into develop
commit
6666509e3e
@ -0,0 +1,94 @@
|
||||
package com.example.sleep;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Base64;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.sleep.database.dao.DatabaseManager;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class EditScreen extends FragmentActivity {
|
||||
private ImageView showImage;
|
||||
private ImageView postImage;
|
||||
private String imageUrl;
|
||||
private EditText messageEdit;
|
||||
private Bitmap bitmap;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.event_content_activity);
|
||||
|
||||
Bundle extras = getIntent().getExtras();
|
||||
boolean isPhoto = getIntent().getBooleanExtra("isPhoto", false);
|
||||
|
||||
postImage = findViewById(R.id.event_content_post);
|
||||
messageEdit = findViewById(R.id.event_content);
|
||||
showImage = findViewById(R.id.event_image);
|
||||
|
||||
// 根据传入的数据决定是否显示图片
|
||||
if (isPhoto) {
|
||||
imageUrl = getIntent().getStringExtra("bitmapImageString");
|
||||
Bitmap bitmap = BitmapFactory.decodeFile(imageUrl);
|
||||
showImage.setImageBitmap(bitmap);
|
||||
} else {
|
||||
if (extras != null) {
|
||||
bitmap = extras.getParcelable("bitmapImage");
|
||||
if (bitmap != null) {
|
||||
showImage.setImageBitmap(bitmap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
postImage.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
String message = messageEdit.getText().toString();
|
||||
if (TextUtils.isEmpty(message)) {
|
||||
Toast.makeText(EditScreen.this, "请添加消息", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
// 从SharedPreferences获取用户名
|
||||
SharedPreferences sharedPreferences = EditScreen.this.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
|
||||
String retrievedUserName = sharedPreferences.getString("UserNameKey", "DefaultUserName");
|
||||
|
||||
// 打开数据库管理器
|
||||
DatabaseManager manager = new DatabaseManager(EditScreen.this);
|
||||
manager.open();
|
||||
|
||||
// 根据是否是照片贴文,将消息和图片信息保存到数据库
|
||||
if (isPhoto) {
|
||||
manager.postMood(retrievedUserName, message, imageUrl);
|
||||
} else {
|
||||
String bitmapUrl = bitmapToBase64(bitmap);
|
||||
manager.postMood(retrievedUserName, message, bitmapUrl);
|
||||
}
|
||||
|
||||
// 关闭数据库管理器
|
||||
manager.close();
|
||||
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 将Bitmap转换为Base64编码的字符串
|
||||
public String bitmapToBase64(Bitmap bitmap) {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
|
||||
byte[] byteArray = byteArrayOutputStream.toByteArray();
|
||||
return Base64.encodeToString(byteArray, Base64.DEFAULT);
|
||||
}
|
||||
}
|
@ -0,0 +1,122 @@
|
||||
package com.example.sleep;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
// 事件类
|
||||
public class Event implements Serializable {
|
||||
public String id; // 事件的唯一标识符
|
||||
public String title; // 事件的标题
|
||||
public String des; // 事件的描述
|
||||
public String imageUrl; // 事件的图片URL
|
||||
public List<EventContent> contents; // 包含事件内容的列表
|
||||
|
||||
// 事件类构造函数
|
||||
public Event(String id, String title, String des, String imageUrl, List<EventContent> contents) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.des = des;
|
||||
this.imageUrl = imageUrl;
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
// 无参构造函数
|
||||
public Event() {
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDes() {
|
||||
return des;
|
||||
}
|
||||
|
||||
public void setDes(String des) {
|
||||
this.des = des;
|
||||
}
|
||||
|
||||
public String getImageUrl() {
|
||||
return imageUrl;
|
||||
}
|
||||
|
||||
public void setImageUrl(String imageUrl) {
|
||||
this.imageUrl = imageUrl;
|
||||
}
|
||||
|
||||
public List<EventContent> getContents() {
|
||||
return contents;
|
||||
}
|
||||
|
||||
public void setContents(List<EventContent> contents) {
|
||||
this.contents = contents;
|
||||
}
|
||||
}
|
||||
|
||||
// 事件内容类
|
||||
class EventContent implements Serializable {
|
||||
public String contentId; // 内容的唯一标识符
|
||||
public String userId; // 用户的唯一标识符,与事件内容关联
|
||||
public String content; // 事件的具体内容
|
||||
public String userName; // 用户的名字
|
||||
|
||||
// 事件内容类构造函数
|
||||
public EventContent(String contentId, String userId, String content, String userName) {
|
||||
this.contentId = contentId;
|
||||
this.userId = userId;
|
||||
this.content = content;
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
// 无参构造函数
|
||||
public EventContent() {
|
||||
}
|
||||
|
||||
public String getContentId() {
|
||||
return contentId;
|
||||
}
|
||||
|
||||
public void setContentId(String contentId) {
|
||||
this.contentId = contentId;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
package com.example.sleep;
|
||||
|
||||
public class ItemModel {
|
||||
private String imageUrl; // 用于存储图像URL或资源标识符
|
||||
private String text; // 用于存储文本信息
|
||||
|
||||
// 构造函数,用于创建 ItemModel 对象并初始化其属性
|
||||
public ItemModel(String imageUrl, String text) {
|
||||
this.imageUrl = imageUrl;
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
// 获取图像URL或资源标识符的方法
|
||||
public String getImageResId() {
|
||||
return imageUrl;
|
||||
}
|
||||
|
||||
// 获取文本信息的方法
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.example.sleep.recyclerView;
|
||||
|
||||
/**
|
||||
* 用来绘制记录的类
|
||||
*/
|
||||
public class Trace {
|
||||
private String Date; // 记录的日期
|
||||
private String Time; // 记录的时间
|
||||
|
||||
public Trace(String mDate, String mTime) {
|
||||
this.Date = mDate;
|
||||
this.Time = mTime;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return Date;
|
||||
}
|
||||
|
||||
public String getTime() {
|
||||
return Time;
|
||||
}
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
package com.example.sleep.recyclerView;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.example.sleep.R;
|
||||
import com.example.sleep.SleepresultScreen;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TraceListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
||||
private LayoutInflater inflater;
|
||||
private List<Trace> traceList;
|
||||
private Context context;
|
||||
private String date;
|
||||
|
||||
public class ViewHolder extends RecyclerView.ViewHolder {
|
||||
private TextView mDate, mTime;
|
||||
private TextView mTopLine;
|
||||
private ImageView mDot;
|
||||
|
||||
ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
mDate = itemView.findViewById(R.id.Date);
|
||||
mTime = itemView.findViewById(R.id.Time);
|
||||
mTopLine = itemView.findViewById(R.id.TopLine);
|
||||
mDot = itemView.findViewById(R.id.Dot);
|
||||
}
|
||||
|
||||
void bindHolder(Trace trace) {
|
||||
mDate.setText(trace.getDate());
|
||||
mTime.setText(trace.getTime());
|
||||
}
|
||||
}
|
||||
|
||||
public TraceListAdapter(Context context, List<Trace> traceList, String date) {
|
||||
// 构造函数,初始化适配器的数据
|
||||
this.context = context;
|
||||
inflater = LayoutInflater.from(context);
|
||||
this.traceList = traceList;
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
// 创建并返回记录项的 ViewHolder
|
||||
return new ViewHolder(inflater.inflate(R.layout.list_cell, parent, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* 绘制记录
|
||||
*
|
||||
* @param holder 记录的holder
|
||||
* @param position 记录的位置
|
||||
*/
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
|
||||
ViewHolder itemHolder = (ViewHolder) holder;
|
||||
if (position == 0) {
|
||||
// 第一行头的竖线不显示
|
||||
itemHolder.mTopLine.setVisibility(View.INVISIBLE);
|
||||
itemHolder.mDot.setBackgroundResource(R.drawable.timeline);
|
||||
} else if (position > 0) {
|
||||
itemHolder.mTopLine.setVisibility(View.VISIBLE);
|
||||
itemHolder.mDot.setBackgroundResource(R.drawable.timeline);
|
||||
}
|
||||
itemHolder.bindHolder(traceList.get(position));
|
||||
final TextView tv1 = holder.itemView.findViewById(R.id.Date);
|
||||
final TextView tv2 = holder.itemView.findViewById(R.id.Time);
|
||||
tv1.setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// 点击日期文本的事件处理
|
||||
Intent i = new Intent(context, SleepresultScreen.class);
|
||||
i.putExtra("date", date);
|
||||
i.putExtra("position", holder.getAdapterPosition());
|
||||
context.startActivity(i);
|
||||
}
|
||||
}
|
||||
);
|
||||
tv2.setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// 点击时间文本的事件处理
|
||||
Intent i = new Intent(context, SleepresultScreen.class);
|
||||
i.putExtra("date", date);
|
||||
i.putExtra("position", holder.getAdapterPosition());
|
||||
context.startActivity(i);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return traceList.size();
|
||||
}
|
||||
|
||||
public interface OnItemClickListener {
|
||||
void onItemClick(View view, int position);
|
||||
}
|
||||
|
||||
private OnItemClickListener mOnItemClickListener;
|
||||
|
||||
public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
|
||||
// 设置 RecyclerView 点击事件的监听器
|
||||
this.mOnItemClickListener = mOnItemClickListener;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue