2012-10-05 133 views
0

我想從foreach循環中添加項目。我的問題是,無論我添加多少,我只能看到一個項目。我在下面做了一個示例代碼,其中有我的問題。從我的佈局開始。listview只顯示一個項目

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

在這裏,我模擬我的foreach循環添加項目。

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; 

幫助最受讚賞!謝謝。編輯: 我認爲scrollview是問題。

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

我刪除它,然後它的工作:) 不爲什麼壽明白.. How can I put a ListView into a ScrollView without it collapsing? Mabye這可以幫助其他任何具有相同的概率

+0

你在哪裏設置適配器到列表視圖? –

+0

OperationsListView.Adapter = CalculationAdapter;對? – Serik

回答

0

使用它來設置適配器列表視圖

OperationsListView.setAdapter(CalculationAdapter); 

這將設置值到ListView

,我不河畔Ë

是否ListView OperationsListView = FindViewById<ListView>(Resource.Id.OperationsListView); 是正確

試試這個

OperationsListView = (ListView) findViewById(your id); 
+0

感謝您的回答!但是當我發佈我的問題時,兩個標籤消失了。你的答案在Java中是正確的?我正在用C#開發monodroid。在那種情況下,對不起。 – Serik

+0

確定對不起,我的答案是java –

+0

但你知道嗎?我懷疑錯誤是在佈局中,它是一樣的權利?無論如何,如果您的佈局爲 – Serik

0

我認爲你缺少notifyDataSetChanged();在最後。 CalculationAdapter.notifyDataSetChanged();

+0

After OperationsListView.Adapter = CalculationAdapter; ? 是否這樣? OperationsListView.Adapter = CalculationAdapter; CalculationAdapter.NotifyDataSetChanged(); – Serik

+0

Yap ...這是正確的.. – Prashant