Android: notifyDataSetChanged () does not update listview after changing orientation

I have a semi-arid problem and hope someone here can help me.

In the click event, I create a thread and start a lengthy operation based on the this method. After completing the long-term task, it performs a callback to another method, which makes a message to the handler:

@Override public void contentSearchModelChanged(Model_ContentSearch csm, ArrayList<Class_Reminder> newRemindersList) { remindersList = newRemindersList; mHandler.post(mUpdateDisplayRunnable); } 

What causes Runnable:

 // post this to the Handler when the background thread completes private final Runnable mUpdateDisplayRunnable = new Runnable() { public void run() { updateDisplay(); } }; 

Finally, here is what my updateDisplay () method does:

 private void updateDisplay() { if (csModel.getState() != Model_ContentSearch.State.RUNNING) { if(remindersList != null && remindersList.size() > 0){ r_adapter = new ReminderAdapater(Activity_ContentSearch.this, remindersList, thisListView); thisListView.setAdapter(r_adapter); r_adapter.notifyDataSetChanged(); } } } 

This works great when I do it normally. However, if I change the orientation during operation for a long time, it does not work. It actually does the callback, and there are items in the reminder list. But when it comes to this line:

 r_adapter.notifyDataSetChanged(); 

Nothing happens. It’s strange if I give another gift and start the whole process again (without changing the orientation), it actually updates the view twice, once for the previous view and again for the next. Thus, the view is updated once with the results of the first submission, and then again with the results of the second submission the second later. Thus, the DID adapter receives data, it just does not refresh the view.

I know that this has something to do with changing orientation, but I cannot understand why because of my life. Can anyone help? Or can someone suggest an alternative method for handling flows with orientation changes?

Bara

+6
android multithreading listview android-arrayadapter orientation
source share
2 answers

The problem is that when the orientation changes, a new activity starts from the beginning (onCreate). Your long process has a handle for old (not visible) activity. You are updating the old activity correctly, but since it is no longer displayed on the screen, you cannot see it.

This is not an easy problem. There is a library there that can help you. It is called DroidFu. Here is a blog post that (far more accurately than me) describes the root cause of what you see and how the DroidFu library is fighting it: http://brainflush.wordpress.com/2009/11/16/introducing-droid- fu-for-android-betteractivity-betterservice-and-betterasynctask /

Edit: (Adding code to track activity)

In your application class add the following:

 private Activity _activeActivity; public void setActiveActivity(Activity activity) { _activeActivity = activity; } public Activity getActiveActivity() { return _activeActivity; } 

In your activity, add the following:

 @Override public void onResume() { super.onResume(); ((MyApplicationClassName)getApplication()).setActiveActivity(this); } 

Now you can get active activity by calling MyApplicationClassName.getActiveActivity ();

This is not how DroidFu does it. DroidFu sets active activity in onCreate, but I don’t feel it is very reliable.

+6
source share

I had a similar problem, I had a thread-consuming threadEmptyMessage, which took a lot of time, to the handler, which in turn called notifyDataSetChanged in the ListAdapter. It worked fine until I changed orientation.

I solved this by declaring a second handler in the user interface thread and making the first sendEmptyMessage handler to this handler, which in turn called notifyDataSetChanged in the ListAdapter. And the presence of the ListAdapter is declared as static.

I'm a newbie, so I don't know if this is an ugly solution, but it worked for me ...

From the description of Jere.Jones, I would suggest that this works like: a lengthy sendEmptyMessage process for a handle from an old Activity, which in turn sendEmptyMessage to a handle in the new Activity.

+1
source share

All Articles