2012-11-19 33 views
9

我有一個列表視圖,它顯示在一個片段上。我在屏幕的底部有一個按鈕,當按下按鈕時,將調用Web服務來重新提供任何其他數據。如果有其他數據,我需要將它添加到列表視圖。我搜索了這個論壇和其他許多網站,試圖找到如何去做,但我沒有成功。任何幫助深表感謝。Android:ListFragment:如何刷新列表

我現在想我是否需要添加片段作爲代詞,而不是在下面的XML佈局上定義它。

我正在使用ListFragment在屏幕上充氣列表視圖。

我有一個屏幕上有兩個片段。對於屏幕的XML如下: -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <fragment 
      android:name="atos.mobilereporting.com.ReportList" 
      android:layout_width="323dp" 
      android:layout_height="match_parent" /> 

     <fragment 
      android:name="atos.mobilereporting.com.ReportDetail" 
      android:layout_width="958dp" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 


    <Button 
     android:id="@+id/getReports" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Refresh Mobile Reports" /> 

</LinearLayout> 

膨脹視圖中的代碼如下: -

public class ReportList extends ListFragment { 

     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      // Get list of reports... 
      repList = ReportDefinitionFactory.buildReportList(3); 

      activity = getActivity(); 

      ListAdapter la = new ArrayAdapter<String>(getActivity(), 
        android.R.layout.simple_list_item_1, 
        ReportDefinitionFactory.getReportDescriptions(repList)); 

      setListAdapter(la); 

     } 

    } 

此代碼示出了具有在其上3行簡單的列表視圖。

在這一點上,我必須停下來,因爲我不知道要從這裏出發。我有代碼將向用於最初構建列表視圖的數組添加額外的行,但我不知道如何在列表視圖上調用刷新。

感謝

馬丁

回答

14

需要調用la.notifyDataSetChanged()以強制列表刷新一旦數據發生了變化。

+0

嗨,它說方法notifyDataSetChanged是未定義類型ListAdapter。 –

+2

只需用'ArrayAdapter la = new ArrayAdapter'替換'ListAdapter la = new ArrayAdapter' – fiddler

+0

事情看起來更好,我現在已經意識到,我沒有填充我的數組correlatty。感謝您對建議的幫助。 –

2

使用ArrayAdapter notifyDataSetChanged()函數。

這可以在ArrayAdapter中添加,如果這是更新數據的位置。否則,將其添加到調用la.add()的同一區域。