Android: ListFragment: How to refresh list

Martin Shinks picture Martin Shinks · Nov 19, 2012 · Viewed 17.5k times · Source

I have a list view which is shown on a fragment. I have a button at the bottom of the screen in which when pressed, will call a webservice to reteive any additional data. If there is additional data, I need to add it to the list view. I have searched this forum and so many other web sites to try and find how to do it, but I have had no success. Any help is much appreciated.

I am now thinking do I need to add the fragment dyncamically instead of having it defined on the following XML layout.

I am using a ListFragment to inflate a list view on the screen.

I have a screen with two fragments on it. The XML for the screen is below: -

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

The code to inflate the view is below: -

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

        }

    }

This code shows a simple list view with 3 rows on it.

It is at this point I must stop as I do not know were to go from here. I have the code which will add an additional row to the array that is used to initially build the list view, but I do not know how I can invoke a refresh on the list view.

Thanks

Martin

Answer

sdabet picture sdabet · Nov 19, 2012

You need to call la.notifyDataSetChanged() to force refresh of the list once the data has changed.