Use data binding for proper formatting of one date field.
Test: build and run the app
Change-Id: Ie1cd7cc47de0f330152ee144e6bcdba3f33cb8b6
diff --git a/samples-flatfoot/GithubBrowser/app/src/main/java/com/android/sample/githubbrowser/Utils.java b/samples-flatfoot/GithubBrowser/app/src/main/java/com/android/sample/githubbrowser/Utils.java
index 254a9e7..ce3ea2f 100644
--- a/samples-flatfoot/GithubBrowser/app/src/main/java/com/android/sample/githubbrowser/Utils.java
+++ b/samples-flatfoot/GithubBrowser/app/src/main/java/com/android/sample/githubbrowser/Utils.java
@@ -36,6 +36,9 @@
* Helper methods.
*/
public class Utils {
+ private static SimpleDateFormat sJsonDateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",
+ Locale.ENGLISH);
+
private Utils() {
// This is here to prevent object initialization
}
@@ -57,12 +60,9 @@
if (TextUtils.isEmpty(jsonDate)) {
return;
}
- final SimpleDateFormat responseDateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",
- Locale.ENGLISH);
try {
- textView.setText(
- SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT).format(
- responseDateParser.parse(jsonDate)));
+ textView.setText(SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT).format(
+ sJsonDateParser.parse(jsonDate)));
} catch (ParseException pe) {
// WTF
}
@@ -74,9 +74,11 @@
@BindingAdapter({"stringRes", "jsonDate"})
public static void formatDateWithString(TextView textView, @StringRes int stringRes,
String jsonDate) {
+ if (TextUtils.isEmpty(jsonDate)) {
+ return;
+ }
try {
- Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",
- Locale.ENGLISH).parse(jsonDate);
+ Date date = sJsonDateParser.parse(jsonDate);
String formattedDate = SimpleDateFormat.getDateInstance(
SimpleDateFormat.SHORT).format(date);
textView.setText(textView.getResources().getString(stringRes,
diff --git a/samples-flatfoot/GithubBrowser/app/src/main/res/layout/fragment_repo_list.xml b/samples-flatfoot/GithubBrowser/app/src/main/res/layout/fragment_repo_list.xml
index 8e4df45..a4083c7 100644
--- a/samples-flatfoot/GithubBrowser/app/src/main/res/layout/fragment_repo_list.xml
+++ b/samples-flatfoot/GithubBrowser/app/src/main/res/layout/fragment_repo_list.xml
@@ -6,5 +6,5 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
- tools:context="com.android.sample.githubbrowser.MainActivityFragment"
+ tools:context="com.android.sample.githubbrowser.RepositoryListFragment"
tools:showIn="@layout/activity_main"/>
diff --git a/samples-flatfoot/GithubBrowser/app/src/main/res/layout/fragment_user_details.xml b/samples-flatfoot/GithubBrowser/app/src/main/res/layout/fragment_user_details.xml
index aea0080..e06dd5c 100644
--- a/samples-flatfoot/GithubBrowser/app/src/main/res/layout/fragment_user_details.xml
+++ b/samples-flatfoot/GithubBrowser/app/src/main/res/layout/fragment_user_details.xml
@@ -7,10 +7,11 @@
<import type="java.lang.Integer"/>
<import type="android.text.TextUtils"/>
<import type="android.view.View"/>
+ <import type="com.android.sample.githubbrowser.R"/>
<import type="com.android.sample.githubbrowser.Utils"/>
<variable name="userPartial" type="com.android.sample.githubbrowser.ContributorData"/>
<variable name="user" type="com.android.sample.githubbrowser.PersonData"/>
- <variable name="fragment" type="android.support.v4.app.Fragment"/>
+ <variable name="fragment" type="com.android.support.lifecycle.LifecycleFragment"/>
</data>
<LinearLayout
@@ -74,7 +75,8 @@
android:textColor="#444"
android:textSize="14sp"
android:visibility="@{((user == null) || TextUtils.isEmpty(user.created_at)) ? View.INVISIBLE : View.VISIBLE}"
- app:jsonDate="@{user.created_at}"/>
+ app:jsonDate="@{user.created_at}"
+ app:stringRes="@{R.string.joined}"/>
</RelativeLayout>