Recyclerview Retrofit 2

编程入门 行业动态 更新时间:2024-10-26 14:38:02
Recyclerview Retrofit 2 - 如何在插入数据库后显示新项目(Recyclerview Retrofit 2 - How to display a new item after inserting to database)

我正在使用RecyclerView来显示评论列表,并在底部(RecyclerView之外)我有一个评论框来向列表中插入新评论。 我可以将数据插入数据库,但我无法知道如何通知适配器添加了新评论并将其显示在列表中。

我所要做的就是在列表顶部插入数据库后显示新评论。

适配器

public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.CommentsHolder>{ private List<PostCommentsData> dataList; private Context context; public CommentsAdapter(Context applicationContext, List<PostCommentsData> commentsArrayList){ this.dataList = commentsArrayList; this.context = applicationContext; } @Override public CommentsHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_post_comment, parent, false); return new CommentsHolder(view); } @Override public void onBindViewHolder(CommentsHolder holder, int position) { Picasso.with(context).load(dataList.get(position).getComment_profile_image()).into(holder.ivProfileImage); holder.tvUserName.setText(dataList.get(position).getComment_username()); holder.tvCommentDate.setText(dataList.get(position).getComment_datetime()); holder.tvCommentBody.setText(dataList.get(position).getComment_body_text()); } @Override public int getItemCount() { return dataList == null ? 0 : dataList.size(); } public static class CommentsHolder extends RecyclerView.ViewHolder{ ImageView ivProfileImage; TextView tvUserName; TextView tvCommentDate; TextView tvCommentBody; public CommentsHolder(View itemView, final OnItemClickListener listener) { super(itemView); ivProfileImage = itemView.findViewById(R.id.comment_profile_image); tvUserName = itemView.findViewById(R.id.comment_username); tvCommentDate = itemView.findViewById(R.id.comment_date); tvCommentBody = itemView.findViewById(R.id.comment_text); } } }

活动

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_post_comments); userSessionManager = new UserSessionManager(getApplicationContext()); HashMap<String, String> user = userSessionManager.getUserDetails(); userid = user.get(UserSessionManager.KEY_ID); Intent intent = getIntent(); mPostID = intent.getStringExtra(EXTRA_POST_ID); newCommentBox = findViewById(R.id.CommentBox); newCommentButton = findViewById(R.id.CommentSendButton); initCommentsView(); submitNewComment(); swipeContainer = findViewById(R.id.CommentsSwipeContainer); swipeContainer.setColorSchemeResources(android.R.color.holo_orange_dark); swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { loadComments(); } }); } private void initCommentsView(){ rvPostComments = findViewById(R.id.post_comments_recyclerview); rvPostComments.setLayoutManager(new LinearLayoutManager(this)); postCommentsAdapter = new CommentsAdapter(this, null); rvPostComments.setAdapter(postCommentsAdapter); rvPostComments.smoothScrollToPosition(0); loadComments(); } //This loads the comments list private void loadComments(){ String post_id = mPostID; postCommentsService = new PostCommentsService(this); postCommentsService.doGetPostComments(post_id, new Callback<PostCommentsList>() { @Override public void onResponse(Call<PostCommentsList> call, Response<PostCommentsList> response) { if(response.isSuccessful()){ commentsListData = response.body().getPost_comments_list(); postCommentsAdapter = new CommentsAdapter(getApplicationContext(), commentsListData); rvPostComments.setAdapter(postCommentsAdapter); swipeContainer.setRefreshing(false); }else{ //do error stuff } } @Override public void onFailure(Call call, Throwable t) { //do failure stuff } }); } //This is the submit function for new comments. public void submitNewComment(){ newCommentButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String newText = newCommentBox.getText().toString(); String postID = mPostID; String userID = userid; postCommentsService = new PostCommentsService(PostCommentsActivity.this); postCommentsService.doAddNewComment(postID, userID, newText, new Callback() { @Override public void onResponse(Call call, Response response) { PostCommentsData p = (PostCommentsData) response.body(); String msg = p.getMessage(); if(response.isSuccessful()){ if(!p.isError()){ //The message has been successfully added to the database. //Now tell the adapter that there is a new comment and add it/show it on the list. //How can this get done? }else{ //do error stuff } } } @Override public void onFailure(Call call, Throwable t) { //do failure stuff } }); } }); }

在获得服务器的响应后,如何使用新评论更新列表(将其添加到顶部)?

提前致谢,

I'm working with RecyclerViewto display a list of comments and at the bottom (outside of the RecyclerView) I have a comment box to insert new comments to the list. I am able to insert the data to the database but I am clueless as to how to notify the adapter that a new comment has been added and to display it on the list.

All I'm trying to do is to display the new comment at the top of the list after it has been inserted to the database.

Adapter

