Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: ListFragment: How to refresh list

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

like image 465
Martin Shinks Avatar asked Nov 19 '12 19:11

Martin Shinks


2 Answers

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

like image 184
sdabet Avatar answered Oct 21 '22 16:10

sdabet


Use the ArrayAdapter notifyDataSetChanged() function.

This could be added in the ArrayAdapter, if that is the location where the data is updated. Otherwise, add it to the same area that calls la.add().

like image 2
PearsonArtPhoto Avatar answered Oct 21 '22 16:10

PearsonArtPhoto