Listview with checkbox in monodroid

kishukishu INMember ✭✭

I created a list view and used SimpleListItemMulitpleChoice to create multiple checkboxes for my listview after that, I followed the link as it is http://docs.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/part_3_-_customizing_a_listview's_appearance but i am not able to see my checkboxes in my listview and I could not find the values from list view whichever checked.

Posts

  • VijayReddyVijayReddy INMember
    edited September 2013

    Can you post your code.

    And please remember, the right place for posting these type of queries is @ [http://forums.xamarin.com/categories/android] where you can expect right answers soon.

  • kishukishu 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;

    namespace EventNotifier.AndroidUI
    {
    [Activity (Label = "SearchGroup")]
    public class SearchGroupList :Activity
    {
    ListView listView;
    Button btnSubscribe;
    CheckBox chkGroup;
    private SearchGroupAdapter searchGroupAdapter;
    public List groups = new List ();
    protected override void OnCreate (Bundle bundle)
    {

            base.OnCreate (bundle);
            SetContentView (Resource.Layout.SearchGroup);
            listView = FindViewById<ListView>(Resource.Id.searchList); // get reference to the ListView in the layout
            btnSubscribe = FindViewById<Button>(Resource.Id.btnSubscribe);
    
            groups.Add(new Group() {  Id = 1, Name = "TANA", Image = "Test Image"});
            groups.Add(new Group() {  Id = 2, Name = "OASSIS", Image = "Test Image"});
            groups.Add(new Group() {  Id = 3, Name = "KALA", Image = "Test Image"});
            groups.Add(new Group() {  Id = 4, Name = "MANCA", Image = "Test Image" });
            groups.Add(new Group() {  Id = 5, Name = "MAITHRY", Image = "Test Image" });
            groups.Add(new Group() {  Id = 6, Name = "ORUMA", Image = "Test Image" });
            groups.Add(new Group() {  Id = 7, Name = "OASSIS", Image = "Test Image" });
            groups.Add(new Group() {  Id = 8, Name = "MANCA", Image = "Test Image" });
    
            // populate the listview with data
            searchGroupAdapter=new SearchGroupAdapter(this,Android.Resource.Layout.SimpleListItemMultipleChoice, groups);
            listView.Adapter = searchGroupAdapter;
            listView.ChoiceMode = Android.Widget.ChoiceMode.Multiple;
    
            listView.ItemClick += OnListItemClick;  
            btnSubscribe.Click += OnbtnSubscribeClik;   
            chkGroup= FindViewById<CheckBox> (Resource.Id.chkGroup);
            //chkGroup.SetOnCheckedChangeListener += OnCheckedChanged;
        }
    
        protected void OnListItemClick(object sender,Android.Widget.AdapterView.ItemClickEventArgs e)
        {           
            var listView = sender as ListView;
            var t = groups[e.Position];
    
            //var chkGroup= FindViewById<CheckBox> (Resource.Id.chkGroup);
            //chkGroup.Checked = true;
        }
    
    
        protected void OnCheckedChanged(CompoundButton buttonView,bool isChecked)
        {
            if (buttonView.Checked) {
                Toast.MakeText (this, "selected", ToastLength.Short).Show ();
            }
            else
            {
                Toast.MakeText (this, "not selected", ToastLength.Long).Show ();
            }
        }
    

    // cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    //
    // public void onCheckedChanged(CompoundButton buttonView,
    // boolean isChecked) {
    // // TODO Auto-generated method stub
    // if (buttonView.isChecked()) {
    // Toast.makeText(getBaseContext(), "Checked",
    // Toast.LENGTH_SHORT).show();
    // }
    // else
    // {
    // Toast.makeText(getBaseContext(), "UnChecked",
    // Toast.LENGTH_SHORT).show();
    // }
    //
    // }
    // });

        protected void OnbtnSubscribeClik(object sender,EventArgs e)
        {
            var myListView = FindViewById<ListView>(Resource.Id.searchList);
            var sparseArray = FindViewById<ListView>(Resource.Id.searchList).CheckedItemPositions;
    
            for (var i = 0; i < sparseArray.Size(); i++) 
            {
                string selectedValue=string.Empty;
                int position = sparseArray.KeyAt(i);
                var currentItem =listView.Adapter.GetItem(position);
                if (sparseArray.ValueAt (i) == true) 
                {
                    selectedValue = groups [position].Name;
                    //groups.Remove (groups [position]);
                    Android.Widget.Toast.MakeText(this,"Subscribed for "+selectedValue, Android.Widget.ToastLength.Short).Show();
                    //searchGroupAdapter.NotifyDataSetChanged ();
                }
    
                //listView.Adapter = new SearchGroupAdapter(this,Android.Resource.Layout.SimpleListItemMultipleChoice, groups);
            }
            Console.WriteLine();
        }
    }
    

    }

    `

    And adapter is.

    `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;

    namespace EventNotifier.AndroidUI
    {
    class SearchGroupAdapter: BaseAdapter {
    List items;
    Activity context;
    public SearchGroupAdapter(Activity context,int resourceId, List items) : base()
    {
    this.context = context;
    this.items = items;
    }
    public override long GetItemId(int position)
    {
    return position;
    }
    public override Group this[int position]
    {
    get { return items[position]; }
    }
    public override int Count
    {
    get { return items.Count; }
    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
    var item = items[position];
    View view = convertView;

            if (view == null) // no view to re-use, create new
                view = context.LayoutInflater.Inflate(Resource.Layout.SearchGroupCell, null);
    
            view.FindViewById<TextView> (Resource.Id.Text2).Text = item.Name;
            return view;
        }
    }
    

    }

    <?xml version="1.0" encoding="utf-8"?>



    `

    and checkbox design,

    <?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:background="#FFDAFF7F" 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/Text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FF7F3300" android:textSize="20dip" android:textStyle="italic" /> <CheckBox android:id="@+id/chkGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:clickable="false" android:descendantFocusability="beforeDescendants" /> <TextView android:id="@+id/Text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14dip" android:textColor="#FF267F00" android:paddingLeft="100dip" /> </LinearLayout> </RelativeLayout>

Sign In or Register to comment.