I have an app that I want to do the following:
When the user clicks on a star imageView it updates the server (adds posts to my favoritePosts, and +1 to likesCounter), but it should also update the imageView (liked to unliked and vice versa), and the textView with the number of likes on the cardView I clicked.
Currently, the imageView changes, and it updates server correctly, but there are some things that do not work the way they should, such as:
Some titles disappear and reappear when I click the star imageView (to add posts to favorites), and the textView is almost never updated at all, and when it is, it sometimes decreases instead of increasing and vice versa. In my database it is updated correctly, like is added/removed correctly, likes counter is updated correctly, also when I exit and reenter the activity it is updated correctly.
I have tried this, this, this, this, this, etc, but none of these solutions worked for me.
UserPostsAdapter:
public class UserPostsAdapter extends RecyclerView.Adapter<UserPostsAdapter.PostsViewHolder> {
private List<UserPosts> postItems;
private Context context;
private SQLiteHandler db;
private static final String TAG = UserPosts.class.getSimpleName();
class PostsViewHolder extends RecyclerView.ViewHolder {
TextView userPostsTitle, userPostsDate, userPost, textViewStars, textViewComments;
ImageView imageViewDisplayComments, imageViewReply, imageViewCommentStar;
String pCcontactId, postId;
PostsViewHolder(View itemView) {
super(itemView);
userPostsTitle = (TextView) itemView.findViewById(R.id.userPostsTitle);
userPostsDate = (TextView) itemView.findViewById(R.id.userPostsDate);
userPost = (TextView) itemView.findViewById(R.id.userPost);
textViewStars = (TextView) itemView.findViewById(R.id.textViewStars);
textViewComments = (TextView) itemView.findViewById(R.id.textViewComments);
imageViewCommentStar = (ImageView) itemView.findViewById(R.id.imageViewCommentStar);
imageViewDisplayComments = (ImageView) itemView.findViewById(R.id.imageViewDisplayComments);
imageViewReply = (ImageView) itemView.findViewById(R.id.imageViewReply);
}
}
public UserPostsAdapter(List<UserPosts> postsList, Context context) {
this.postItems = postsList;
this.context = context;
}
@Override
public UserPostsAdapter.PostsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_user_post, parent, false);
return new UserPostsAdapter.PostsViewHolder(itemView);
}
@Override
public void onBindViewHolder(final UserPostsAdapter.PostsViewHolder holder, int position) {
final UserPosts post = postItems.get(position);
db = new SQLiteHandler(context.getApplicationContext());
HashMap<String, String> user = db.getUserDetails();
final String currentUserId = user.get("uid");
if (post.getTitle() != null && !post.getTitle().isEmpty()) {
holder.userPostsTitle.setText(post.getTitle());
} else {
holder.userPostsTitle.setVisibility(View.GONE);
}
holder.userPostsDate.setText(post.getCreatedDateTime());
holder.userPost.setText(post.getPostedText());
if (Integer.parseInt(post.getLikesNumber()) > 0) {
holder.textViewStars.setText(post.getLikesNumber());
}
holder.textViewComments.setText(post.getCommentsNumber());
holder.pCcontactId = post.getUserId();
final String postId = holder.postId = post.getId();
if (post.getFavouritePost()) {
holder.imageViewCommentStar.setImageResource(R.drawable.ic_icon_star_ppdcolor);
} else {
holder.imageViewCommentStar.setImageResource(R.drawable.ic_icon_star_outline_ppdcolor);
}
holder.imageViewCommentStar.setOnClickListener(new View.OnClickListener() {
boolean isPressed = post.getFavouritePost();
@Override
public void onClick(View v) {
if (isPressed) {
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<DefaultResponse> call = apiService.removePostFavourite(currentUserId, postId);
call.enqueue(new Callback<DefaultResponse>() {
@Override
public void onResponse(Call<DefaultResponse> call, retrofit2.Response<DefaultResponse> response) {
boolean success = response.body().isSuccess();
onFavouriteClicked(position);
}
@Override
public void onFailure(Call<DefaultResponse> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
} else {
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<DefaultResponse> call = apiService.addPostFavourite(currentUserId, postId);
call.enqueue(new Callback<DefaultResponse>() {
@Override
public void onResponse(Call<DefaultResponse> call, retrofit2.Response<DefaultResponse> response) {
boolean success = response.body().isSuccess();
onFavouriteClicked(position);
}
@Override
public void onFailure(Call<DefaultResponse> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
}
isPressed = !isPressed;
}
});
holder.imageViewDisplayComments.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
holder.imageViewReply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder addCommentBuilder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
View addCommentView = inflater.inflate(R.layout.popup_add_comment_reply, null);
final EditText addCommentReplyEditText = (EditText) addCommentView.findViewById(R.id.addCommentReplyEditText);
FloatingActionButton imageViewCommentReply = (FloatingActionButton) addCommentView.findViewById(R.id.imageViewCommentReply);
addCommentBuilder.setView(addCommentView);
final AlertDialog addCommentReply = addCommentBuilder.create();
addCommentReply.show();
imageViewCommentReply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!addCommentReplyEditText.getText().toString().isEmpty()) {
addCommentReply.dismiss();
} else {
Toast.makeText(context,
R.string.failure,
Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
private void onFavouriteClicked(int position) {
UserPosts post = postItems.get(position);
post.setFavouritePost(!post.getFavouritePost());
postItems.set(position, post);
notifyItemChanged(position);
}
@Override
public int getItemCount() {
return postItems.size();
}
}
Cardview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/userPostsCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/userPostsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:textStyle="bold"
tools:ignore="RtlHardcoded,SpUsage" />
<TextView
android:id="@+id/userPostsDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/userPostsTitle"
android:layout_marginTop="1dip"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:textColor="@color/aluminum"
tools:ignore="RtlHardcoded,SpUsage" />
<TextView
android:id="@+id/userPost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/userPostsDate"
android:padding="5dp"/>
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/userPost"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#B6B6B6" />
<LinearLayout
android:id="@+id/idLLUserPostsFooter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/divider"
android:orientation="horizontal"
android:weightSum="9">
<ImageView
android:id="@+id/imageViewCommentStar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="@string/star"
android:padding="5dp"/>
<TextView
android:id="@+id/textViewStars"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:padding="5dp" />
<ImageView
android:id="@+id/imageViewDisplayComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="@string/comments"
android:padding="5dp"
card_view:srcCompat="@drawable/comment_multiple_outline_ppdcolor" />
<TextView
android:id="@+id/textViewComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:padding="5dp" />
<ImageView
android:id="@+id/imageViewReply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="@string/reply"
android:padding="5dp"
card_view:srcCompat="@drawable/reply_ppdcolor" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
<TextView
android:id="@+id/userAppId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/number_zero"
android:visibility="gone" />
<TextView
android:id="@+id/userContactId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/number_zero"
android:visibility="gone" />
</LinearLayout>