-1

I have an adapter class as below with an interface containing onclick listeners.

public interface OnFeedItemClickListener {
    void onCommentsClick(View v, int position);

    void onMoreClick(View v, int position);

    void onProfileClick(View v);
}

On my activity class when i implement one of the methods it works.

@Override
public void onProfileClick(View v) {
    int[] startingLocation = new int[2];
    v.getLocationOnScreen(startingLocation);
    startingLocation[0] += v.getWidth() / 2;
    UserProfileActivity.startUserProfileFromLocation(startingLocation, this);
    overridePendingTransition(0, 0);
}

I want help getting an item from the adapter to the activity and displaying it

this is my onViewBinder class

@Override
public void onBindViewHolder(@NonNull FeedAdapter.CellFeedViewHolder viewHolder, final int position) {
    viewHolder.bindView(feedItems.get(position));


    FeedItem item = feedItems.get(position);

    //Making up the feed
    viewHolder.name.setText(item.getName());
mikayiri
  • 39
  • 7
  • Possible duplicate of [How pass data from RecyclerView to activity](https://stackoverflow.com/questions/47182944/how-pass-data-from-recyclerview-to-activity) – ʍѳђઽ૯ท Sep 28 '18 at 08:11

2 Answers2

0

I want help getting an item from the adapter to the activity and displaying it

In your onBindViewHolder(), you can get the item's Name(for example) by item.getName() like you are already doing.

This will set the name for your viewHolder.name. So, to get and set the other items, you'll need to customize your list_item then declaring the items in ViewHolder and then setting in onBindViewHolder().

But now the point is, it should show the name on the items in your Activity.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • I already have the on View Binder. I found out the values can be got from the on view binder. Now me I want help on how to do it on one of the methods on the interface because it is in the activity where it can be accessed. I want to be able to get the value from the adapter to the activity – mikayiri Sep 28 '18 at 08:00
0

If I understand correctly your question, you want the object clicked passed back to your activity. In that case just pass it in the callback, for example:

public interface OnFeedItemClickListener {
   void onCommentsClick(View v, FeedItem item);

   void onMoreClick(View v, FeedItem item);

   void onProfileClick(View v, FeedItem item);
}

So instead of the position, you can directly pass the object related to the clicked row and retrieve/display/do what you want with desired information.

  • If we see how the value is got from the on Bind View method, won't we need to pass to the view holder onto the methods and also the position so that we get the value – mikayiri Sep 28 '18 at 08:06
  • You are getting the "value" (I guess is the obkject?) doing `FeedItem item = feedItems.get(position);` so you don't need to pass the ViewHolder or the position, but just this – Alessandro Mautone Sep 28 '18 at 08:10