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. What's on earth the SAX Parser changes between 2.1 and 2.2

I have wrote a ContentHandler for SAX parser to retrieve data from and xml
file, but it does't work on 2.1-update1, but works fine on 2.2,
What is on earth is the changes, anyone knows?

the problem I encountered is exactly the same as stated at:

http://code.google.com/p/android/issues/detail?id=11223

----

*Reported by m.de.kwant, Sep 14, 2010*

My application uses a urlconnection to retrieve a soap response. This XML is
run through the default saxparser available in the java/android lib.

In android version 2.1 the XML is not parsed correctly. I fact there seems
to be no parsing at all, while the raw input is available.

In android version 2.2 the input XML is parsed and the return result from my
handlers is correct.

In short. SaxParser on 2.1 does nothing (no result, no error, no parsing),
Saxparser on 2.2 works like it supposed to work.

Are there any work arounds for this problem ?


*Comment 1 by project member e...@google.com, Sep 14, 2010*

you can have a look at the differences between 2.1 and 2.2 yourself.

Status: Declined
Owner: e...@google.com
Labels: Component-Dalvik
Delete comment
Comment 2 by jiangjun.jking, Today (95 minutes ago)

I have the same issue, could you please explain more clear about it?

-- 

2. Looking for help testing an app

Hey everyone,

I don't know if this is kosher, but I wanted to see if anyone would
want to test an app I built.  I don't want to release an app to the
market without getting out all the bugs I can, but I'm a lone
developer trying to build apps on the side in my free time.  I don't
know many people with Droids, so I'm trying to reach out to the
community.

I built an app I call "Next Track on Shake".  Not a good name, (and
I'm up for suggestions for a better name) but I can't think of
anything else to explain its function.  I listen to alot of music,
usually on shuffle, and I hate having to wake the phone up, unlock the
screen, open the music player, and change the track.  The new versions
of Android require PIN or Password when you use a corporate account,
and that adds another step in changing tracks.

I built an app that allows you to shake the phone, whether awake or
not, and it will move to the next track for you.  I also added the
ability to customize the shake; you can set how hard or how fast you
need to shake the phone to change the track.  The default shake is a
good setting for most people, but alot of people jog with the phone on
their arm or hip and would need to adjust the strength of the shake so
as to not accidentally change tracks.  I've also added the ability to
stop/start the service when needed, and I display an ongoing
notification when the service is running.  When the phone is rebooted,
the app checks the preferences, and only starts the app if you had the
service running before reboot.

That's my app in a nutshell. As I said, I'd love it to be as bug-free
as possible when I release it, so I'm looking for some help.  Please
leave comments with any feedback, issues, or suggestions you have.  I
appreciate everyone's help.


Here's the link to the app:  http://www.megaupload.com/?d=E1YXV89X


Thanks,

Mike M

-- 

3. Anybody experiencing a boost in sales since opening up to other countries?

4. How to pause game on pop-ups.

5. Buying numbers vs developing numbers

6. Architectural question

7. Accessing the apk-signing certificate from inside the program