Pritish
Pritish

Reputation: 2224

Not able to fire the ListVIew Item click event when there is UI Control like Button in xamarin

Here I am not able to fire the ListView Item click event. this is my code with MainActivity.cs file , Main.axml file and MainActivityAdapter.cs file.

MainActivity .cs

public class MainActivity : Activity
        {
            private ListView _contactsView;
            private List<Contact> _contacts;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

//not able to fire event
            FindViewById<Button>(Resource.Id.add).Click += (s, e) =>
            {
                var intent = new Intent(this, typeof(EditPhoneDetailsActivity));
                base.StartActivity(intent);
            };

            _contactsView = FindViewById<ListView>(Resource.Id.homeListViewMain);
            if(_contactsView!=null){
                _contactsView.ItemClick += (s, e) =>
                    {
                        var intent = new Intent(this, typeof(EditPhoneDetailsActivity));
                        intent.PutExtra("SelectedContactId", _contacts[e.Position].Id.ToString());
                        base.StartActivity(intent);
                    };
            }

        }
        protected override void OnResume()
        {
            base.OnResume();
            _contacts = new ContactManager().GetContacts();
            _contactsView.Adapter = new MainActivityAdapter(this, _contacts);
        }
    }

Main.axml

    <!-- language: lang-xml -->

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">
        <ListView
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/homeListViewMain" />
        <RelativeLayout
            android:id="@+id/homeInnerRelativeLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">
            <Button
                android:id="@+id/add"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="Add Personal Details" />
        </RelativeLayout>
    </RelativeLayout>

MainActivityAdapter.cs

    public class MainActivityAdapter : BaseAdapter<Contact>
    {
        Activity _context;
        private List<Contact> _contacts;

        public MainActivityAdapter(Activity context, List<Contact> contacts)
            :base()
        {
            this._context = context;
            this._contacts = contacts;
        }
        public override Contact this[int position]
        {
            get
            {
                return _contacts[position];
            }
        }

        public override int Count
        {
            get
            {
                return _contacts.Count;
            }
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = _contacts[position];
            View view = convertView;
            if (view == null)
                view = _context.LayoutInflater.Inflate(Resource.Layout.TestListViewLoader, null);

            view.FindViewById<TextView>(Resource.Id.textout).Text =" Name :"+ item.ContactPersonName;
            view.FindViewById<Button>(Resource.Id.btnCall);
            return view;
        }
    }

TestListViewLoader.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">
    <LinearLayout
        android:id="@+id/Text"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dip">
        <TextView
            android:id="@+id/textout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#0099CC"
            android:textSize="14dip"
            android:textStyle="italic" />
    </LinearLayout>
    <Button
        android:id="@+id/remove"
        android:layout_width="75dp"
        android:layout_height="40dp"
        android:textSize="10dip"
        android:textColor="#FF267F00"
        android:paddingLeft="22dip"
        android:layout_alignParentRight="true"
        android:text="Email" />
    <Button
        android:id="@+id/btnCall"
        android:layout_width="75dp"
        android:layout_height="40dp"
        android:textSize="10dip"
        android:textColor="#FF267F00"
        android:paddingLeft="22dip"
        android:layout_toLeftOf="@id/remove"
        android:text="Call" />
<!--<ListView
    android:id="@+id/viewCallDetailListView"
    android:layout_width="fill_parent"
    android:layout_height="210dip" />-->
</RelativeLayout>

this is simple code but I dont know why this is happens it will not fire the listview item click event in the listView I am adde two button and some text. here do i need to set any property to fire such event I am new to xamarin.

Upvotes: 1

Views: 1464

Answers (1)

Pritish
Pritish

Reputation: 2224

I solve my issue just single line of code

set "Focusable = false" on the Button in the cell.

Upvotes: 2

Related Questions