how to go to a new activity from a card view click event

aaravaarav ✭✭INMember ✭✭

i see no such post in xamarin for going from card view click event to a new activity thanks in advance

Answers

  • BerayBentesenBerayBentesen ✭✭✭✭ TRUniversity ✭✭✭✭

    @aarav CardView is a View like Button etc. Define your CardView in activity with FindViewById and then you will be able to use Click method like button.Click += ... That is all.

  • aaravaarav ✭✭ INMember ✭✭
    edited December 2016

    @BerayBentesen thanx for reply i did exactly what u said lets ay id my card view id is is card_view then what i m doing is:

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        var card = DoctorsRecycleList.FindViewById<TextView>(Resource.Id.card_view);
        card.Click += Card_Click;
    }
    
    private void Card_Click(object sender, EventArgs e)
    {
        StartActivity(typeof(Activity1));
    }
    

    here i m getting an error start activity doesnot exist in current context
    thanks in advance

  • BerayBentesenBerayBentesen ✭✭✭✭ TRUniversity ✭✭✭✭

    @aarav it is not correct way to use click event with adapter. I thought you were trying to access card view directly, not from adapter.

    So, since recycler view does not have click method by default you must use event handler by yourself. It is quite easy, just follow guide from here or check sample code.

    If you have any problem, let me know again.

  • ShahanaHabeebShahanaHabeeb USMember

    Hi just for the record, you can write click events within adapter. I have used this approach many times. As for aaravs case, where are u inflating your item layout.. Also since you are using RecyclerView you should be following the ViewHolder methodology, in that case you will be using OnBindViewHolder inorder to initialize your view and register click/drag events etc.

  • aaravaarav ✭✭ INMember ✭✭
    edited December 2016

    @BerayBentesen i have followed the guide and i have added the click event but now where i can add my start activity method thanks

  • aaravaarav ✭✭ INMember ✭✭

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.Support.V7.Widget;
    using System.Net;
    using Android.Graphics;
    using Newtonsoft.Json;
    using TabbedApp.Helper;

    namespace TabbedApp
    {
    [Activity(Label = "ChooseDocActivity")]
    public class ChooseDocActivity : Activity
    {
    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    private WebClient mClient;
    private Uri mUrl;
    List Info;
    RecyclerAdapter mAdapter;
    protected override void OnCreate(Bundle savedInstanceState)
    {
    base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.Choosedoc);
    
            //webclient start
            Info = new List<DocInfo>();
            mClient = new WebClient();
            mUrl = new Uri("http://thecybriainc-001-site32.itempurl.com/api/BmmDataFetch/GetFetchDoctordata/1");
            mClient.DownloadDataAsync(mUrl);
            mClient.DownloadDataCompleted += MClient_DownloadDataCompleted;
    
            recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
    
    
            mAdapter = new RecyclerAdapter(Info);
            layoutManager = new LinearLayoutManager(this, LinearLayoutManager.Vertical, false);
            recyclerView.SetLayoutManager(layoutManager);
    
            mAdapter.ItemClick += MAdapter_ItemClick;
            recyclerView.SetAdapter(new RecyclerAdapter(Info));
    
    
        }
    
        private void MAdapter_ItemClick(object sender, int e)
        {
            int photoNum = e + 1;
            StartActivity(typeof(IconTextDataFragment));
        }
    
        private Bitmap GetImageBitmapFromUrl(string url)
        {
            Bitmap imageBitmap = null;
    
            using (var webClient = new WebClient())
            {
                var imageBytes = webClient.DownloadData(url);
                if (imageBytes != null && imageBytes.Length > 0)
                {
                    imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
                }
            }
    
            return imageBitmap;
        }
    
    
        private void MClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            RunOnUiThread(() =>
            {
                string json = System.Text.Encoding.UTF8.GetString(e.Result);
                Info = JsonConvert.DeserializeObject<List<DocInfo>>(json);
                foreach (var item in Info)
                {
                    item.ImagePath = "http://www.bookmymedic.com/" + item.ImagePath;
                    var Img = item.ImagePath;
    
    
    
                }
                recyclerView.SetAdapter(new RecyclerAdapter(Info));
    
            });
        }
    
    
    
    }
    public class RecyclerAdapter : RecyclerView.Adapter
    {
        private List<DocInfo> nInfo;
        private Context context;
        public event EventHandler<int> ItemClick;
    
        public RecyclerAdapter(List<DocInfo> Info)
        {
            nInfo = Info;
        }
    
    
    
        public class Myview : RecyclerView.ViewHolder
        {
            public View mMainview { get; set; }
            public ImageView ImageView1 { get; set; }
            public ImageView ImageView2 { get; set; }
            public TextView Name { get; set; }
            public TextView DocName { get; set; }
            public TextView Fee { get; set; }
            public TextView WorkExp { get; set; }
            public String ImagePath { get; set; }
            public Button Mbook { get; set; }
    
            public Myview(View itemView, Action<int> listener) 
            : base (itemView)
            {
                itemView.Click += (sender, e) => listener(base.Position);
            }
    
            public Myview(View view) : base(view)
            {
                mMainview = view;
                Context context = view.Context;
    
            }
    
        }
        public override int ItemCount
        {
            get
            {
                return nInfo.Count;
            }
        }
        void OnClick(int position)
        {
            if (ItemClick != null)
                ItemClick(this, position);
        }
    
        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
    
            //var item = Items[position];
            Myview myholder = holder as Myview;
    
            //myholder.mMainview.Click += delegate
            //{
            //    var activity2 = new Intent(context, typeof(IconTextDataFragment));
    
            //    context.StartActivity(activity2);
            //};
    
    
    
    
    
            //myholder.ImagePath.SetImageResource(nInfo[position].ImagePath);
            myholder.Name.Text = nInfo[position].HospitalName;
            myholder.DocName.Text = nInfo[position].Name;
    
    
    
            myholder.ImageView1.SetImageBitmap(BusinessHelper.GetImageBitmapFromUrl(nInfo[position].ImagePath));
        }
    
    
    
        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            View DoctorsRecycleList = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.DoctorsRecycleList, parent, false);
    
            ImageView imageView1 = DoctorsRecycleList.FindViewById<ImageView>(Resource.Id.list_image);
            TextView Name = DoctorsRecycleList.FindViewById<TextView>(Resource.Id.Price);
            TextView DocName = DoctorsRecycleList.FindViewById<TextView>(Resource.Id.Name);
            //var card = DoctorsRecycleList.FindViewById<TextView>(Resource.Id.card_view);
            Button MBook = DoctorsRecycleList.FindViewById<Button>(Resource.Id.Bookbutton);
            TextView Fee = DoctorsRecycleList.FindViewById<TextView>(Resource.Id.total);
            TextView WorkExp = DoctorsRecycleList.FindViewById<TextView>(Resource.Id.tv_quantity);
            //ImageView imageView2 = DoctorsRecycleList.FindViewById<ImageView>(Resource.Id.list_image1);
            //card.Click += Card_Click;
    
    
            Myview view = new Myview(DoctorsRecycleList, OnClick)
            {
                ImageView1 = imageView1,
                Mbook = MBook,
                Name = Name,
                DocName = DocName,
                Fee = Fee,
                WorkExp = WorkExp,
                //DocName1 = DocName1
    
    
            }; return view;
        }
    
    }
    

    }

  • aaravaarav ✭✭ INMember ✭✭

    @BerayBentesen that was my code

  • BerayBentesenBerayBentesen ✭✭✭✭ TRUniversity ✭✭✭✭

    @aaravYou already did it. What is the problem ?

                mAdapter.ItemClick += MAdapter_ItemClick;
    

    That code should work.

  • aaravaarav ✭✭ INMember ✭✭

    its not working

  • aaravaarav ✭✭ INMember ✭✭

    it not goining to a new activity @BerayBentesen

  • BerayBentesenBerayBentesen ✭✭✭✭ TRUniversity ✭✭✭✭

    @aarav use this line after initializing adapter

        ...
        mAdapter = new RecyclerAdapter(Info);
    
        recyclerView.SetAdapter(new RecyclerAdapter(Info));
    
            ...
    

    Then use mAdapter.NotifyDataSetChanged(); to notify recycler view.

    Also pass Context as a constructor parameter. For correct adapter setup you could check my source code here

  • aaravaarav ✭✭ INMember ✭✭

    @BerayBentesen sorry for being late i was out of town m getting an error in
    mAdapter = new RecyclerAdapter(Info);
    there is no argument given that corresponds to required formal parameter context of recyleradapter(list,context)

Sign In or Register to comment.