Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with notifyDataSetChanged() when porting to Kindle Fire

So I'm porting my Android app over to Kindle, and expected some tweaks for compatibility, but this is getting ridiculous.

The first thing I found out was that apparently running an AsyncTask from onCreate() can cause some problems if you are also initializing your layout from onCreate() as well. That's definitely not the case in regular Android, but the end result I was getting on Kindle was parts of my layout not being displayed at all. Moving the AsyncTask to onStart solved the problem, but this was a bit surprising. I still have a problem invovling this AsyncTask though.

The purpose of this AsyncTask is to load data from online and display the result in the activity's layout. Pretty straightforward, at least on regular Android devices. Just load the data in doInBackground, then in onPostExecute apply the data to the UI to build the page.

However it seems that in some cases (not all) the data from the AsyncTask won't display on screen. for instance, some data is displayed in a GridView. I update the GridView adapter, use the adapter's notifyDataSetChanged() method, and what should happen is that the view is changed to display the data from the adapter. This process on a normal Android device works fine.

On this Kindle however the data doesn't load (again in some cases, not all, which makes this even more frustrating). I've found that if you press the Back button on the Kindle, then drag your finger off of it (so finish() isn't called), the data will sometimes show up! I get the feeling the normal behavior of notifyDataSetChanged() has been altered for Kindle's version of Android, but I'm not sure what else I'm expected to do to show data loaded into an adapter.

Has anyone run into this issue?

Also, I should note in the case of it only happening sometimes, the actual data structure remains constant, but for certain pages it works and others it does not (the app pertains to music, so for example one artist's page loads properly while the other's does not).

I've included the AsyncTask in question, it works perfectly on any Android device so I don't know what use it will be, but here it goes anyway:

private class GetAttendees extends AsyncTask<Void, Void, AttendeesSectionData> {

    @Override
    protected AttendeesSectionData doInBackground(Void... params) {
        JsonHandler jsonHandler = new JsonHandler(getApplicationContext());
        return jsonHandler.getEventAttendeesFromFb(mEvent.getFacebookEventId(), JsonHandler.FACEBOOK_FRIENDS_NO_LIKES);
    }

    @Override
    protected void onPostExecute(AttendeesSectionData data) {
        if(data != null && !data.isEmpty()) {

            if(data.size() > 18) {
                ArrayList<String> toShow = new ArrayList<String>();

                int friendLimit = data.getFriendsAttendingIds().size();
                if(friendLimit > 18)
                    friendLimit = 18;

                for(int i = 0; i < friendLimit; i++)
                    toShow.add(data.getFriendsAttendingIds().get(i).getImageURL());

                int toShowSizeAfterFriends = toShow.size();

                if(toShowSizeAfterFriends < 18) {
                    int othersLimit = data.getNonFriendsAttendingIds().size();
                    if(othersLimit > 18 - toShowSizeAfterFriends)
                        othersLimit = 18 - toShowSizeAfterFriends;

                    for (int i = 0; i < othersLimit; i++)
                        toShow.add(data.getNonFriendsAttendingIds().get(i).getImageURL());

                }

                mFbImagesAdapter.setItems(toShow);
            }
            else
                mFbImagesAdapter.setItems(data.unloadIntoOneArrayList());

            String caption = String.format(AppSettings.ATTENDEE_HEADER_CAPTION, 
                    //fill in the %s with:
                    data.getFriendsAttendingIds().size(), 
                    data.getNonFriendsAttendingIds().size());

            caption = caption.replace("*", "<b>");
            caption = caption.replace("?", "</b>");

            mAttendeesCaption.setText(Html.fromHtml(caption));

            calculateGridSize(data.size());
        }
        else {
            Print.log("no attendees for event " + mEvent.getVenue_FormattedLocation());
            mAttendeesGrid.setVisibility(View.GONE);
            mAttendeesHeaderContainer.setVisibility(View.GONE);
        }

        loadFacebookImages();
    }
}

Thanks!

like image 398
JMRboosties Avatar asked Oct 21 '22 00:10

JMRboosties


1 Answers

I found the solution. Oh man that took a LONG time, but I finally got it.

It's really dumb. Basically, the xml attribute android:animateLayoutChanges will totally break your layouts. I guess Kindle will attempt to animate, but fail, so the end result is nothing happening. Removing all instances of android:animateLayoutChanges from my app fixed the problem.

I hope someone else who has this issue in the future can save a ton of time by stumbling across this answer.

like image 103
JMRboosties Avatar answered Nov 17 '22 23:11

JMRboosties