首页 > 其他分享 >基于Android平台开发,在线电影购票系统(九)用户评论列表实现

基于Android平台开发,在线电影购票系统(九)用户评论列表实现

时间:2024-07-20 23:27:43浏览次数:11  
标签:comment String 购票 id 在线电影 time Android public name

1. 涉及到的技术点

  1. 使用SQLite数据库实现用户评论数据保存
  2. 使用RecyclerView+adapter实现用户评论列表

2. 具体代码实现过程

  1. 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);
        }
    }
}
  1. 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;
    }

}
  1. 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;
    }
}
  1. 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>

3. 最终效果图

在这里插入图片描述

标签:comment,String,购票,id,在线电影,time,Android,public,name
From: https://blog.csdn.net/jky_yihuangxing/article/details/140579478

相关文章

  • Android C++系列:Linux文件系统(二)
    1.VFS虚拟文件系统Linux支持各种各样的文件系统格式,如ext2、ext3、reiserfs、FAT、NTFS、iso9660等等,不同的磁盘分区、光盘或其它存储设备都有不同的文件系统格式,然而这些文件系统都可以mount到某个目录下,使我们看到一个统一的目录树,各种文件系统上的目录和文件我们用l......
  • Android C++系列:函数返回值注意事项
    1.背景函数返回值就是使用return语句终止正在执行的函数,看是很简单的问题有什么说的呢?因为越是简单的问题里面越是有一些不易发现的坑。比如在循环中使用return语句:boolfindChar(conststring&str,constcharc){autosize=str.size();for(decltype(size......
  • Android开发 - xmlns命名空间中tools详解
    xmlns:tools是什么命名空间tools可以告诉AndroidStudio,哪些属性在运行的时候是被忽略的,只在设计布局的时候有效。比如我们要让android:text属性只在布局预览中有效。tools可以覆盖android的所有标准属性,将android:换成tools:即可。同时在运行的时候就连tools:本身都是被忽略......
  • Android开发 - 布局文件之 include 使用
    简介include是在一个布局中,导入另一个布局文件。优势是:相同的页面只需写一次,提高了共通布局的复用性。下面我们以标题栏为例,详细说明它的使用步骤使用步骤第一步:通用布局-创建title_bar.xml//title_bar<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:an......
  • Android开发 - Context解析
    Context是什么Context的中文翻译为:语境;上下文;背景;环境,在开发中我们经常说称之为“上下文”,那么这个“上下文”到底是指什么意思呢?在语文中,我们可以理解为语境,在程序中,我们可以理解为当前对象在程序中所处的一个环境,一个与系统交互的过程。比如微信聊天,此时的“环境”是指......
  • android audio不同音频流,(三)各音频流默认音量加载过程
    各音频流默认值,定义文件路径:frameworks/base/media/java/android/media/AudioSystem.java默认音量定义数组: /**@hide*/ publicstaticint[]DEFAULT_STREAM_VOLUME=newint[]{     4, //STREAM_VOICE_CALL     7, //STREAM_SYSTEM ......
  • 简化Android数据管理:深入探索SQLite数据库
    SQLite数据库在Android中的使用SQLite是一种精巧的、轻量级的、无服务器的、零配置的、事务性SQL数据库引擎。相较于其他数据库系统,SQLite更适用于需要轻量级解决方案的移动应用场景。本文将详细介绍SQLite数据库在Android中的使用,包括数据库的创建、表的建立、数据的增删......
  • Android开发 - inflate方法与创建视图解析
    简介在Android开发过程中,很多地方都不可避免的使用到inflate方法,如在给Fragment进行CreateView(创建视图)时,我们通常是inflater.inflate(R.layout.xxx,container,false)来调用inflate方法的,不难发现,inflate方法的作用是将一个xml布局文件变成一个view对象。注意事项......
  • Android笔试面试题AI答之Activity(2)
    答案仅供参考,大部分为文心一言AI作答目录1.请介绍一下Activity生命周期?1.完全生命周期2.可见生命周期3.前台生命周期4.配置更改5.特殊场景2.请介绍一下横竖屏切换时Activity的生命周期变化?1.默认行为(未设置`android:configChanges`)2.设置`android:configChang......
  • Android 14 适配之 - 隐式/显示 Intent 和 广播适配
    隐式Intent对隐式Intent限制:对Android14(API级别34)或更高版本为目标平台的应用,Android会限制应用向内部应用组件发送隐式intent:1.即隐式intent只能发送给导出的组件。在应用必须使用显式intent来发送组件,且被发送的组件是未被导出的属性配置。2.如果被发出的......