【问题标题】:How can I make a ExpandableListView on Mono for Android如何在 Mono for Android 上制作 ExpandableListView
【发布时间】:2011-12-29 19:13:04
【问题描述】:

我正在尝试制作一个可扩展的 ListView,但我不知道如何从 ExpandableListAdapter 检索某些方法的信息,所以这是我的代码: 这是主要活动:

[Activity (Label = "AR Transactions Details")]          
public class ARInquiry : ExpandableListActivity
{
    private ARExpandableListAdapter adapter;
    private string[] groups = {"1","2","3","4"};
    private string[,] children = {
            { "Arnold", "Barry", "Chuck", "David" },
            {"Ace", "Bandit", "Cha-Cha", "Deuce"},
            { "Fluffy", "Snuggles", "", ""},
            { "Goldy", "Bubbles", "", "" }
    };

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView(Resource.Layout.ar_inquiry);

        //Restrieve the ExpandableListView from the layout
        ExpandableListView listview = FindViewById<ExpandableListView>(Android.Resource.Id.List);

        //Initialize the adapter with blank groups and children
        //We will be adding children on a thread, and then update the ListView
        adapter = new ARExpandableListAdapter(this, groups, children);

        listview.SetAdapter(adapter);
    }
}

这是 ExpandableListViewAdapter 类代码:

public class ARExpandableListAdapter : BaseExpandableListAdapter
{
    private Context context;
    private string[] groups;
    private string[,] children;

    public ARExpandableListAdapter(Context context, string[] groups, string[,] children)
    {
        this.context = context;
        this.groups = groups;
        this.children = children;
    } 


    public override Java.Lang.Object GetChild (int groupPosition, int childPosition)
    {
        return children[groupPosition, childPosition];
    }

    public override long GetChildId (int groupPosition, int childPosition)
    {
        return childPosition;
    }

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

        TextView tv = (TextView) convertView.FindViewById(Resource.Id.tvChild);
        if(isLastChild == true)
        {
          return convertView;   
        }
        tv.SetText(" " + children[groupPosition, childPosition], TextView.BufferType.Normal);

        return convertView;
    }

    public override int GetChildrenCount (int groupPosition)
    {
        return children[groupPosition, 3];
    }

    public override Java.Lang.Object GetGroup (int groupPosition)
    {
        return groups[groupPosition];
    }

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

    public override View GetGroupView (int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
    {
        string _group = (string)GetGroup(groupPosition);
        if(convertView==null)
        {
            LayoutInflater infalInflater = (LayoutInflater) context.GetSystemService(Context.LayoutInflaterService);
            convertView = infalInflater.Inflate(Resource.Layout.group_layout, null);
        }
        TextView tv = (TextView)convertView.FindViewById(Resource.Id.tvGroup);
        tv.SetText(_group, TextView.BufferType.Normal);
        return convertView;
    }

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

    public override int GroupCount {
        get {
            return groups.Count();
        }
    }

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


}

如何获取 GetChildrenCount 方法的长度?

【问题讨论】:

    标签: c# xamarin.android expandablelistview expandablelistadapter


    【解决方案1】:

    要获得子组的长度,它应该是:

    public override int GetChildrenCount (int groupPosition)
    {
        return children[groupPosition].Length;
    }
    

    【讨论】:

      【解决方案2】:

      我这样解决,因为它的工作原理:

      public class ARExpandableListAdapter : BaseExpandableListAdapter
      {
      
          private Context context;
          private IList<string> groups;
          private IList<IAccountsReceivable> children;
      
          public ARExpandableListAdapter(Context context, IList<string> groups, IList<IAccountsReceivable> children)
          {
              this.context = context;
              this.groups = groups;
              this.children = children;
          } 
      
          #region implemented abstract members of Android.Widget.BaseExpandableListAdapter
          public override Java.Lang.Object GetChild (int groupPosition, int childPosition)
          {
      
              return (Java.Lang.Object)children[groupPosition]; //as Java.Lang.Object.JavaCast<decimal>();
          }
      
          public override long GetChildId (int groupPosition, int childPosition)
          {
              return childPosition;
          }
      
          public override View GetChildView (int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
          {
              if(convertView == null)
              {
                  LayoutInflater infaInflater = (LayoutInflater) context.GetSystemService(Context.LayoutInflaterService);
                  convertView = infaInflater.Inflate(Resource.Layout.child_layout, null);
              }
      
              if(childPosition<6)
              {
                  string label = " ";
                  if (childPosition == 0)
                      label = children[groupPosition].TransactionType;
                  if (childPosition == 1)
                      label = children[groupPosition].TransactionValue.ToString();
                  if (childPosition == 2)
                      label = children[groupPosition].TransactionDate.ToString();
                  if (childPosition == 3)
                      label = children[groupPosition].TransactionDueDate.ToString();
                  if (childPosition == 4)
                      label = children[groupPosition].OrderNumber.ToString();
                  if (childPosition == 5)
                      label = children[groupPosition].CustomerReference;
      
      
              TextView tv = (TextView) convertView.FindViewById(Resource.Id.tvChild);
              tv.SetText(" " + label, TextView.BufferType.Normal);
              }
              return convertView;
          }
      
          public override int GetChildrenCount (int groupPosition)
          {
              return children.Count;
          }
      
          public override Java.Lang.Object GetGroup (int groupPosition)
          {
              return groups[groupPosition];
          }
      
          public override long GetGroupId (int groupPosition)
          {
              return groupPosition;
          }
      
          public override View GetGroupView (int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
          {
              string _group = (string)GetGroup(groupPosition);
              if(convertView==null)
              {
                  LayoutInflater infalInflater = (LayoutInflater) context.GetSystemService(Context.LayoutInflaterService);
                  convertView = infalInflater.Inflate(Resource.Layout.group_layout, null);
              }
              TextView tv = (TextView)convertView.FindViewById(Resource.Id.tvGroup);
              tv.SetText(_group, TextView.BufferType.Normal);
              return convertView;
          }
      
          public override bool IsChildSelectable (int groupPosition, int childPosition)
          {
              return true;
          }
      
          public override int GroupCount {
              get {
                  return groups.Count();
              }
          }
      
          public override bool HasStableIds {
              get {
                  return true;
              }
          }
          #endregion
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-04
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-05
        • 2012-10-31
        • 1970-01-01
        相关资源
        最近更新 更多