【问题标题】:Is it possible to use a ListView without ListActivity in Mono Android?是否可以在 Mono Android 中使用没有 ListActivity 的 ListView?
【发布时间】:2011-04-29 12:57:07
【问题描述】:

我正在尝试使用 ListView 在其他小部件中实现活动。因此,如果我理解正确,我将无法使用 ListActivity(因为它只显示一个对我来说没问题的大 ListView)。

我从 SO 中找到了很多在 java 中执行此操作的不同示例,例如 this 或者 thisgreat example.
我试过以同样的方式做,但它不能正常工作。我很好奇这个功能是否存在于单声道android中?

我发现只有一个在单声道 android 中使用 ListView 的例子是 this 这个例子只描述了使用 ListActivity。

所以,我的布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="@+id/widget36"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
    <ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    </ListView>
</TableLayout>

我的 OnCreate:

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

        JavaList<string> somadata = new JavaList<string> { "111", "222", "333" };

        ListView view = FindViewById<ListView>(Resource.Id.list);
        view.Adapter = new ArrayAdapter<string>(this, Resource.Layout.CreateUser, somadata);
        view.TextFilterEnabled = true;          
    }

【问题讨论】:

    标签: android mono android-widget xamarin.android


    【解决方案1】:

    我刚刚找到了解决方案。我在这一行有错误:

    view.Adapter = new ArrayAdapter<string>(this, Resource.Layout.CreateUser, somadata);
    

    This 话题对我帮助很大。

    这里是进一步的布局列表, 你可以use

    预定义布局的来源是here

    因此,在 Mono Android 中,有两个选项可以在没有 ListActivity 的情况下修复错误并使 ListView 工作。
    第一:
    只需复制/粘贴名为simple_list_item_1 的标准资源布局:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:minHeight="?android:attr/listPreferredItemHeight"
    />
    

    第二个(我相信这是最好的方法):
    只需使用标准资源。在 Mono Android 中,它命名为 Android.Resource.Layout.SimpleListItem1
    所以我的 OnCreate 更新版本完美运行:

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.CreateUser);
    
            JavaList<string> somadata = new JavaList<string> { "111", "222", "333" };
    
            ListView view = FindViewById<ListView>(Resource.Id.list);
            view.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, somadata);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      相关资源
      最近更新 更多