public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.CommentsHolder>{ private List<PostCommentsData> dataList; private Context context; public CommentsAdapter(Context applicationContext, List<PostCommentsData> commentsArrayList){ this.dataList = commentsArrayList; this.context = applicationContext; } @Override public CommentsHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_post_comment, parent, false); return new CommentsHolder(view); } @Override public void onBindViewHolder(CommentsHolder holder, int position) { Picasso.with(context).load(dataList.get(position).getComment_profile_image()).into(holder.ivProfileImage); holder.tvUserName.setText(dataList.get(position).getComment_username()); holder.tvCommentDate.setText(dataList.get(position).getComment_datetime()); holder.tvCommentBody.setText(dataList.get(position).getComment_body_text()); } @Override public int getItemCount() { return dataList == null ? 0 : dataList.size(); } public static class CommentsHolder extends RecyclerView.ViewHolder{ ImageView ivProfileImage; TextView tvUserName; TextView tvCommentDate; TextView tvCommentBody; public CommentsHolder(View itemView, final OnItemClickListener listener) { super(itemView); ivProfileImage = itemView.findViewById(R.id.comment_profile_image); tvUserName = itemView.findViewById(R.id.comment_username); tvCommentDate = itemView.findViewById(R.id.comment_date); tvCommentBody = itemView.findViewById(R.id.comment_text); } } }

Activity

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_post_comments); userSessionManager = new UserSessionManager(getApplicationContext()); HashMap<String, String> user = userSessionManager.getUserDetails(); userid = user.get(UserSessionManager.KEY_ID); Intent intent = getIntent(); mPostID = intent.getStringExtra(EXTRA_POST_ID); newCommentBox = findViewById(R.id.CommentBox); newCommentButton = findViewById(R.id.CommentSendButton); initCommentsView(); submitNewComment(); swipeContainer = findViewById(R.id.CommentsSwipeContainer); swipeContainer.setColorSchemeResources(android.R.color.holo_orange_dark); swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { loadComments(); } }); } private void initCommentsView(){ rvPostComments = findViewById(R.id.post_comments_recyclerview); rvPostComments.setLayoutManager(new LinearLayoutManager(this)); postCommentsAdapter = new CommentsAdapter(this, null); rvPostComments.setAdapter(postCommentsAdapter); rvPostComments.smoothScrollToPosition(0); loadComments(); } //This loads the comments list private void loadComments(){ String post_id = mPostID; postCommentsService = new PostCommentsService(this); postCommentsService.doGetPostComments(post_id, new Callback<PostCommentsList>() { @Override public void onResponse(Call<PostCommentsList> call, Response<PostCommentsList> response) { if(response.isSuccessful()){ commentsListData = response.body().getPost_comments_list(); postCommentsAdapter = new CommentsAdapter(getApplicationContext(), commentsListData); rvPostComments.setAdapter(postCommentsAdapter); swipeContainer.setRefreshing(false); }else{ //do error stuff } } @Override public void onFailure(Call call, Throwable t) { //do failure stuff } }); } //This is the submit function for new comments. public void submitNewComment(){ newCommentButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String newText = newCommentBox.getText().toString(); String postID = mPostID; String userID = userid; postCommentsService = new PostCommentsService(PostCommentsActivity.this); postCommentsService.doAddNewComment(postID, userID, newText, new Callback() { @Override public void onResponse(Call call, Response response) { PostCommentsData p = (PostCommentsData) response.body(); String msg = p.getMessage(); if(response.isSuccessful()){ if(!p.isError()){ //The message has been successfully added to the database. //Now tell the adapter that there is a new comment and add it/show it on the list. //How can this get done? }else{ //do error stuff } } } @Override public void onFailure(Call call, Throwable t) { //do failure stuff } }); } }); }

How can I update the list with the new comment (add it to the top) after I get the response from the server?

Thanks in advance,

最满意答案

在适配器中添加一个函数

public void addNewComment(PostCommentsData postCommentsData){ this.dataList.add(0,postCommentsData); notifyItemInserted(0); }

那么当您在数据库中添加注释时,请在适配器对象上调用此函数。 <adapter>.addNewComment(comment)

PostCommentsData p = (PostCommentsData) response.body(); String msg = p.getMessage(); if (response.isSuccessful()) { if (!p.isError()) { postCommentsAdapter.addNewComment(p); } else { //do error stuff } }

Add a function in your adapter

public void addNewComment(PostCommentsData postCommentsData){ this.dataList.add(0,postCommentsData); notifyItemInserted(0); }

then when you add a comment in the database, call this function on the adapter object. <adapter>.addNewComment(comment)

PostCommentsData p = (PostCommentsData) response.body(); String msg = p.getMessage(); if (response.isSuccessful()) { if (!p.isError()) { postCommentsAdapter.addNewComment(p); } else { //do error stuff } }

更多推荐

本文发布于:2023-08-04 11:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Recyclerview   Retrofit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!