From 63d2c5700f9451722a244ac9969de766a60be6e6 Mon Sep 17 00:00:00 2001 From: David Development Date: Fri, 4 Jul 2014 11:06:39 +0200 Subject: Fix app crash (modify data without calling notifyDataSetChanged) --- .../SubscriptionExpandableListAdapter.java | 124 ++++++++++++++------- .../owncloudnewsreader/NewsReaderListFragment.java | 26 +---- 2 files changed, 87 insertions(+), 63 deletions(-) (limited to 'News-Android-App/src/main') diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/ListView/SubscriptionExpandableListAdapter.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/ListView/SubscriptionExpandableListAdapter.java index 76017382..1e1040f3 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/ListView/SubscriptionExpandableListAdapter.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/ListView/SubscriptionExpandableListAdapter.java @@ -51,6 +51,7 @@ import de.luhmer.owncloudnewsreader.R; import de.luhmer.owncloudnewsreader.SettingsActivity; import de.luhmer.owncloudnewsreader.database.DatabaseConnection; import de.luhmer.owncloudnewsreader.helper.FavIconHandler; +import de.luhmer.owncloudnewsreader.helper.FileUtils; import de.luhmer.owncloudnewsreader.helper.FontHelper; import de.luhmer.owncloudnewsreader.helper.ImageHandler; import de.luhmer.owncloudnewsreader.helper.ThemeChooser; @@ -105,7 +106,7 @@ public class SubscriptionExpandableListAdapter extends BaseExpandableListAdapter LayoutInflater inflater; private final String favIconPath; boolean mIsTwoPane; - private static boolean isTwoPane(Context context) { + public static boolean isTwoPane(Context context) { return context.getResources().getBoolean(R.bool.two_pane); //return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE; } @@ -116,7 +117,7 @@ public class SubscriptionExpandableListAdapter extends BaseExpandableListAdapter //Picasso.with(mContext).setDebugging(true); - this.favIconPath = ImageHandler.getPathFavIcons(mContext); + this.favIconPath = FileUtils.getPathFavIcons(mContext); this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.mContext = mContext; this.dbConn = dbConn; @@ -139,18 +140,18 @@ public class SubscriptionExpandableListAdapter extends BaseExpandableListAdapter this.listView = listView; } - private void AddEverythingInCursorFolderToSubscriptions(Cursor itemsCursor) + private void AddEverythingInCursorFolderToSubscriptions(Cursor itemsCursor, ArrayList dest) { while (itemsCursor.moveToNext()) { String header = itemsCursor.getString(itemsCursor.getColumnIndex(DatabaseConnection.FOLDER_LABEL)); //String id_folder = itemsCursor.getString(itemsCursor.getColumnIndex(DatabaseConnection.FOLDER_LABEL_ID)); long id = itemsCursor.getLong(0); - mCategoriesArrayList.add(new FolderSubscribtionItem(header, null, id, null)); + dest.add(new FolderSubscribtionItem(header, null, id, null)); } itemsCursor.close(); } - private void AddEverythingInCursorFeedsToSubscriptions(Cursor itemsCursor) + private void AddEverythingInCursorFeedsToSubscriptions(Cursor itemsCursor, ArrayList dest) { while (itemsCursor.moveToNext()) { String header = itemsCursor.getString(itemsCursor.getColumnIndex(DatabaseConnection.SUBSCRIPTION_HEADERTEXT)); @@ -158,7 +159,7 @@ public class SubscriptionExpandableListAdapter extends BaseExpandableListAdapter long id = itemsCursor.getLong(0); String subscriptionId = itemsCursor.getString(itemsCursor.getColumnIndex(DatabaseConnection.SUBSCRIPTION_ID)); String favIconUrl = itemsCursor.getString(itemsCursor.getColumnIndex(DatabaseConnection.SUBSCRIPTION_FAVICON_URL)); - mCategoriesArrayList.add(new ConcreteFeedItem(header, ITEMS_WITHOUT_FOLDER.getValueString(), subscriptionId, favIconUrl, id)); + dest.add(new ConcreteFeedItem(header, ITEMS_WITHOUT_FOLDER.getValueString(), subscriptionId, favIconUrl, id)); } itemsCursor.close(); } @@ -495,28 +496,31 @@ public class SubscriptionExpandableListAdapter extends BaseExpandableListAdapter } + ArrayList mCategoriesArrayListAsync; + SparseArray> mItemsArrayListAsync; public void ReloadAdapter() { SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext); showOnlyUnread = mPrefs.getBoolean(SettingsActivity.CB_SHOWONLYUNREAD_STRING, false); - mCategoriesArrayList = new ArrayList(); - mCategoriesArrayList.add(new FolderSubscribtionItem(mContext.getString(R.string.allUnreadFeeds), null, ALL_UNREAD_ITEMS.getValue(), null)); - mCategoriesArrayList.add(new FolderSubscribtionItem(mContext.getString(R.string.starredFeeds), null, ALL_STARRED_ITEMS.getValue(), null)); + mCategoriesArrayListAsync = new ArrayList(); + mCategoriesArrayListAsync.add(new FolderSubscribtionItem(mContext.getString(R.string.allUnreadFeeds), null, ALL_UNREAD_ITEMS.getValue(), null)); + mCategoriesArrayListAsync.add(new FolderSubscribtionItem(mContext.getString(R.string.starredFeeds), null, ALL_STARRED_ITEMS.getValue(), null)); //mCategoriesArrayList.add(new FolderSubscribtionItem(mContext.getString(R.string.starredFeeds), -11, null, null)); - AddEverythingInCursorFolderToSubscriptions(dbConn.getAllTopSubscriptions(showOnlyUnread)); - AddEverythingInCursorFeedsToSubscriptions(dbConn.getAllTopSubscriptionsWithoutFolder(showOnlyUnread)); + AddEverythingInCursorFolderToSubscriptions(dbConn.getAllTopSubscriptions(showOnlyUnread), mCategoriesArrayListAsync); + AddEverythingInCursorFeedsToSubscriptions(dbConn.getAllTopSubscriptionsWithoutFolder(showOnlyUnread), mCategoriesArrayListAsync); //AddEverythingInCursorToSubscriptions(dbConn.getAllTopSubscriptionsWithUnreadFeeds()); - mItemsArrayList = new SparseArray>(); + mItemsArrayListAsync = new SparseArray>(); - for(int groupPosition = 0; groupPosition < mCategoriesArrayList.size(); groupPosition++) { - int parent_id = (int)getGroupId(groupPosition); + for(int groupPosition = 0; groupPosition < mCategoriesArrayListAsync.size(); groupPosition++) { + //int parent_id = (int)getGroupId(groupPosition); + int parent_id = (int) mCategoriesArrayListAsync.get(groupPosition).id_database; String parentId = String.valueOf(parent_id); - mItemsArrayList.append(parent_id, new SparseArray()); + mItemsArrayListAsync.append(parent_id, new SparseArray()); Cursor itemsCursor = dbConn.getAllSubscriptionForFolder(parentId, showOnlyUnread); itemsCursor.requery(); int childPosTemp = 0; @@ -527,7 +531,7 @@ public class SubscriptionExpandableListAdapter extends BaseExpandableListAdapter String subscription_id = itemsCursor.getString(itemsCursor.getColumnIndex(DatabaseConnection.SUBSCRIPTION_ID)); String urlFavicon = itemsCursor.getString(itemsCursor.getColumnIndex(DatabaseConnection.SUBSCRIPTION_FAVICON_URL)); ConcreteFeedItem newItem = new ConcreteFeedItem(name, parentId, subscription_id, urlFavicon, id_database); - mItemsArrayList.get(parent_id).put(childPosTemp, newItem); + mItemsArrayListAsync.get(parent_id).put(childPosTemp, newItem); childPosTemp++; } while (itemsCursor.moveToNext()); itemsCursor.close(); @@ -552,50 +556,94 @@ public class SubscriptionExpandableListAdapter extends BaseExpandableListAdapter private class NotifyDataSetChangedAsyncTask extends AsyncTask { - // Block children layout for now - BlockingExpandableListView bView; SparseArray unreadCountFoldersTemp; SparseArray unreadCountFeedsTemp; SparseArray urlsToFavIconsTemp; + @Override + protected Void doInBackground(Void... voids) { + unreadCountFoldersTemp = dbConn.getUnreadItemCountForFolder(mContext); + unreadCountFeedsTemp = dbConn.getUnreadItemCountForFeed(); + urlsToFavIconsTemp = dbConn.getUrlsToFavIcons(); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + notifyCountDataSetChanged(unreadCountFoldersTemp, unreadCountFeedsTemp, urlsToFavIconsTemp); + super.onPostExecute(aVoid); + } + } + + public void ReloadAdapterAsync(View progressBar) { + new ReloadAdapterAsyncTask(progressBar).execute((Void) null); + } + + private class ReloadAdapterAsyncTask extends AsyncTask { + + View progressBar; + + public ReloadAdapterAsyncTask(View progressBar) { + this.progressBar = progressBar; + } @Override protected void onPreExecute() { - bView = (BlockingExpandableListView) listView; + progressBar.setVisibility(View.VISIBLE); super.onPreExecute(); } @Override protected Void doInBackground(Void... voids) { - unreadCountFoldersTemp = dbConn.getUnreadItemCountForFolder(mContext); - unreadCountFeedsTemp = dbConn.getUnreadItemCountForFeed(); - urlsToFavIconsTemp = dbConn.getUrlsToFavIcons(); + ReloadAdapter(); return null; } @Override protected void onPostExecute(Void aVoid) { - unreadCountFolders = unreadCountFoldersTemp; - unreadCountFeeds = unreadCountFeedsTemp; - urlsToFavIcons = urlsToFavIconsTemp; + notifyReloadAdapterDataChanged(); - int firstVisPos = bView.getFirstVisiblePosition(); - View firstVisView = bView.getChildAt(0); - int top = firstVisView != null ? firstVisView.getTop() : 0; + progressBar.setVisibility(View.GONE); - // Number of items added before the first visible item - int itemsAddedBeforeFirstVisible = 0; + super.onPostExecute(aVoid); + } + } - bView.setBlockLayoutChildren(true); - notifyDataSetChanged(); - bView.setBlockLayoutChildren(false); - // Call setSelectionFromTop to change the ListView position - if(bView.getCount() >= firstVisPos + itemsAddedBeforeFirstVisible) - bView.setSelectionFromTop(firstVisPos + itemsAddedBeforeFirstVisible, top); + public void notifyReloadAdapterDataChanged() + { + mCategoriesArrayList = mCategoriesArrayListAsync; + mCategoriesArrayListAsync = null; - super.onPostExecute(aVoid); - } + mItemsArrayList = mItemsArrayListAsync; + mItemsArrayListAsync = null; + + notifyDataSetChanged(); + + NotifyDataSetChangedAsync(); + } + + public void notifyCountDataSetChanged(SparseArray unreadCountFolders, SparseArray unreadCountFeeds, SparseArray urlsToFavIcons) { + this.unreadCountFolders = unreadCountFolders; + this.unreadCountFeeds = unreadCountFeeds; + this.urlsToFavIcons = urlsToFavIcons; + + BlockingExpandableListView bView = (BlockingExpandableListView) listView; + + int firstVisPos = bView.getFirstVisiblePosition(); + View firstVisView = bView.getChildAt(0); + int top = firstVisView != null ? firstVisView.getTop() : 0; + + // Number of items added before the first visible item + int itemsAddedBeforeFirstVisible = 0; + + bView.setBlockLayoutChildren(true); + notifyDataSetChanged(); + bView.setBlockLayoutChildren(false); + + // Call setSelectionFromTop to change the ListView position + if(bView.getCount() >= firstVisPos + itemsAddedBeforeFirstVisible) + bView.setSelectionFromTop(firstVisPos + itemsAddedBeforeFirstVisible, top); } diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListFragment.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListFragment.java index 95e8ae4f..6d9606ce 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListFragment.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListFragment.java @@ -30,7 +30,6 @@ import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.content.SharedPreferences; -import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; @@ -165,33 +164,10 @@ public class NewsReaderListFragment extends SherlockFragment implements OnCreate } public void ReloadAdapter() { - new ReloadAdapterAsync().execute((Void) null); + lvAdapter.ReloadAdapterAsync(progressBar); } - private class ReloadAdapterAsync extends AsyncTask { - @Override - protected void onPreExecute() { - progressBar.setVisibility(View.VISIBLE); - super.onPreExecute(); - } - - @Override - protected Void doInBackground(Void... voids) { - lvAdapter.ReloadAdapter(); - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - //lvAdapter.notifyDataSetChanged(); - lvAdapter.NotifyDataSetChangedAsync(); - - progressBar.setVisibility(View.GONE); - - super.onPostExecute(aVoid); - } - } -- cgit v1.2.3