1

I want to add items to it from a foreach loop. My problem is that I can only see one item no matter how many I add. I've made an example code below with my problem in it. Starting with my layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:focusableInTouchMode="true">

  <ScrollView android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:focusableInTouchMode="true">

      <TextView
          android:textSize="10pt"
          android:id="@+id/HeaderLbl"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
         />

      <TextView
            android:textSize="7pt"
            android:id="@+id/ErrorLbl"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffff0000"
            />
      <Spinner
          android:id="@+id/OrderSpinner"
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
               >
      </Spinner>

      <TextView
            android:textSize="8pt"
            android:id="@+id/OrderRef"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
           />

      <TextView
            android:textSize="7pt"
            android:id="@+id/OrderDateTime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
           />

      <TextView
            android:textSize="7pt"
            android:id="@+id/OrderCustomer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
           />

      <TextView
            android:textSize="7pt"
            android:id="@+id/OrderInnerComment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
           />

      <Spinner
          android:id="@+id/ArticleSpinner"
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
               >
      </Spinner>

      <ListView
        android:id="@+id/OperationsListView"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
          >
      </ListView>


    </LinearLayout>
  </ScrollView>
</LinearLayout>

And here I simulate my foreach loop adding items to it.

ArrayAdapter CalculationAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1);
    ListView OperationsListView = FindViewById<ListView>(Resource.Id.OperationsListView);

        for (int i = 0; i < 3; i++)
        {
            CalculationAdapter.Add(i.ToString());
        }

        OperationsListView.Adapter = CalculationAdapter;

Help is most appreciated! Thank you.

EDIT: I think the scrollview was the problem.

<ScrollView android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

I removed it and then it worked :) Don't understand why tho.. How can I put a ListView into a ScrollView without it collapsing? Mabye this can help any others with same prob

Community
  • 1
  • 1
Erik
  • 117
  • 3
  • 15

2 Answers2

0

use this to set adapter to the listview

 OperationsListView.setAdapter(CalculationAdapter);

This will set the values to the listview

and i am not sure whether ListView OperationsListView = FindViewById<ListView>(Resource.Id.OperationsListView);

is right

try this

 OperationsListView = (ListView) findViewById(your id );
G_S
  • 7,068
  • 2
  • 21
  • 51
  • Thanks for the answer! But when i posted my question two tags disappeared. Your answer is in java right? I'm working in monodroid with C#. In that case, sorry. – Erik Oct 05 '12 at 08:54
  • But you know what! I suspect the error is in the layout and it is the same right? You might be able to help me anyways if your pro with the layout – Erik Oct 05 '12 at 09:08
  • you might have the listitems in the list. (check them i think they will be visible when scrolled) – G_S Oct 05 '12 at 09:37
  • Can't scroll. But if that would be the problem, how do i remove the scrolling and make it a list showing all items? – Erik Oct 05 '12 at 10:02
0

I think you are missing notifyDataSetChanged(); at the last. CalculationAdapter.notifyDataSetChanged();

Prashant
  • 13
  • 3
  • After OperationsListView.Adapter = CalculationAdapter; ? Like this? OperationsListView.Adapter = CalculationAdapter; CalculationAdapter.NotifyDataSetChanged(); – Erik Oct 05 '12 at 10:05