1. 涉及到的技术点
- 使用SQLite数据库实现用户评论数据保存
- 使用RecyclerView+adapter实现用户评论列表
2. 具体代码实现过程
- CommentListAdapter.java适配器
public class CommentListAdapter extends RecyclerView.Adapter<CommentListAdapter.MyHolder> {
private List<CommentInfo> mCommentInfoList = new ArrayList<>();
public void setCommentInfoList(List<CommentInfo> commentInfoList) {
mCommentInfoList = commentInfoList;
notifyDataSetChanged();
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comments_list_item, parent, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
CommentInfo commentInfo = mCommentInfoList.get(position);
holder.tv_username.setText(commentInfo.getUsername());
holder.tv_content.setText(commentInfo.getComment());
holder.tv_time.setText(commentInfo.getComment_create_time());
holder.rb_star.setRating(commentInfo.getRating());
}
@Override
public int getItemCount() {
return mCommentInfoList.size();
}
static class MyHolder extends RecyclerView.ViewHolder {
TextView tv_username;
TextView tv_content;
TextView tv_time;
RatingBar rb_star;
public MyHolder(@NonNull View itemView) {
super(itemView);
tv_username = itemView.findViewById(R.id.tv_username);
tv_content = itemView.findViewById(R.id.tv_content);
tv_time = itemView.findViewById(R.id.tv_time);
rb_star = itemView.findViewById(R.id.rb_star);
}
}
}
- CommentDbHelper.java 数据库建表
public class CommentDbHelper extends SQLiteOpenHelper {
private static CommentDbHelper sHelper;
private static final String DB_NAME = "comment.db";
private static final int VERSION = 1;
public CommentDbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//创建单例,供使用调用该类里面的的增删改查的方法
public synchronized static CommentDbHelper getInstance(Context context) {
if (null == sHelper) {
sHelper = new CommentDbHelper(context, DB_NAME, null, VERSION);
}
return sHelper;
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建order_table表
db.execSQL("create table comment_table(comment_id integer primary key autoincrement, " +
"movie_name text," + //用户名
"username text," +
"comment text," +
"comment_create_time text," +
"rating float" +
")");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
/**
* 评论
*/
public int insertComment(CommentInfo commentInfo) {
//获取SQLiteDatabase实例
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
//填充占位符
values.put("movie_name", commentInfo.getMovie_name());
values.put("username", commentInfo.getUsername());
values.put("comment", commentInfo.getComment());
values.put("comment_create_time", getCurrentTime());
values.put("rating", commentInfo.getRating());
String nullColumnHack = "values(null,?,?,?,?,?)";
//执行
int insert = (int) db.insert("comment_table", nullColumnHack, values);
// db.close();
return insert;
}
/**
* 删除自己的评论
*/
public void deleteComment(int comment_id) {
SQLiteDatabase db = getWritableDatabase();
db.delete("comment_table", "comment_id=?", new String[]{String.valueOf(comment_id)});
db.close();
}
/**
* 根据电影名查询评论
*/
@SuppressLint("Range")
public List<CommentInfo> queryCommentByMovieName(String movie_name) {
List<CommentInfo> orderList = new ArrayList<>();
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.query("comment_table", null, "movie_name=?", new String[]{movie_name}, null, null, "comment_id desc");
while (cursor.moveToNext()) {
int comment_id = cursor.getInt(cursor.getColumnIndex("comment_id"));
String movieName = cursor.getString(cursor.getColumnIndex("movie_name"));
String username = cursor.getString(cursor.getColumnIndex("username"));
String comment = cursor.getString(cursor.getColumnIndex("comment"));
String comment_create_time = cursor.getString(cursor.getColumnIndex("comment_create_time"));
float rating = cursor.getFloat(cursor.getColumnIndex("rating"));
orderList.add(new CommentInfo(comment_id, movieName, username, comment, comment_create_time, rating));
}
return orderList;
}
/**
* 获取当前系统时间
*/
private String getCurrentTime() {
// 获取当前时间
Date currentDate = new Date();
// 创建一个日期格式化对象
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 将当前时间格式化为指定格式的字符串
String formattedDate = dateFormat.format(currentDate);
return formattedDate;
}
}
- CommentInfo.java 评论数据实体
public class CommentInfo {
private int comment_id;
private String movie_name;
private String username;
private String comment;
private String comment_create_time;
private float rating;
public CommentInfo() {
}
public CommentInfo(int comment_id, String movie_name, String username, String comment, String comment_create_time, float rating) {
this.comment_id = comment_id;
this.movie_name = movie_name;
this.username = username;
this.comment = comment;
this.comment_create_time = comment_create_time;
this.rating = rating;
}
public int getComment_id() {
return comment_id;
}
public void setComment_id(int comment_id) {
this.comment_id = comment_id;
}
public String getMovie_name() {
return movie_name;
}
public void setMovie_name(String movie_name) {
this.movie_name = movie_name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getComment_create_time() {
return comment_create_time;
}
public void setComment_create_time(String comment_create_time) {
this.comment_create_time = comment_create_time;
}
public float getRating() {
return rating;
}
public void setRating(float rating) {
this.rating = rating;
}
}
- comments_list_item.xml recyclerView所对应的item布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/img_avatar" />
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="乖,摸摸头"
android:textColor="#333333"
android:textStyle="bold" />
</androidx.appcompat.widget.LinearLayoutCompat>
<RatingBar
android:id="@+id/rb_star"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:isIndicator="true"
android:numStars="5"
android:stepSize="0.5" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:lineSpacingExtra="6dp"
android:text="三星半 隔这么久人能凑这么齐挺不容易的"
android:textColor="#494949"
android:textSize="12sp" />
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="2024-07-16 12:44:12"
android:textColor="#aaaaaa"
android:textSize="12sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#f5f5f5" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>