I deduce articles of a certain group from VK in RecyclerView. Right now I want to implement likes. Everything works without problems, but the likes are placed not on those articles, but with an offset of one. help fix?
adapter such:
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> { private LayoutInflater mInflater; private int mShowStyle; private int musicShowStyle; VKPostArray obj; VKApiPost p; VKApiPhoto vkPhoto; ArrayList<String> imgUrls1; VKApiUser mainUser; Context ctx; Picasso picasso; public int i=0; private int lastPosition = -1; private final static int FADE_DURATION = 1000; private final static int PHOTO = 0; private final static int MUSIC = 1; public PostAdapter(Context context, VKPostArray posts, VKApiUser user, int showStyle) { super(); this.obj = posts; this.mainUser = user; this.mInflater = LayoutInflater.from(context); this.mShowStyle = showStyle; ctx = context; } @Override public void onBindViewHolder(final PostViewHolder holder, final int position) { p = obj.get(position); holder.textPost.setText(p.text); Linkify.addLinks(holder.textPost, Linkify.WEB_URLS); holder.like1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { setLike("post", "group", p.getId(), position); Toast.makeText(ctx, "Лайк поставлен", Toast.LENGTH_SHORT).show(); }else{ setDezLike("post", "group", p.getId(), position); Toast.makeText(ctx, "Лайк снят", Toast.LENGTH_SHORT).show(); }} }); holder.textlikePost.setText("" + p.likes_count); holder.textrepostPost.setText("" + p.reposts_count); SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm"); holder.textDatePost.setText(format.format(new java.util.Date(p.date * 1000))); VKAttachments att = new VKAttachments(); att = p.attachments; int i; imgUrls1 = new ArrayList<>(att.size()); try { for (i = 0; i <= att.size(); i++) { vkPhoto = (VKApiPhoto) att.get(i); imgUrls1.add(vkPhoto.photo_604); } } catch (Exception e) { holder.mNglContent.setImagesData(imgUrls1); } @Override public int getItemCount() { return obj.size(); } @Override public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (mShowStyle == NineGridImageView.STYLE_FILL) { return new PostViewHolder(mInflater.inflate(R.layout.recycler_item_wall, parent, false)); } else { return new PostViewHolder(mInflater.inflate(R.layout.recycler_item_wall, parent, false)); } } public class PostViewHolder extends RecyclerView.ViewHolder { CardView cv; TextView textPost, news_name, textDatePost, textrepostPost, textlikePost, autor, artistMusic, albumMusic, durationMusic, urlMusic; ImageView like, repost, playMusic; CircleImageView news_ava; CheckBox like1; NineGridImageView mNglContent; GridView grid; private NineGridImageViewAdapter<String> mAdapter = new NineGridImageViewAdapter<String>() { @Override protected void onDisplayImage(Context context, ImageView imageView, String s) { Picasso.with(context).load(s).placeholder(R.drawable.ic_ab_app).into(imageView); } @Override protected ImageView generateImageView(Context context) { return super.generateImageView(context); } @Override protected void onItemImageClick(Context context, int position, List<String> list) { //// } }; public PostViewHolder(View itemView) { super(itemView); cv = (CardView) itemView.findViewById(R.id.card_view_wall); news_name = (TextView) itemView.findViewById(R.id.news_name); news_ava = (CircleImageView) itemView.findViewById(R.id.news_ava); textDatePost = (TextView) itemView.findViewById(R.id.textDatePost); textPost = (TextView) itemView.findViewById(R.id.textPost); textlikePost = (TextView) itemView.findViewById(R.id.tv_likes_count); like1 = (CheckBox) itemView.findViewById(R.id.iv_like_button); textrepostPost = (TextView) itemView.findViewById(R.id.tv_reposts_count); repost = (ImageView) itemView.findViewById(R.id.iv_repost); mNglContent = (NineGridImageView) itemView.findViewById(R.id.ngl_images); mNglContent.setAdapter(mAdapter); } } public static String setLike(String type, String owner_id, int item_id, int position ) { final String[] res = {""}; VKRequest request = new VKRequest("likes.add", VKParameters.from("type", type, "owner_id", owner_id, "item_id", item_id)); request.executeSyncWithListener(new VKRequest.VKRequestListener() { @Override public void onComplete(VKResponse response) { super.onComplete(response); try { JSONObject jsonObject = response.json.getJSONObject("response"); res[0] = jsonObject.getString("likes"); } catch (JSONException e) { e.printStackTrace(); }} }); return res[0]; } public static String setDezLike(String type, String owner_id, int item_id, int position ) { final String[] res = {""}; VKRequest request = new VKRequest("likes.delete", VKParameters.from("type", type, "owner_id", owner_id, "item_id", item_id)); request.executeSyncWithListener(new VKRequest.VKRequestListener() { @Override public void onComplete(VKResponse response) { super.onComplete(response); try { JSONObject jsonObject = response.json.getJSONObject("response"); res[0] = jsonObject.getString("likes"); } catch (JSONException e) { e.printStackTrace(); }} }); return res[0]; } } shared with onCreate:
VKRequest reqWall = VKApi.wall().get(VKParameters.from(VKApiConst.OWNER_ID, group, VKApiConst.EXTENDED, 1, VKApiConst.COUNT, 100, "text")); reqWall.setPreferredLang("ru"); reqWall.executeWithListener(new VKRequest.VKRequestListener() { @Override public void onComplete(VKResponse response) { super.onComplete(response); JSONObject jsonObject = response.json; String owner = null; try { owner = (((JSONObject) ((JSONArray) ((JSONObject) jsonObject.get("response")).get("items")).get(0)).getString("owner_id")); } catch (JSONException e) { e.printStackTrace(); } posts = new VKPostArray(); try { posts.parse(response.json); } catch (JSONException e) { e.printStackTrace(); } try { JSONArray array = ((JSONArray) ((JSONObject) jsonObject.get("response")).get("groups")); } catch (JSONException e) { e.printStackTrace(); } try { VKApiPost post = posts.get(0); } catch (Exception e) { } rv = (RecyclerView) findViewById(R.id.userWall); rv.setLayoutManager(new LinearLayoutManager(context)); mNineImageAdapter = new PostAdapter(MainActivity.this, posts, mainUser, NineGridImageView.STYLE_GRID); rv.setAdapter(mNineImageAdapter); mNineImageAdapter.notifyDataSetChanged();