android - 是否可以在 Mono Android 中使用不带 ListActivity 的 ListView?

标签 android mono android-widget xamarin.android

我正在尝试使用 ListView 和其他小部件来实现 Activity 。因此,如果我理解正确,我就不能使用 ListActivity(因为它只显示一个对我来说没问题的大 ListView)。

我从 SO 中找到了很多在 Java 中执行此操作的不同示例,例如 this 或者 thisgreat example .
我试过以同样的方式做,但它不能正常工作。我很好奇这个功能是否存在于 mono 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;          
    }

最佳答案

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

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

This主题对我帮助很大。

Here is a further list of layouts that you can use.

Sources of predefined layouts are here.

因此,有两个选项可以解决错误并使 ListView 在没有 ListActivity 的情况下在 Mono Android 中工作。
第一:
只需复制/粘贴名为 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);
    }

关于android - 是否可以在 Mono Android 中使用不带 ListActivity 的 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5832338/

相关文章:

android - 任何优秀的文本编辑器 - Android 应用程序 - 为程序员优化?

java - 不要显示 "This app won' t run without google play services”对话框

javascript - 如何在我们的应用程序中使用此 sencha 触摸图像?

c# - 如何使用 Monotouch 遍历 CGPDFDictionary?

c++ - 如何使用 GCC 配置 Mono Include Directory 路径

android - 无法从 CellSignalStrengthLte Android 获取 RSRP 和 RSRQ 的正确值

c# - 获取 DNS TXT 记录

android - 如何在android中像ipad一样分屏?

android - 如何在android中用文本显示圆弧

java - 单击按钮时如何立即更新小部件 View