PCcloob.Ir
PCcloob.Ir

Reputation: 97

update one item in recyclerview adapter

Here is my ReplyAdapter.java :

public class ReplyAdapter extends RecyclerView.Adapter<ReplyAdapter.MyViewHolder> {
private Context context;
private List<Reply> replies;
private ProgressDialog pDialog;


public ReplyAdapter(Context context, List<Reply> replies) {
    this.context = context;
    this.replies = replies;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.replies_list, parent, false);
    return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, @SuppressLint("RecyclerView") final int position) {

    holder.reply_id.setText(String.valueOf(replies.get(position).getId()));
    holder.replyuserid.setText(String.valueOf(replies.get(position).getUserid()));
    holder.alike.setText(replies.get(position).getAlike());
    holder.adislike.setText(replies.get(position).getAdislike());
    holder.btn_like_reply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pDialog = new ProgressDialog(context, R.style.CustomProgress);
            pDialog.setCancelable(false);
            pDialog.show();
            int postid = replies.get(position).getId();
            int vote = 1;
            SendVoteRequest(postid, vote);

        }
    });


private void SendVoteRequest(int postid, final int vote) {
    PrefConfig prefConfig = new PrefConfig(context);
    int userid = Integer.parseInt(prefConfig.Readuserid());
    String token = prefConfig.Readtoken();
    APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
    Call<Vote> call = apiInterface.SendNewVote(userid, postid, token, vote);
    call.enqueue(new Callback<Vote>() {
        @Override
        public void onResponse(Call<Vote> call, Response<Vote> response) {
            hidePDialog();
            if (response.isSuccessful()) {
                Vote vote = response.body();
                Toast.makeText(context, vote.getMessage(), Toast.LENGTH_LONG).show();
                String flag = vote.getFlag();
                if (flag.equals("1")) {
                //    Intent intent = ((Activity) context).getIntent();
                  //  context.startActivity(intent);
                 //   ((Activity) context).finish();

                    //Here alike value must be updated.
                }

            } 

When user click on like button , "SendVoteRequest" call and send to server. if response is Successful return flag=1; With this code page refreshed and values updated :

Intent intent = ((Activity) context).getIntent();
context.startActivity(intent);
((Activity) context).finish();

But i dont want page refreshed.I reload "holder.alike" value.

notifyItemChanged not worked.

 if (flag.equals("1")) {
  ReplyAdapter.this.notifyItemChanged(1);
  }

Upvotes: 0

Views: 62

Answers (2)

Wahdat Jan
Wahdat Jan

Reputation: 4156

You need to notify your adapter that item at some position changes. please have a look Need an example about RecyclerView.Adapter.notifyItemChanged(int position, Object payload)

Upvotes: 0

Cuong Nguyen
Cuong Nguyen

Reputation: 1018

You can use two ways:

1 - Use adapter.notifyItemChanged(updateIndex)

2 - Use DiffUtil link

I recommend you research DiffUtil

Upvotes: 3

Related Questions