0

I've got a FragmentActivity in which I fire an AsyncTask that gathers a list of data. This activity has a ListFragment and I want to update the listview as the data keeps coming.

I have another fragment that should also get the data, so I implemented some quick version of observer pattern, so that they both get notified when an element has been added to the list:

private void addElement(MyListElement item) {
    myList.add(item);
        Collections.sort(myList, Collections.reverseOrder());   
        notifyObservers();
}
private void notifyObservers() {
    for(IListObserver dObserver: observers){
          dObserver.updateList(myList);
    }
}

But my list is never updated (or the other fragment, for the matter).

Here is the ListFragment code:

public class MyListFragment extends SherlockListFragment implements IListObserver{

    private TextView first;
    private Context mContext;
    private List<MyListElement> myList;
    private MyListAdapter myListAdapter;

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

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.mContext = getActivity().getApplicationContext();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_listview, container, false);



            myList = new ArrayList<SizeDirectory>(0);
        myListAdapter = new MyListAdapter(getActivity(), myList);
        setListAdapter(myListAdapter);
        myListAdapter.notifyDataSetChanged();


        return view;
    }





    @Override
    public void updateList(List<MyListElement> myList) {
        this.myList = myList;
        this.myListAdapter.notifyDataSetChanged();
        getListView().requestLayout();
    }
}

And this is the AsyncTask:

class ListLoader extends AsyncTask<String, MyListElement, Boolean> {

    private String LOG_TAG = ListLoader .class.getSimpleName();

    /**
     * Maybe show loading indicator
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }


    protected Boolean doInBackground(String... args) {

                     MyListElement item = getListItem(...)
                     publishProgress(item );

        return true;
    }

    /**
     * After completing background task Dismiss the progress dialog
     **/
    protected void onPostExecute(Boolean result) {
        if (result) {
            //Everything was ok
        }
    }

    @Override
    protected void onProgressUpdate(MyListElement... values) {
        super.onProgressUpdate(values);
        addElement(values[0]);
    }

}

The funny thing is, if I put this AsyncTask in my ListFragment, and call updateList from there, then I do get the list updated as the asynctask progresses.

I though maybe this was due to the activity Lifecycle calling the onCreateView of my Fragment AFTER creating activity (and thus resetting the list) but I checked with debugger and it's not the case. I am also assuming this is not related to not being able to update UI from another thread since I do it from onProgressUpdate plus I would've gotten an Exception (or wouldn't I?).

EDIT: layout_listview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/firstTextView"
            android:layout_width="fill_parent"
            android:layout_height="129dp"
            android:scrollbars="vertical"
            android:text="@string/hello_world" />

        <Button
            android:id="@+itd/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Button" />

    </RelativeLayout>

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

</LinearLayout>
M Rajoy
  • 4,028
  • 14
  • 54
  • 111
  • post contents of R.layout.layout_listview; FYI since you are using a ListFragment, the fragment already has a listview and a textView, if you want a custom layout the listView needs to have a specific ID as mentioned in one of my previous posts: http://stackoverflow.com/questions/15113969/onclicklistner-not-working-in-fragment-listview/15223134#15223134 – Abhishek Nandi Mar 07 '13 at 13:56
  • post updated with layout – M Rajoy Mar 07 '13 at 13:57

1 Answers1

0

After the updateList() call, your list adapter is still holding a reference to the original value of myList . Simply setting MyListFragment.myList won't update the adapter. You need a way of setting MyListAdapter's copy of the list. If this is one of the stock Android adapters you may need to create a new adapter for the new list and set it on the list fragment.

Martin Stone
  • 12,682
  • 2
  • 39
  • 53