How to put getIntent.getStringExtra("username") in android adapter onBindViewHolder(@NonNull final ViewHolder holder, final int position)

i want to call getIntent().getStringExtra() inside this adapter class. I passed username in my previous class. but here I can't call get intent in this adapter class.

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.layout_gig_item, parent, false);

    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {

    final GigHolder gigHolder = gigList.get(position);

    holder.lbl_title.setText(gigHolder.getTitle());
    holder.lbl_price.setText(Utils.getDecimal(gigHolder.getTotal()));
    holder.lbl_contact.setText(gigHolder.getContact());
    holder.img_cover.setImageURI(gigHolder.getImage());
    holder.ll_main.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(!isAdminView){
                Intent intent = new Intent(activity, GigViewActivity.class);
                intent.putExtra(Constants.BUNDLE_ID, gigHolder.getPrimaryKey());
                activity.startActivity(intent);
            }
        }
    });
}

}

Upvotes: 0

Views: 653

Answers (2)

Kidus
Kidus

Reputation: 474

You have two ways to solve this issue:

First way is to pass the Context of the calling Class as a Constructor parameter of the Adapter. You can then access the StringExtra by calling context.getIntent().getStringExtra().

Second way is to get the StringExtra from your calling Class and then pass it as a Contructor parameter of the Adapter.

Upvotes: 0

Clinton
Clinton

Reputation: 21

It would be better to just get the data from the activity or fragment supporting the adapter class and pass it to the adapter class through a function

Upvotes: 0

Related Questions