0

I have a list of orders in one fragment. In a second fragment, I display the detail of the order, and I use a third fragment to display the buttons that change the status of the order.

In the list, each order is displayed with a background color that indicates its status, for example green for a completed delivery.

When in landscape mode, both the detail and list are shown. In portrait mode I use two separate activities.

This all works fine, up until I change the status of an order. I can't find a way to get the list to update.

As I understand it, what needs to happen is the adapter needs to have its notifyDateChanged() method called. I've tried calling it directly from the method that processes the button click, I've tried an asynctask, and I've tried a handler. My debug methods show that the call is happening, but the list doesn't get updated.

It's possible I'm doing something completely bone-headed, but I've double and triple checked things. I suspect there is some key element I don't understand. I hope someone else does and will tell me what I'm missing.

I had some code posted, but it was clearly wrong. Not sure what code to post, since I think this is more a conceptual than coding issue.

Rben
  • 489
  • 1
  • 7
  • 18
  • this won't work if `adapter` is not a variable on UI thread. – StoneBird Apr 25 '13 at 01:41
  • adapter is an OrderListFragment which is extended from a SimpleCursorAdapter. adapter is used to link my data from a cursor to a ListView. – Rben Apr 25 '13 at 17:41
  • I've tried both handlers and asynctasks and neither seems to work. One part of the problem might be that the button that is clicked is in a different fragment from the one where the list is displayed. – Rben Apr 25 '13 at 17:42
  • store a reference in this fragment of that adapter, or you need to make your adapter `public static` and call `YourActivity.adapter` – StoneBird Apr 25 '13 at 17:45
  • I'll give that a try, StoneBird – Rben Apr 25 '13 at 18:06

2 Answers2

0

If you want to update a view, try to use :

  1. Thread + Handler

  2. AsyncTask

bullda
  • 1
  • 1
  • Using AsyncTask was definitely part of the solution. Thread + Handler was a bit too complicated for this kind of thing. – Rben Apr 28 '13 at 17:28
0

What I discovered, through the helpful comments of other posters, is that my problem wasn't that the adapter wasn't getting the notifyDataChanged() call on the correct thread. I put in debug code that proved that. The problem was that the background of the listview item was based on a change in the underlying data itself, so the real answer was to get the Adapter to refresh the Cursor. I made some changes. I modified the AsyncTask I was using to notify the adapter. Now the AsyncTask gets a new cursor and calls adapter.changeCursor(cursor) with the result. The AsyncTask is called from the MainActivity (which hosts both the list and detail fragments) when the status is changed. It's also called in the onResume() portion of the ListFragment code, so the list will be updated properly when coming back from the detail fragment. Works great.

Rben
  • 489
  • 1
  • 7
  • 18