user7629010
user7629010

Reputation:

Start activity from RecyclerView (Xamarin)

I have Xamarin app with RecyclerView.

I have TextView in block. I want to start activity on click.

Here is code:

public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    var movieViewHolder = (MovieViewHolder)holder;
    movieViewHolder.MovieNameTextView.Text = movies[position].CompanyName;
    var position_new = position + 1;
    movieViewHolder.MovieCount.Text = position_new.ToString();

    movieViewHolder.MovieNameTextView.Click += delegate {
        StartActivity(typeof(ClientLogin));
    };
}

But I have this error:

The name 'StartActivity' does not exist in the current context.
Cannot resolve symbol 'StartActivity'

enter image description here

How I can start activity from Recycler view?

Upvotes: 0

Views: 561

Answers (3)

pinedax
pinedax

Reputation: 9356

You could pass in an instance of the Activity (Context) in the MoviesAdapter constructor. Save this instance in a private field and use it later when navigating to the new Activity on the click event.

Activity _context;

public MovieAdapter(Activity context)
{
    _context = context;
}

When you create the object of the Adapter you pass in the instance of the Activity:

var movieAdapter = MovieAdapter(this);

Then modify your click delegate to

movieViewHolder.MovieNameTextView.Click += delegate
{
    _context.StartActivity(typeof(ClientLogin));
};

Upvotes: 0

Qw4z1
Qw4z1

Reputation: 3030

Well yes and you should. StartActivity is a function available in the Context class, not the adapter. So grab a instance of context (maybe from the ViewHolders itemView) and call holder.ItemView.Context.StartActivity(typeof(ClientLogin));.

Upvotes: 0

Nika Kurdadze
Nika Kurdadze

Reputation: 2512

Try this :

movieViewHolder.MovieNameTextView.Context.StartActivity(typeof(ClientLogin));

Upvotes: 1

Related Questions