简体   繁体   中英

Is it possible to use a ListView without ListActivity in Mono Android?

I'm trying to implement activity with ListView among other widgets. So if I understand correctly I can't use ListActivity (because it shows only one big ListView that ist ok for me).

I found a lot different examples of doing this in java from SO like this or this or great example .
I've tried to do in same way but it doesn't work correctly. I'm curious is this functionality exists in mono android at all?

I found only one example of using ListView in mono android which is this and this example describe using ListActivity only.

So, my layout:

<?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>

My 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;          
    }

I've just found solution. I had error in this line:

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

This topic helps me a lot.

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

Sources of predefined layouts are here .

So, there is two options how to fix error and make ListView work without ListActivity in Mono Android.
First:
Just copy/paste layout of standard resource named 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"
/>

Second (I'm sure it's a best way):
Just use standard resource. In Mono Android it named Android.Resource.Layout.SimpleListItem1
So my updated version of OnCreate which works perfect:

    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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM