4

I am trying to pass position of recyclerview to another activity where I am calliing retrofit Interface. Can I pass position of Recyclerview item so I can load different post from api

 @Override
public void onBindViewHolder(TagAdapter.Tag_ViewHolder holder, final int position) {
    holder.tagName.setText(Tags.get(position));
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (position < 8){
                int  pos = (int) getItemId(position) + 1;
                Intent intent = new Intent (view.getContext(), Tag1Post.class);
                intent.putExtra("pos", pos);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);

            }
        }

My EndPoint is

@GET("/tag/{id}/")
Call<Tag> test(@Path("id") int id);

I want to replace id by RecyclerView position.

Hemraj Rijal
  • 65
  • 1
  • 2
  • 12

4 Answers4

2

You can create an interface then transfer the position from the adapter to the activity.

holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (position < 8){
                int  pos = (int) getItemId(position) + 1;
                Intent intent = new Intent (view.getContext(), Tag1Post.class);
                intent.putExtra("pos", pos);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                //create your interface in Activity, then send it to adapter and call it here:
                yourListener.onTransferPosition(position);
            }
        }

On your activity that implements interface:

 public void onTransferPosition(int position) {
    this.position= position;
  }
Bui Quang Huy
  • 1,784
  • 2
  • 17
  • 50
2

Instead of passing the position of Recycler View I pass the id of the particular post to navigate to comment on that post.

1. Provide Intent Extra with id of clicked post on Bindview holder of Adapter

public void onBindViewHolder(PostViewHolder holder, final int position) {
        holder.postTitle.setText(posts.get(position).getTitle());
        holder.postBody.setText(posts.get(position).getBody());
        setAnimation(holder.itemView);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = posts.get(position).getId(); // get Id
                Intent intent = new Intent(context, CommentActivity.class);
                intent.putExtra("pos", id); // Pass Id
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
    }

2. Retrieve passed intent data

int mPostId = getIntent().getIntExtra("pos", 0);

3. Pass retrieved data to the retrofit endpoints

Call<List<Comment>> call = apiService.getComments(mPostId);

Retrofit Endpoints

@GET("/posts/{id}/comments/")
    Call<List<Comment>> getComments(@Path("id") int id);

I hope this will help someone to encounter this type of problems.

Happy Coding :)

Hemraj Rijal
  • 65
  • 1
  • 2
  • 12
0

Since you are passing the view position using Intent class you can simply get the position in your second activity as below

int position = getIntent().getExtras().getInt("pos");

Or you can pass value using a interface

  public interface OnRecyclerClickListener  {
    void makeRestCall(int position);
}

Instantiate the interface as follow

onRecyclerClickListener  = (OnRecyclerClick) this.mContext;

send the view position like below

 holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (position < 8){
            int  pos = (int) getItemId(position) + 1;
            startActivity(new Intent (view.getContext(), Tag1Post.class));
            onRecyclerClickListener.makeRestCall(pos);    
        }
    }

Finally, implement interface in your second activity

public class SecondActivity extends AppCompatActivity implements OnRecyclerClickListener{

@Override
public void makeRestCall(int pos) {
   // do your GET request
}
Shiran Gabriel
  • 440
  • 4
  • 15
0
private AdapterView.OnItemClickListener mOnClickListener;

Use this in your Adapter and ask this from your constructor.

Adapter(AdapterView.OnItemClickListener onClickListener){
this.mOnClickListener=onClickListener;
}

in your onclick method in holder class do like this:

 @Override
        public void onClick(View view) {

            mOnClickListener.onItemClick(null, view, getAdapterPosition(), view.getId());
        }

And implement AdapterView.OnItemClickListener in you activity

@Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                 // here you will get the postion of the clicked item
    }
Lakhwinder Singh
  • 6,799
  • 4
  • 25
  • 42