notifyDataSetChanged() not updating listview after orientation change

by Bara » Tue, 22 Jun 2010 04:28:09 GMT


Sponsored Links
 Hello all,

I have a semi-complicated problem and hoping that someone here will be
able to help me.

On a click event I create a thread and start a long-running operation
based on this method ( http://jnb.ociweb.com/jnb/jnbJan2009.html ).
After the long-running task is completed, it does a callback to
another method, which does a post to the handler:

@Override
public void contentSearchModelChanged(Model_ContentSearch csm,
ArrayList<Class_Reminder> newRemindersList) {
    remindersList = newRemindersList;
    mHandler.post(mUpdateDisplayRunnable);
}
Which calls a 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 is doing:

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 beautifully when I do this normally. However, if I change
the orientation while the long-running operation is running, it
doesn't work. It does make the callback properly, and the
remindersList does have items in it. But when it gets to this line:

r_adapter.notifyDataSetChanged();
Nothing happens. The odd thing is, if I do another submit and have it
run the whole process again (without changing orientation), it
actually updates the view twice, once for the previous submit and
again for the next. So the view updates once with the results of the
first submit, then again with the results of the second submit a
second later. So the adapater DID get the data, it just isn't
refreshing the view.

I know this has something to do with the orientation change, but I
can't for the life of me figure out why. Can anyone help? Or, can
anyone suggest an alternative method of handling threads with
orientation changes?

Bara

--



notifyDataSetChanged() not updating listview after orientation change

by Streets Of Boston » Tue, 22 Jun 2010 07:27:54 GMT


 This probably happens because your runnable 'mUpdateDisplayRunnable'
has an implicit reference to 'this' activity that calls
'this.updateDisplay()' in its run() method.

When an orientation-change happens the current activity ('this') is
destroyed  a brand-new activity is created. When the handler
('mHandler') finally executes your old 'mUpdateDisplayRunnable' after
the rotation change, the 'mUpdateDisplayRunnable' still has an
implicit reference to the destroyed activity. This will cause the list-
view of the destroyed activity to be updated, not the list-view of the
new activity.

In other words, the instance that is referred to by
'mUpdateDisplayRunnable' should be a static instance that does not
have a reference to 'this' activity. To get hold of the current active
(and not destroyed) activity, use either a static variable or use the
onRetainNonConfigurationInstance/getLastNonConfigurationInstance
methods.





--


Sponsored Links


notifyDataSetChanged() not updating listview after orientation change

by Bara » Tue, 22 Jun 2010 11:30:45 GMT


 ait, what exactly should I be returning with
onRetainNonConfigurationInstance? Wouldn't anything I return with
that be from the original activity, not the new one? So wouldn't that
cause the same problem?

Bara

On Jun 21, 7:27pm, Streets Of Boston <flyingdutc...@gmail.com> wrote:

--



notifyDataSetChanged() not updating listview after orientation change

by Streets Of Boston » Tue, 22 Jun 2010 22:23:33 GMT


 ou return an object that doesn't hold any permanent residence to an
activity, just a temporary one.

private final Runnable mUpdateDisplayRunnable = new Runnable() {
public void run() {
mData.getCurrentActivity().updateDisplay();
}
};

public void onCreate(...) {
...
mData = (MyRetainedData)getLastNonConfigurationInstance();
if (mData == null) {
mData = new MyRetainedData();
...
...
}
mData.setCurrentActivity(this);
...
}


public void onDestroy() {
...
// don't hold on to activity about to be destroyed
mData.setCurrentActivity(null);
..
}

public Object onRetainNonConfigurationInstance() {
return mData;
}

The trick here is that the field mData in the destroyed Activity and
the newly created Activity point to the exact same instance of
MyRetainedData. This instance almost acts like a static/global
variable

And if you would be using AsyncTasks in your app, you can put
references to those in the MyRetainedData to hold on to them when
changing configurations. Or you could stuff other stateful data in
there that needs to survive config-changes.

On Jun 21, 11:30pm, Bara <bara.kath...@gmail.com> wrote:

--



notifyDataSetChanged() not updating listview after orientation change

by Bara » Tue, 22 Jun 2010 22:49:26 GMT


 'm sorry, but I'm still not understanding what exactly I should be
holding a reference to. What should mData contain (or rather, what
are the properties in the MyRetainedData class)? I appreciate the
help immensely :)

