how to call intent in adapter class in android

how to call intent in adapter class in android

To call an Intent in an adapter class in Android, you can use the context object that is usually passed to the adapter's constructor to start the Intent.

Here's an example of how you might do this in an adapter class:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private Context context;
    private List<Item> items;

    public MyAdapter(Context context, List<Item> items) {
        this.context = context;
        this.items = items;
    }

    // Other adapter methods

    public void onItemClick(int position) {
        Item item = items.get(position);
        Intent intent = new Intent(context, DetailActivity.class);
        intent.putExtra("item", item);
        context.startActivity(intent);
    }
}
Source:w‮tual.ww‬turi.com

In this example, the MyAdapter class has a Context object called context and a list of Item objects called items. The onItemClick method is called when an item in the adapter is clicked, and it starts an Intent to open the DetailActivity class, passing the clicked Item object as an extra.

Created Time:2017-11-01 12:05:12  Author:lautturi