diff options
author | Johan Olsson <johan@jmvo.se> | 2017-09-10 11:29:39 +0200 |
---|---|---|
committer | Johan Olsson <johan@jmvo.se> | 2017-09-10 11:43:23 +0200 |
commit | 5f256164b6445d33116a6bac85617642f3da82ee (patch) | |
tree | d952f7e581365f538182f3cd24caf0954d42238b | |
parent | 5f4c6b073c72f098d5dcf04f84099657dc2c3950 (diff) | |
download | android_packages_apps_Jelly-5f256164b6445d33116a6bac85617642f3da82ee.tar.gz android_packages_apps_Jelly-5f256164b6445d33116a6bac85617642f3da82ee.tar.bz2 android_packages_apps_Jelly-5f256164b6445d33116a6bac85617642f3da82ee.zip |
Jelly: Reuse DateFormat instance in HistoryAdapter.
Previously a DateFormat instance was created for each list item in the RecyclerView.
This solution creates only one instance per HistoryAdapter and reuses it whenever a
list item is being binded.
Change-Id: I676c0b54a82bd741b78f2d3d2d6a70f0bcffc2f4
-rw-r--r-- | app/src/main/java/org/lineageos/jelly/history/HistoryAdapter.java | 11 | ||||
-rw-r--r-- | app/src/main/java/org/lineageos/jelly/history/HistoryHolder.java | 10 |
2 files changed, 13 insertions, 8 deletions
diff --git a/app/src/main/java/org/lineageos/jelly/history/HistoryAdapter.java b/app/src/main/java/org/lineageos/jelly/history/HistoryAdapter.java index 137ddb2..31cc6bb 100644 --- a/app/src/main/java/org/lineageos/jelly/history/HistoryAdapter.java +++ b/app/src/main/java/org/lineageos/jelly/history/HistoryAdapter.java @@ -23,8 +23,14 @@ import android.view.ViewGroup; import org.lineageos.jelly.R; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + class HistoryAdapter extends RecyclerView.Adapter<HistoryHolder> { private final Context mContext; + private final DateFormat mHistoryDateFormat; private Cursor mCursor; private int mIdColumnIndex; @@ -34,6 +40,8 @@ class HistoryAdapter extends RecyclerView.Adapter<HistoryHolder> { HistoryAdapter(Context context) { mContext = context; + mHistoryDateFormat = new SimpleDateFormat(context.getString(R.string.history_date_format), + Locale.getDefault()); setHasStableIds(true); } @@ -67,9 +75,10 @@ class HistoryAdapter extends RecyclerView.Adapter<HistoryHolder> { } long id = mCursor.getLong(mIdColumnIndex); long timestamp = mCursor.getLong(mTimestampColumnIndex); + String summary = mHistoryDateFormat.format(new Date(timestamp)); String title = mCursor.getString(mTitleColumnIndex); String url = mCursor.getString(mUrlColumnIndex); - holder.bind(mContext, id, title, url, timestamp); + holder.bind(mContext, id, title, url, summary, timestamp); } @Override diff --git a/app/src/main/java/org/lineageos/jelly/history/HistoryHolder.java b/app/src/main/java/org/lineageos/jelly/history/HistoryHolder.java index 44bc223..ccbad46 100644 --- a/app/src/main/java/org/lineageos/jelly/history/HistoryHolder.java +++ b/app/src/main/java/org/lineageos/jelly/history/HistoryHolder.java @@ -30,10 +30,6 @@ import org.lineageos.jelly.MainActivity; import org.lineageos.jelly.R; import org.lineageos.jelly.utils.UiUtils; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; - class HistoryHolder extends RecyclerView.ViewHolder { private final LinearLayout mRootLayout; @@ -47,13 +43,13 @@ class HistoryHolder extends RecyclerView.ViewHolder { mSummary = (TextView) view.findViewById(R.id.row_history_summary); } - void bind(final Context context, final long id, String title, String url, long timestamp) { + void bind(final Context context, final long id, String title, String url, String summary, + long timestamp) { if (title == null || title.isEmpty()) { title = url.split("/")[2]; } mTitle.setText(title); - mSummary.setText(new SimpleDateFormat(context.getString(R.string.history_date_format), - Locale.getDefault()).format(new Date(timestamp))); + mSummary.setText(summary); mRootLayout.setOnClickListener(v -> { Intent intent = new Intent(context, MainActivity.class); |