0

Activity A has a recycler view, which has each row as a post, now each post has a comment count textview which launches a comment activity where user can see comments and also add comments.

Now question is, after adding a comment how do I update the comment count in that particular row of recycler view in Activity A when comment Activity finishes or pressing back button to get back on posts page. But I don't want to reload the recycler view again as it will distract the user from the current location.

Please guide me in the right direction. Below is my Recyclerview adapter code snippet.

public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        int viewType = getItemViewType(position);
        final Posts posts = postsList.get(position);
        final viewHolderPost viewHolderPost = (viewHolderPost) viewHolder;

        viewHolderPost.commentcount_wrap.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(v.getContext(), CommentViewerActivity.class);
                        intent.putExtra("post_id", posts.getPost_id());
                        v.getContext().startActivity(intent);
                    }
                });
}
Chayan C
  • 123
  • 2
  • 10

3 Answers3

0

You can use startActivityForResult instead of startActivity

Ref: https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int,%20android.os.Bundle)

So when the CommentActivity is finish, the previous activity will receive a callback that is the result of CommentActivity, you will handle that result and update your recyclerview's adapter.

Hope that help. :)

Brian H.
  • 1,603
  • 11
  • 16
0

You could notify the Adapter that there is a change at the particular position. check: adapter.notifyItemChanged(1)

-edit-

On your Activity A add this.

override fun onResume() {
    super.onResume()
    if(lastPosition != -1)
        recycler_test.adapter?.notifyItemChanged(lastPosition)
}

And your adapter should provide a callback like this.

recycler_test.adapter = CustomAdapter(Controller.shared.myList){ positionClicked ->
        //This block of code will be called every time a user clicked a comment.

        lastPosition = positionClicked

        val intent = Intent(applicationContext, ActivityB::class.java)
        intent.putExtra("position", positionClicked)
        startActivity(intent)
    }
K.Kotsi
  • 76
  • 3
  • From where should I call it? Activity A or Comment Activity? Can you please post a code snippet? – Chayan C Jun 16 '20 at 08:33
  • I added some code. Also, instead of onResume you could onActivityResult as the guys have already told you. – K.Kotsi Jun 17 '20 at 12:22
0

Okay here is what I would do:

  1. Create common object (preferably Repository), where I would store list of Posts with Comments in memory.
  2. When clicking on Post, I would call startActivityForResult method from PostActivity to open CommentActivity
  3. After adding/deleting/editing the comment in CommentActivity I would update the list in Repository and finish activity with proper result
  4. In PostActivity I would override onActivityResult method to check if the CommentActivity finished with result that will tell me to refresh list in RecyclerView
  5. If so, I would get the updated list from repository in PostActivity and update it in Adapter
Mariusz Brona
  • 1,549
  • 10
  • 12
  • Thanks a lot for the step by step guide. I am currently this process as @Yasu also guided the same thing. – Chayan C Jun 16 '20 at 13:57
  • I have tried this using ((Activity) context).startActivityForResult(intent,301); But the problem is I am getting Null as return data in Origin Activity at onActivityResult(). Can you please tell me why is this happening? – Chayan C Jun 18 '20 at 06:50
  • Hey, check this answer, it might help you: https://stackoverflow.com/a/14785924/2448589 – Mariusz Brona Jun 18 '20 at 07:05
  • Hi Mariusz, I have done exactly the same thing except for one difference. I am calling the second activity from RecyclerView.Adapter using ((Activity) context).startActivityForResult(intent,301); – Chayan C Jun 18 '20 at 07:11
  • Here is the pastebin code snippet https://pastebin.com/arZDYgxH – Chayan C Jun 18 '20 at 07:15
  • Okay, when you are putting the post_position and comment_count are they Int or String? Because you are trying to read a String in onActivityResult – Mariusz Brona Jun 18 '20 at 07:18
  • I have just used intent.putExtra("comment_count",result_comment_count); I did not specify anything – Chayan C Jun 18 '20 at 07:20
  • But both variables are defined as integer – Chayan C Jun 18 '20 at 07:22
  • Okay, but putExtra is an overloaded method, which means that there are many putExtra methods, which are accepting different parameters. So you need to make sure when you read the data in onActivityResult, you are trying to obtain the same type you put in putExtra – Mariusz Brona Jun 18 '20 at 07:23
  • so you need to call `int commentCount = data.getIntExtra("comment_count", -1);`, where -1 means that the commentCount is not passed/not passed properly from previous activity – Mariusz Brona Jun 18 '20 at 07:24
  • OMG!!! Thank you Mariusz it worked and I got the result. – Chayan C Jun 18 '20 at 07:36
  • Great! :) If everything's fine please accept the answer ;) – Mariusz Brona Jun 18 '20 at 07:39