I have a recyclerview that populates data from a API. Each row has a like button that the user can press and change the color of the icon.
When the user press the button, it works fine, but when he scroll the page up and then come back to the liked row, the like is gone and looks like it never been pressed before.
How do I keep this "liked" state?
public class SocialAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
...
public SocialAdapter(Context context, JSONArray pages){
mContext = context;
mPages = pages;
...
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.social_row, parent, false);
MyHolder holder=new MyHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
MyHolder myHolder= (MyHolder) holder;
try {
JSONObject mpackid = mPages.getJSONObject(position);
//myHolder.button_like.setText("Eu quero!");
myHolder.button_like.setEnabled(true);
myHolder.button_like.setBackground(mContext.getResources().getDrawable(R.drawable.empty_star));
myHolder.number_likes.setText(mpackid.optJSONObject("info").optString("likes")+" foo");
myHolder.number_likes.setTextColor(Color.parseColor("#525252"));
myHolder.user_name.setText(mpackid.optString("user_full_name"));
myHolder.user_text.setText(mpackid.optJSONObject("info").optString("text"));
myHolder.feed_id.setText(mpackid.optJSONObject("info").optString("feed_suggestion_id"));
myHolder.number_likes.setText(mpackid.optJSONObject("info").optString("likes")+" foo");
myHolder.like_int.setText(mpackid.optJSONObject("info").optString("likes"));
myHolder.tags.setText("#"+mpackid.optJSONObject("info").optJSONArray("tags").toString().replace("[", "").replace("]", "").replace("\"",""));
myHolder.date.setText(mpackid.optJSONObject("info").optString("elapsed_time"));
String likes = mpackid.optJSONObject("info").optJSONArray("liked_by").toString().replace("[", "").replace("]", "").replace("\"","");
if(likes.contains(mUserId)){
myHolder.button_like.setEnabled(false);
myHolder.button_like.setBackground(mContext.getResources().getDrawable(R.drawable.filled_star));
myHolder.number_likes.setText("foo "+mpackid.optJSONObject("info").optString("likes")+" foo");
myHolder.number_likes.setTextColor(Color.parseColor("#525252"));
}
Glide.with(mContext).load(mpackid.optString("user_avatar_url"))
.asBitmap()
.thumbnail(Glide.with(mContext).load(R.drawable.ajax_loader).asBitmap())
.fitCenter()
.into(myHolder.user_image);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public long getItemId(int position) {
return 0;// not using this
}
@Override
public int getItemCount() {
return mPages.length();
}
private static class ViewHolder{
ImageView icon;
TextView itemName;
TextView url;
}
class MyHolder extends RecyclerView.ViewHolder{
CircleImageView user_image;
TextView user_name;
TextView user_text;
TextView feed_id;
TextView number_likes;
TextView like_int;
TextView date;
TextView tags;
ImageButton button_like;
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
user_image = (CircleImageView) itemView.findViewById(R.id.user_image);
user_name = (TextView) itemView.findViewById(R.id.user_name);
user_text = (TextView) itemView.findViewById(R.id.headline);
number_likes = (TextView) itemView.findViewById(R.id.number_likes);
feed_id = (TextView) itemView.findViewById(R.id._id);
like_int = (TextView) itemView.findViewById(R.id.likes_nb);
button_like = (ImageButton) itemView.findViewById(R.id.button5);
date = (TextView) itemView.findViewById(R.id.date);
tags = (TextView) itemView.findViewById(R.id.feed_tag);
}
}
public JSONObject getItem(int position) {
try {
return mPages.getJSONObject(position);
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
public void addMoreData(JSONArray loadMoreArray) throws JSONException {
//Seta data pra LoadArray
mPages.length();
this.mPages = mPages.put(loadMoreArray.getJSONObject(0));
notifyItemRangeInserted(mPages.length(), mPages.length()+1);
SmoothRecyclerView recyclerView = (SmoothRecyclerView) ((Activity) this.mContext).findViewById(R.id.chat_social);
recyclerView.scrollToPosition(mPages.length()-1);
}