c# - 适用于 Android 的 ExpandableListView Mono

标签 c# listview xamarin.android expandablelistview

我正在尝试做这个例子 http://techdroid.kbeanie.com/2010/09/expandablelistview-on-android.html ,但是使用 monodroid,我的问题是我以前用 java 编程但是我有问题调用 BaseExpandableListAdapter Abstracs 方法上的子列表,因为我需要例如从列表中放置 groupPosition 和 childPosition,所以我怎么能解决这个问题?

最佳答案

创建 ExpandableListAdapter 的问题在于这些方法要求您返回 Java.Lnag.Object 的类型。 我无法获得您的 java 示例上类。

但这里是一个使用 ExpandableListView 的示例,没有定义您自己的 ExpandableListAdapter 而只是使用 SimpleExpandableListAdapter 类。

此示例将使您能够使用 ExpandableListView,但它不会为您提供 BaseExpandableListAdapter 带来的额外灵 active 。

using System;
using System.Collections.Generic;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Scratch.ExpandableListActivity
{
    [Activity (Label = "Scratch.ExpandableListActivity", MainLauncher = true)]
    public class Activity1 : Android.App.ExpandableListActivity
    {
        IExpandableListAdapter mAdapter;
        const string Name = "NAME";
        const string IsEven = "IS_EVEN";

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            using (var groupData = new JavaList<IDictionary<string, object>> ())
            using (var childData = new JavaList<IList<IDictionary<string, object>>> ()) {
                for (int i = 0; i < 20; i++) {
                    using (var curGroupMap = new JavaDictionary<string, object>()) {
                        groupData.Add(curGroupMap);
                        curGroupMap.Add(Name, "Group " + i);
                        curGroupMap.Add(IsEven, (i % 2 == 0) ? "This group is even" : "This group is odd");
                        using (var children = new JavaList<IDictionary<string, object>> ()) {
                            for ( int j = 0; j < 15; j++) {
                                using (var curChildMap = new JavaDictionary<string, object> ()) {
                                    children.Add(curChildMap);
                                    curChildMap.Add(Name, "Child " + j);
                                    curChildMap.Add(IsEven, (j % 2 == 0) ? "This child is even" : "This child is odd");
                                }
                            }
                            childData.Add(children);
                        }
                    }
                }
                // Set up our adapter
                mAdapter = new SimpleExpandableListAdapter (
                        this,
                        groupData,
                        Android.Resource.Layout.SimpleExpandableListItem1,
                        new string[] { Name, IsEven},
                        new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 },
                        childData,
                        Android.Resource.Layout.SimpleExpandableListItem2,
                        new string[] { Name, IsEven },
                        new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 }
                        );
                SetListAdapter(mAdapter);
            }
        }
    }
}

关于c# - 适用于 Android 的 ExpandableListView Mono,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8674628/

相关文章:

c# - MVC4 中的 Styles.Render

c# - 查看几个 DateTime 数组之间是否存在任何匹配项的最简单方法是什么?

c# - 尽管变量确实存在,但在当前上下文中不存在

android - 如何从 Android 中的 ListView 禁用(暗淡)onItemLongClick 中的选项?

c# - Android 位置时间戳转换为 C-Sharp DateTime

c# - Xamarin : Testing Android application using Moq/NUnit and the MockContext?

c# - 自定义控件和事件处理?

android - 如何根据android中的内容设置Listview项目不可点击

android - 防止在 ListView 中双重选择

android - 有没有办法将 ProGuard 与 MonoDroid 一起使用?