Chapter 9 Challenge Solution


#1

In the CrimeListFragment class, make the following change to the bind() method:

public void bind(Crime crime) {
mCrime = crime;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(DateFormat.format(“EEEE, MMM dd, yyyy”, mCrime.getDate()));
mSolvedImageView.setVisibility(crime.isSolved() ? View.VISIBLE : View.GONE);
}

This table is helpful in creating the formatting string


#2

I am getting this error, non static method (format) cannot be referenced from static method.


#3

crime.getDate() not mCrime.getDate()


#4

When use “EEEE, MMM dd, yyyy” you hard fixing the order of month name and date: month, then day.
But for many countries it is not true! Many locales supposed to use format “21 Mar 2018” not “Mar 21, 2018”
Also, user can change 12/24 time format independently of system language. For example, I can use English (United States) language for UI, but set 24 time format in Time&Date settings.

My solution:

DateFormat dateDateFormat = android.text.format.DateFormat.getMediumDateFormat(getActivity());
DateFormat timeDateFormat = android.text.format.DateFormat.getTimeFormat(getActivity());
Date date = crime.getDate();
String dateString = String.format("%s, %s %s",
        android.text.format.DateFormat.format("EEEE", date),
        dateDateFormat.format(date),
        timeDateFormat.format(date));
dateTextView.setText(dateString);

#5

I did with the use of Calendar class.

Inside public class CrimeListFragment:

 Calendar myCalendar = Calendar.getInstance();
    String currentDate = DateFormat.getDateInstance(DateFormat.FULL).format(myCalendar.getTime());

//now apply changes to bind() method

public void bind(Crime crime){
.....
 mDateTextView.setText(currentDate);
}

#6

it works great but can you explain how you came up with this code


#7

Wanted to note that the android documentation recommends not sending in your own format, but if you do, it suggests using this method:

getBestDateTimePattern(Locale locale, String skeleton)

The Locale class contains various static locales that can be passed in such as Locale.US or Locale.UK and it will adjust your pattern accordingly. Here’s how I implemented it:

private String formatDate(Date date, Locale locale) {
    String df = DateFormat.getBestDateTimePattern(locale, "EEEE, MMM dd, yyyy");
    return DateFormat.format(df, date).toString();
}

And I call that method in the CrimeHolder bind method like:

mDateTextView.setText(formatDate(crime.getDate(), Locale.US));

#8

I just changed the GETTER in Crime.java to return the converted string, and in the BIND method I removed the .toString() call.

In my view this is the best location for the date conversion, what do you think?

/**
* THIS GETTER HAS BEEN MODIFIED FOR THE CHALLENGE P 204, BY MAKING
THE DATA CONVERSION
* AND RETURNING THE STRING TO CRIME LIST FRAGMENT.JAVA
*/
public String getDate() {
String mDateView = DateFormat.getDateInstance(DateFormat.FULL).format(mDate);
return mDateView;
}

public void bind(Crime crime) {
mCrime = crime;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDate());//CHALLENGE DATA from CRIME.JAVA
mSolvedImageView.setVisibility(crime.isSolved() ? View.VISIBLE : View.GONE);
}//method


#9

My goal was to keep the bind method clean while getting the date format to read as “Thursday, Oct 11, 2018”. I ultimately opted for one line of code:

public void bind(Crime crime) {
        mCrime=crime;
        mTitleTextView.setText(mCrime.getTitle());
        mDateTextView.setText(new SimpleDateFormat("EEEE, MMM d, yyyy",Locale.getDefault()).format(crime.getDate()));
        mSolvedImageView.setVisibility(crime.isSolved()?View.VISIBLE:View.GONE);
    }

For a list of the date meanings/abbreviations (i.e. “EEEE, MMM” etc.) go to [https://developer.android.com/reference/java/time/format/DateTimeFormatter]
If you only use one letter for the day “E” it will give you “Thu” and “EEEE” will spell out the whole day “Thursday”. Play around with it.

This is a link for setting the Locale. I went with Locale.getDefault() to keep it dynamic.
[https://developer.android.com/reference/java/util/Locale]

I am interested in hearing other ideas.