ListView only adds one element

0

I am declaring and adding data to a listview in Xamarin Android, but even though I add 4 data it only shows me 1, I declare it this way:

        list = new List<String>();
        list.Add("Dato 1");
        list.Add("Dato 2");
        list.Add("Dato 3");
        list.Add("Dato 4");

        lista = FindViewById<ListView>(Resource.Id.lista);

        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, list);
        lista.SetAdapter(adapter);

and here I declare my ListView in the layout:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:layout_width="800px"
            android:layout_marginBottom="60px">
            <ListView
                android:minWidth="25px"
                android:minHeight="25px"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/lista"
                android:textColor="#F0EEEE" />
        </LinearLayout>

Any idea why it shows me only one element in the listView?

    
asked by EriK 01.02.2018 в 19:46
source

1 answer

0

I was able to do the following.

As having the ListView within the ScrollView , the ListView adjusted the size of your first item, create CustomListView.

CustomListView.cs

public class CustomListView : ListView
{
    public CustomListView(Context context, IAttributeSet attrs) : base(context, attrs)
    {

    }

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int expandSpec = MeasureSpec.MakeMeasureSpec(int.MaxValue >> 2, MeasureSpecMode.AtMost);
        base.OnMeasure(widthMeasureSpec, expandSpec);
    }
}

Main.axml

change the namespace to your project.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <ScrollView              
    android:layout_width="match_parent"
     android:layout_height="wrap_content">
    <Namespace.CustomListView
              android:minWidth="25px"
              android:minHeight="25px"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/lista"
              android:textColor="#F0EEEE" />
  </ScrollView>
</LinearLayout>

and declare and add the data to ListView as you did. Just edit the last line.

    var list = new List<string>();
        list.Add("Dato 1");
        list.Add("Dato 2");
        list.Add("Dato 3");
        list.Add("Dato 4");

        var lista = FindViewById<ListView>(Resource.Id.lista);

        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, list);
        lista.Adapter = adapter;

I hope it works. In this case all the items of ListView

will be displayed     
answered by 02.03.2018 в 19:15