old王讲课的第七天

本文介绍了如何在应用中使用Room库来管理和优化本地结构化数据,包括其编译时验证和简化数据库操作的功能。同时,详细展示了如何集成评论API和适配器,实现实时评论显示和发送功能。
摘要由CSDN通过智能技术生成

处理大量结构化数据的应用可极大地受益于在本地保留这些数据。最常见的使用场景是缓存相关的数据,这样一来,当设备无法访问网络时,用户仍然可以在离线状态下浏览该内容。

Room 持久性库在 SQLite 上提供了一个抽象层,以便在充分利用 SQLite 的强大功能的同时,能够流畅地访问数据库。具体来说,Room 具有以下优势:

  • 提供针对 SQL 查询的编译时验证。
  • 提供方便注解,可最大限度减少重复和容易出错的样板代码。
  • 简化了数据库迁移路径。

出于这些方面的考虑,我们强烈建议您使用 Room,而不是直接使用 SQLite API

@Entity(tableName = "Videoss")
public class Video {
    @PrimaryKey
    private long id;
    @ColumnInfo
    private String videomainimg;
    @ColumnInfo
    private String videopath;
    @ColumnInfo
    private String caption;

    public Video(long id, String videomainimg, String videopath, String caption) {
        this.id = id;
        this.videomainimg = videomainimg;
        this.videopath = videopath;
        this.caption = caption;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getVideomainimg() {
        return videomainimg;
    }

    public void setVideomainimg(String videomainimg) {
        this.videomainimg = videomainimg;
    }

    public String getVideopath() {
        return videopath;
    }

    public void setVideopath(String videopath) {
        this.videopath = videopath;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    @Override
    public String toString() {
        return "Video{" +
                "id=" + id +
                ", videomainimg='" + videomainimg + '\'' +
                ", videopath='" + videopath + '\'' +
                ", caption='" + caption + '\'' +
                '}';
    }
}

数据库类为应用提供与该数据库关联的 DAO 的实例。反过来,应用可以使用 DAO 从数据库中检索数据,作为关联的数据实体对象的实例。此外,应用还可以使用定义的数据实体更新相应表中的行,或者创建新行供插入。图 1 说明了 Room 的不同组件之间的关系。

今天的重点是评论:

Api里的是基础:

@GET("/comment/getCommentByVideoId")
Observable<CommentEntity> getCommentData(@Query("videoId") int videoId);

@POST("/comment/comment")
Observable<CommentEntity> postSendComment(@Body RequestBody body);

也需要实体类和适配器:

public class MyCommentAdapter extends BaseQuickAdapter<CommentEntity.DataBean, BaseViewHolder> {
    public MyCommentAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, CommentEntity.DataBean dataBean) {
        ImageView imageView=baseViewHolder.getView(R.id.iv_head_comment);
        GlideUtil.loadciecleImage(getContext(),dataBean.getIcon(),imageView);
        baseViewHolder.setText(R.id.tv_user_comment,dataBean.getUsername());
        baseViewHolder.setText(R.id.tv_message_comment,dataBean.getMsg());
    }
}

想要评论显示最重要的一步是:

mViewModel.commentLiveData.observe(this, new Observer<CommentEntity>() {
    @Override
    public void onChanged(CommentEntity commentEntity) {
        if (commentEntity.getCode()==200){
            List<CommentEntity.DataBean> dataBeans=commentEntity.getData();
            myCommentAdapter.getData().addAll(dataBeans);
            myCommentAdapter.notifyDataSetChanged();
            tv_commetcount.setText(myCommentAdapter.getData().size()+"条评论");

        }
    }
});
mViewModel.sendcommentLiveData.observe(this, new Observer<CommentEntity>() {
    @Override
    public void onChanged(CommentEntity commentEntity) {
        if (commentEntity.getCode()==200){
            Toast.makeText(getContext(), "发布评论成功", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(getContext(), "发布评论失败", Toast.LENGTH_SHORT).show();
        }
    }
});
  • 22
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值