From c76e4421b05da6648878faec902bbf55107df2e8 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Fri, 8 Jul 2016 18:44:40 +0800 Subject: [PATCH] fixed crashes --- .../mariotaku/twidere/util/DataStoreUtils.java | 18 +++++++++++------- .../mariotaku/twidere/adapter/DraftsAdapter.kt | 6 +++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/DataStoreUtils.java b/twidere/src/main/java/org/mariotaku/twidere/util/DataStoreUtils.java index 4ea4b256c..c3e0ae81e 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/DataStoreUtils.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/DataStoreUtils.java @@ -705,19 +705,23 @@ public class DataStoreUtils implements Constants { return filterExpression; } - public static int[] getAccountColors(final Context context, final UserKey[] accountKeys) { - if (context == null || accountKeys == null) return new int[0]; + @NonNull + public static int[] getAccountColors(@NonNull final Context context, @NonNull final UserKey[] accountKeys) { final String[] cols = new String[]{Accounts.ACCOUNT_KEY, Accounts.COLOR}; final String where = Expression.inArgs(new Column(Accounts.ACCOUNT_KEY), accountKeys.length).getSQL(); final String[] whereArgs = TwidereArrayUtils.toStringArray(accountKeys); + final int[] colors = new int[accountKeys.length]; final Cursor cur = context.getContentResolver().query(Accounts.CONTENT_URI, cols, where, whereArgs, null); - if (cur == null) return new int[0]; + if (cur == null) return colors; try { - final int[] colors = new int[cur.getCount()]; - for (int i = 0, j = cur.getCount(); i < j; i++) { - cur.moveToPosition(i); - colors[ArrayUtils.indexOf(accountKeys, cur.getLong(0))] = cur.getInt(1); + cur.moveToFirst(); + while (!cur.isAfterLast()) { + final int idx = ArrayUtils.indexOf(accountKeys, UserKey.valueOf(cur.getString(0))); + if (idx >= 0) { + colors[idx] = cur.getInt(1); + } + cur.moveToNext(); } return colors; } finally { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DraftsAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DraftsAdapter.kt index b1a9c660b..3147b263e 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DraftsAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DraftsAdapter.kt @@ -77,7 +77,11 @@ class DraftsAdapter(context: Context) : SimpleCursorAdapter(context, R.layout.li holder.media_preview_container.visibility = View.GONE } } - holder.content.drawEnd(*DataStoreUtils.getAccountColors(context, accountKeys)) + if (accountKeys != null) { + holder.content.drawEnd(*DataStoreUtils.getAccountColors(context, accountKeys)) + } else { + holder.content.drawEnd() + } holder.setTextSize(mTextSize) val emptyContent = TextUtils.isEmpty(text) if (emptyContent) {