I have a simple sun rise/sun set app that gives the user the times given a selected location.
When I run the code in the updateTime()
method the time outputted to the Log (SUNRISE Unformatted\SUNRISE Formatted) is correct, but the TextView
display does not update until the date has been changed to something else (and is correct when the date is changed back).
Is there anything that would cause the log to display the correct output but the TextView
immediately after does not?
Full Fragment Code:
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import swindroid.suntime.R;
import swindroid.suntime.calc.AstronomicalCalendar;
import swindroid.suntime.calc.GeoLocation;
public class singleRiseSet extends Fragment {
int year, month, day;
private static GeoLocation currentLocation;
DatePicker dp;
static TextView sunriseTV, sunsetTV;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.single_rise_set, container, false);
dp = (DatePicker) view.findViewById(R.id.datePicker);
sunriseTV = (TextView) view.findViewById(R.id.sunriseTimeTV);
sunsetTV = (TextView) view.findViewById(R.id.sunsetTimeTV);
initialiseUI();
return view;
}
public void initialiseUI() {
Log.d("SINGLERISESET", "INIT");
Calendar cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);
dp.init(year,month,day,dateChangeHandler); // setup initial values and reg. handler
}
public void updateTime(int newYear, int monthOfYear, int dayOfMonth, GeoLocation currentLocation) {
AstronomicalCalendar ac = new AstronomicalCalendar(currentLocation);
Log.d("SUNRISE Current", currentLocation.getLocationName());
if (!(newYear == 0 && monthOfYear == 0 && dayOfMonth == 0)) {
this.year = newYear;
this.month = monthOfYear;
this.day = dayOfMonth;
}
ac.getCalendar().set(year, month, day);
Date srise = ac.getSunrise();
Date sset = ac.getSunset();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Log.d("SUNRISE Unformatted", srise+"");
Log.d("SUNRISE formatted", sdf.format(srise));
sunriseTV.setText(sdf.format(srise));
sunsetTV.setText(sdf.format(sset));
}
DatePicker.OnDateChangedListener dateChangeHandler = new DatePicker.OnDateChangedListener()
{
public void onDateChanged(DatePicker dp, int year, int monthOfYear, int dayOfMonth)
{
updateTime(year, monthOfYear, dayOfMonth, currentLocation);
}
};
}
layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1"
android:gravity="center"
android:padding="5sp">
<TableRow android:id="@+id/TableRow01"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:padding="2sp"
android:gravity="center"
android:textColor="#ffd700"
android:textSize="20sp"
android:text="Sun Rise"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</TextView>
<TextView android:padding="2sp"
android:gravity="center"
android:textColor="#ff8400"
android:textSize="20sp"
android:text="Sun Set"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</TextView>
</TableRow>
<TableRow android:id="@+id/TableRow02"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:padding="2sp"
android:textSize="38sp"
android:textColor="#ffd700"
android:gravity="center"
android:layout_height="wrap_content"
android:id="@+id/sunriseTimeTV"
android:layout_width="wrap_content">
</TextView>
<TextView android:padding="2sp"
android:textSize="38sp"
android:textColor="#ff8400"
android:gravity="center"
android:layout_height="wrap_content"
android:id="@+id/sunsetTimeTV"
android:layout_width="wrap_content">
</TextView>
</TableRow>
</TableLayout>
<ImageView android:id="@+id/ImageView01"
android:layout_width="match_parent"
android:src="@drawable/sunpic"
android:padding="8dp"
android:layout_height="60sp">
</ImageView>
<DatePicker android:id="@+id/datePicker"
android:layout_width="fill_parent"
android:padding="5sp"
android:gravity="center"
android:layout_height="wrap_content"
android:calendarViewShown="false">
</DatePicker>
</LinearLayout>