0

I want to display to the user a list of all the units and all their sub-units to select from in an ExpandableListView where they can check off which ones they want to move. Since it would seem having clickable Views inside of the ListView prevents it from expanding and collapsing as it naturally would, I was going to simply allow the CheckedChange event to expand/collapse based on the groupPosition. But when I change the check on one CheckBox, several or all groups responds instead of just the group that I'm clicking in.

equiplistparent.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:gravity="center_vertical">
<CheckBox
    android:id="@+id/check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="45dp" />
<TextView
    android:id="@+id/info"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:textSize="20dp"
    android:gravity="left" />
<AutoCompleteTextView
    android:id="@+id/sites"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:textColor="@android:color/black"
    android:background="@android:color/white"
    android:layout_margin="5dp"
    android:textCursorDrawable="@null"
    android:gravity="center_horizontal"
    android:hint="To Store" />
</LinearLayout>

Custom Adapter

class EquipAdapter : BaseExpandableListAdapter
{
    private string[] Parent { get; set; }
    private List<List<CPEquipment>> Child { get; set; }
    private Context _context { get; set; }
    private IListAdapter _adapter { get; set; }
    private ExpandableListView _list { get; set; }

    public EquipAdapter(Context context, List<string> parent, List<List<CPEquipment>> child, IListAdapter adapter, ExpandableListView list)
    {
        _context = context;
        Parent = parent.ToArray();
        Child = child;
        _adapter = adapter;
        _list = list;
    }

    public override Object GetChild(int groupPosition, int childPosition)
    {
        List<CPEquipment> level1 = Child.ElementAt(groupPosition);
        CPEquipment level2 = level1.ElementAt(childPosition);

        return level2.Serial + " " + level2.Model;
    }

    public override long GetChildId(int groupPosition, int childPosition)
    {
        return Convert.ToInt32(groupPosition.ToString(CultureInfo.InvariantCulture) + childPosition.ToString(CultureInfo.InvariantCulture));
    }

    public override int GetChildrenCount(int groupPosition)
    {
        return Child.ElementAt(groupPosition).Count;
    }

    public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) _context.GetSystemService(Context.LayoutInflaterService);
            convertView = inflater.Inflate(Android.Resource.Layout.SimpleExpandableListItem2, null);
        }

        TextView text = (TextView) convertView.FindViewById(Android.Resource.Id.Text2);
        text.Text = GetChild(groupPosition, childPosition).ToString();

        return convertView;
    }

    public override Object GetGroup(int groupPosition)
    {
        return Parent[groupPosition];
    }

    public override long GetGroupId(int groupPosition)
    {
        return groupPosition;
    }

    public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) _context.GetSystemService(Context.LayoutInflaterService);
            convertView = inflater.Inflate(Resource.Layout.equiplistparent, null);
        }

        TextView info = (TextView) convertView.FindViewById(Resource.Id.info);
        info.Text = GetGroup(groupPosition).ToString();
        AutoCompleteTextView acText = (AutoCompleteTextView) convertView.FindViewById(Resource.Id.sites);
        acText.Adapter = _adapter;

        CheckBox check = (CheckBox) convertView.FindViewById(Resource.Id.check);
        check.CheckedChange += (o, args) =>
                                   {
                                       int currGroup = groupPosition;
                                       if (args.IsChecked)
                                           _list.ExpandGroup(currGroup);
                                       else
                                       {
                                           _list.CollapseGroup(currGroup);
                                       }
                                   };
        return convertView;
    }

    public override bool IsChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }

    public override int GroupCount
    {
        get { return Parent.Length; }
    }

    public override bool HasStableIds
    {
        get { return true; }
    }

}

I attempted working around this issue using this suggestion, but it made no difference in the ListViews ability to naturally expand or collapse.

I tried putting the checkboxes in the children instead of parent and just forcing all groups to be open. But now if I check 3 of the boxes, 3 more boxes will just check themselves at random. I know there is a way to use checkboxes in ListViews, but they are not behaving as expected in mine. Is it an issue with MonoDroid?

Cœur
  • 37,241
  • 25
  • 195
  • 267
jmease
  • 2,507
  • 5
  • 49
  • 89

1 Answers1

0

I just recently went through this myself and finally got it all working properly on my end. I posted my original question here along with my final solution to get everything working properly. Not sure if that will help you out, but it sounds REAL close to the issue that I was having and using my solution, I was able to get it working properly.

Community
  • 1
  • 1
  • Thanks. I actually ran across that post in my research for another issue I was having with expandable lists. Doesn't setting the focusable attribute to false on everything make it so none of my clickable views work anymore though? I am also still having that weird problem with checkboxes checking at random but I guess that needs to be a separate post. – jmease Jul 03 '12 at 13:07
  • The setting the focusable attribute to false allows the events to run all the way up the chain. So if you have a expandablelistview and want to manage the click or long click event in the activity itself, you would set that focusable attribute on the adapter views. I was having issue with the events not going all the way up to the listview itself. AS for the checkboxes, I was having the same issue. If you look at the bottom of the CheckListItemAdapter.cs in the question, you will notice that I have 2 classes there that are used for the items. Follow that pattern to fix the checkboxes. – Chaitanya Marvici Jul 03 '12 at 15:32
  • By managing the checked value of the checkbox through those sub classes, that will alleviate your problem with random checkboxes getting checked/unchecked. At that point it's being managed by the list of data and not by the listview itself. – Chaitanya Marvici Jul 03 '12 at 15:35
  • Thanks but when I set the Layouts and their Views to focusable=false as in your example, then the ListView stops being expandable. I also see where you removed your click events in GetChildView. Where then did you add your click events back? – jmease Jul 03 '12 at 19:53
  • Things have gotten pretty fuzzy here so I have posted a new question with more clear and up to date information on the exact problem I am facing now. http://stackoverflow.com/questions/11318920/clickable-views-in-customer-expandablelistview – jmease Jul 03 '12 at 20:47