I tried to implement you code with some modifications to get rid of errors.
It’s working good. But there’s another problem I can’t figure out what but I think it’s in the line:
for (Feeds feed: response.getCurrentPage()) {
List<BackendlessUser> likedUsers = feeds.getUsersThatLike();
As you can see instead of using feed I’m using “feeds” to get the exact position of an item because if I use “feed” it changes the background of all my button in all the views:
List<BackendlessUser> likedUsers = feed.getUsersThatLike();
Posting my Adapter class and my Feeds class. Please help me.
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.FeedsHolder> {
private List<Feeds> list;
private Context context;
private static String TAG = "MainActivity";
public FeedAdapter(Context context, List<Feeds> list) {
this.context = context;
this.list = list;
}
@Override
public FeedsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_item, parent, false);
return new FeedsHolder(view);
}
@Override
public void onBindViewHolder(final FeedsHolder holder, int position) {
//Starting Feeds
final Feeds feeds = list.get(position);
//Name
holder.name.setText(feeds.getOwner());
//Profile picture
holder.profilePictureURL = feeds.getProfilePictureURL();
//Image
holder.imageURL = feeds.getImageUrl();
//Getting date
Date myD = feeds.getCreated();
long ddate = myD.getTime();
String myDate = String.valueOf(DateUtils.getRelativeTimeSpanString(ddate, System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS));
holder.timeAgo.setText("• " + myDate);
//Get total likes
final int i = feeds.getLikes();
//Query
QueryOptions options = new QueryOptions();
options.setRelated( Arrays.asList( "usersThatLike" ) );
BackendlessDataQuery query = new BackendlessDataQuery();
query.setQueryOptions( options );
// getting all saved feeds with related users
Backendless.Data.of(Feeds.class).find(query, new AsyncCallback<BackendlessCollection<Feeds>>() {
@Override
public void handleResponse(BackendlessCollection<Feeds> response) {
String userId = Backendless.UserService.CurrentUser().getObjectId();
for (Feeds feed: response.getCurrentPage()) {
List<BackendlessUser> likedUsers = feeds.getUsersThatLike();
for (BackendlessUser user : likedUsers)
if (user.getObjectId().equals(userId)) {
Log.d(TAG, "No -------------------------------------");
holder.like.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_star_rate_on));
holder.like.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "You already like this item", Toast.LENGTH_SHORT).show();
}
});
} else {
holder.like.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_star_rate_off));
holder.like.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "It should work +++++++++++++++++++++++++++++++");
// getting current user
Toast.makeText(context, "Arrived", Toast.LENGTH_SHORT).show();
BackendlessUser currentUser = Backendless.UserService.CurrentUser();
// adding current user as one who "liked" feed, you should implement "adding" by yourself
List<BackendlessUser> list = new ArrayList<>();
list.add(currentUser);
feeds.setUsersThatLike(list);
holder.like.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_star_rate_on));
smallBang.setmListener(new SmallBangListener() {
@Override
public void onAnimationStart() {
holder.like.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_star_rate_on));
}
@Override
public void onAnimationEnd() {
holder.like.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_star_rate_on));
}
});
feeds.setLikes(i + 1);
Backendless.Data.of(Feeds.class).save(feeds, new AsyncCallback<Feeds>() {
@Override
public void handleResponse(Feeds feeds) {
int likes = feeds.getLikes();
if (likes == 1) {
holder.likes.setText(i + 1 + " like");
} else {
holder.likes.setText(i + 1 + " likes");
}
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
}
});
}
});
}
}
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
}
});
holder.status.setText(feeds.getStatus());
String thisString = "no";
String myImageString = "no";
Picasso.with(context).load(holder.profilePictureURL).placeholder(R.drawable.placeholder).into(holder.profilePicture);
String image = feeds.getIsImageUrlEmpty();
if (!image.equals(myImageString)) {
holder.image.setVisibility(View.GONE);
holder.tagStatusBottom.setVisibility(View.VISIBLE);
holder.tagImageBottom.setVisibility(View.GONE);
} else {
holder.image.setVisibility(View.VISIBLE);
holder.tagStatusBottom.setVisibility(View.GONE);
holder.tagImageBottom.setVisibility(View.VISIBLE);
Picasso.with(context).load(holder.imageURL).placeholder(R.drawable.placeholder).into(holder.image);
}
String myString = feeds.getIsTagEmpty();
if (myString.equals(thisString)){
holder.tagImageBottom.setVisibility(View.VISIBLE);
if (!image.equals(myImageString)) {
holder.image.setVisibility(View.GONE);
holder.tagStatusBottom.setVisibility(View.VISIBLE);
holder.tagImageBottom.setVisibility(View.GONE);
} else {
holder.image.setVisibility(View.VISIBLE);
holder.tagStatusBottom.setVisibility(View.GONE);
holder.tagImageBottom.setVisibility(View.VISIBLE);
Picasso.with(context).load(holder.imageURL).placeholder(R.drawable.placeholder).into(holder.image);
}
} else {
holder.tagImageBottom.setVisibility(View.GONE);
holder.tagStatusBottom.setVisibility(View.GONE);
}
String str = feeds.getTag();
ArrayList<int[]> hashtagSpans1 = getSpans(str, '#');
SpannableString commentsContent1 =
new SpannableString(str);
setSpanComment(commentsContent1, hashtagSpans1) ;
holder.tagImageBottom.setText(commentsContent1);
holder.tagStatusBottom.setText(commentsContent1);
holder.tagImageBottom.setMovementMethod(LinkMovementMethod.getInstance());
int likes = feeds.getLikes();
if (likes == 1) {
holder.likes.setText(i +" like");
} else {
holder.likes.setText(i +" likes");
}
}
public ArrayList<int[]> getSpans(String body, char prefix) {
ArrayList<int[]> spans = new ArrayList<int[]>();
Pattern pattern = Pattern.compile(prefix + "\\w+");
Matcher matcher = pattern.matcher(body);
// Check all occurrences
while (matcher.find()) {
int[] currentSpan = new int[2];
currentSpan[0] = matcher.start();
currentSpan[1] = matcher.end();
spans.add(currentSpan);
}
return spans;
}
private void setSpanComment(SpannableString commentsContent, ArrayList<int[]> hashtagSpans) {
for(int i = 0; i < hashtagSpans.size(); i++) {
int[] span = hashtagSpans.get(i);
int hashTagStart = span[0];
int hashTagEnd = span[1];
commentsContent.setSpan(new Hashtag(context),
hashTagStart,
hashTagEnd, 0);
}
}
@Override
public int getItemCount() {
return list.size();
}
public class FeedsHolder extends RecyclerView.ViewHolder {
//Setup Views
CardView card;
TextView name;
TextView tagImageBottom;
TextView tagStatusBottom;
TextView timeAgo;
TextView likes;
TextView comments;
ImageView profilePicture;
ImageView image;
TextView status;
String profilePictureURL;
String imageURL;
Button like;
RelativeLayout likeLayout;
public FeedsHolder(View itemView) {
super(itemView);
//Assign views by ID
card = (CardView) itemView.findViewById(R.id.cv);
name = (TextView) itemView.findViewById(R.id.person_name);
tagImageBottom = (TextView) itemView.findViewById(R.id.tagImageBottom);
tagStatusBottom = (TextView) itemView.findViewById(R.id.tagStatusBottom);
status = (TextView) itemView.findViewById(R.id.status);
timeAgo = (TextView) itemView.findViewById(R.id.timeStamp);
profilePicture = (ImageView) itemView.findViewById(R.id.profilePicture);
image = (ImageView) itemView.findViewById(R.id.picture);
likes = (TextView) itemView.findViewById(R.id.likesCounter);
comments = (TextView) itemView.findViewById(R.id.commentsCounter);
like = (Button) itemView.findViewById(R.id.like);
likeLayout = (RelativeLayout) itemView.findViewById(R.id.likeButton);
}
}
}
public class Feeds
{
private String owner;
private String tag;
private String profilePictureURL;
private String imageURL;
private Date created;
private Date updated;
private String status;
private int likes;
private String isTagEmpty;
private String isImageUrlEmpty;
private List<BackendlessUser> usersThatLike;
public String getOwner()
{
return owner;
}
public void setOwner( String owner )
{
this.owner = owner;
}
public int getLikes()
{
return likes;
}
public void setLikes ( int likes )
{
this.likes = likes;
}
public String getIsTagEmpty()
{
return isTagEmpty;
}
public void setIsTagEmpty ( String isTagEmpty )
{
this.isTagEmpty = isTagEmpty;
}
public String getIsImageUrlEmpty()
{
return isImageUrlEmpty;
}
public void setIsImageUrlEmpty ( String isImageUrlEmpty )
{
this.isImageUrlEmpty = isImageUrlEmpty;
}
public String getStatus()
{
return status;
}
public void setStatus( String status )
{
this.status = status;
}
public String getTag()
{
return tag;
}
public void setTag( String tag )
{
this.tag = tag;
}
public String getProfilePictureURL()
{
return profilePictureURL;
}
public void setProfilePictureURL ( String profilePictureURL )
{
this.profilePictureURL = profilePictureURL;
}
public String getImageUrl()
{
return imageURL;
}
public void setImageUrl ( String imageURL )
{
this.imageURL = imageURL;
}
public Date getCreated()
{
return created;
}
public Date getUpdated()
{
return updated;
}
public List<BackendlessUser> getUsersThatLike() {
return usersThatLike;
}
public void setUsersThatLike(List<BackendlessUser> usersThatLike) {
this.usersThatLike = usersThatLike;
}
}
So basically now the problem is that the code checks the current user and compare with the Backendless user:
String userId = Backendless.UserService.CurrentUser().getObjectId();
for (Feeds feed: response.getCurrentPage()) {
List<BackendlessUser> likedUsers = feeds.getUsersThatLike();
for (BackendlessUser user : likedUsers)
if (user.getObjectId().equals(userId)) { //do something} else {//do something};
But it’s returning always that “user” equals “userId” so I’m unable to put like on the posts. But I’m able to see the toast if I click the pot I already liked. This means that it returning always true.
I tried to create new posts and than tried to click the like button but it is like disabled and as you can see in my code I didn’t disabled it…
Help please. It’s urgent