Bara

On Jun 22, 10:23am, Streets Of Boston <flyingdutc...@gmail.com>
wrote:

--



notifyDataSetChanged() not updating listview after orientation change

by Bara » Wed, 23 Jun 2010 11:33:23 GMT


 After some work, I implemented Streets of Boston's code but I am still
running into the same problem.  I created a new class that holds an
instance of my activity.  I then send this object using
OnRetainNonConfigurationInstance, and use it in my Runnable to call
UpdateDisplay() just like he did.  But still, no luck.  My adapter is
getting the list of reminders, but it is not updating the view.

Any other suggestions that may help?

Bara




--



notifyDataSetChanged() not updating listview after orientation change

by Streets Of Boston » Wed, 23 Jun 2010 11:47:41 GMT


 The call to onRetainConfigurationChange and
getLastNonConfigurationInstance always works, if you have your
activity declared not to handle configuration changes by itself (which
it doesn't by default).




--



notifyDataSetChanged() not updating listview after orientation change

by Bara » Wed, 23 Jun 2010 11:56:15 GMT


 Hmm... could it be my ArrayAdapter class causing the problem?

This is how I call it:

r_adapter = new ReminderAdapater(rData.getCurrentActivity(),
remindersList, thisListView);
thisListView.setAdapter(r_adapter);
r_adapter.notifyDataSetChanged();

And this is ReminderAdapater itself:

    public class ReminderAdapater extends ArrayAdapter<Class_Reminder>
{
        ArrayList<Class_Reminder> items = new
ArrayList<Class_Reminder>();
        private ListView listView;

        public ReminderAdapater(Activity activity, ArrayList<Class_Reminder>
items, ListView listView) {
            super(activity, 0, items);
            this.listView = listView;
            this.items = items;

            Log.i("ReminderAdapater", "Constructor; Size: " + items.size());
        }

        @Override
        public void add(Class_Reminder object)
        {
            Log.i("ReminderAdapater.add", "Item count = " + items.size());
            super.add(object);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
{
            //... some view related stuff here, but it never actually gets to
this point
        }
    }

Do you guys see anything wrong with that?

On Jun 22, 11:47pm, Streets Of Boston <flyingdutc...@gmail.com>




--



notifyDataSetChanged() not updating listview after orientation change

by Streets Of Boston » Thu, 24 Jun 2010 01:45:09 GMT


 thisListView.setAdapter(r_adapter);
r_adapter.notifyDataSetChanged();

If you do 'setAdapter', calling notifyDataSetChanged() is not
necessary, if i'm not mistaken. But calling an extra
notifyDataSetChanged() should hurt.

Override the adapter's getItem and getCount methods and put a break-
point in these methods. See if these get hit and, if they do, see what
values they return.






--



notifyDataSetChanged() not updating listview after orientation change

by Bara » Mon, 28 Jun 2010 09:12:41 GMT


  figured it out!  As it turns out, I have the ListView's visibility
set to GONE by default. I show the ListView in my OnClick event for
my button. When the orientation changes and the Activity gets
destroyed and re-created, the ListView's visibility gets reset to the
default set in the XML, GONE. All I had to do was to set the
visibility to VISIBLE in UpdateDisplay() and it all worked!

Thanks to everyone for the help! It is much appreciated, and I'll be
sure to put all of your usernames in the About/Thank You section of my
app :)

Bara

On Jun 23, 1:44pm, Streets Of Boston <flyingdutc...@gmail.com> wrote:

--



notifyDataSetChanged() not updating listview after orientation change

by Streets Of Boston » Mon, 28 Jun 2010 22:21:54 GMT


 reat you figured that one out! :-)


On Jun 27, 9:12pm, Bara <bara.kath...@gmail.com> wrote:

--



Other Threads

1. Change Message Content before Save.

Hi All,


I need to change Text( SMS) message content before save in in-box. Is it
possible to change text message content in BroadcastReceiver?



Chandana

-- 

2. Tamil Fonts in android 2.1

Hi,

Is the tamil font available in android 2.1 . If not , when the same
will be available....



Thanks in advance ...

-- 

3. HttpURLConnection with local network, socket time up

4. Populating the groups table in contacts2.db

5. help connecting my app to an external Mysql DB

6. GUI Design Patterns of the Twitter Client presented at Google I/O

7. For usage question