Ich habe eine einfache Sonne Sonnenaufgang/Sonne Set-App, die dem Benutzer die Zeiten gegeben einen ausgewählten Ort gegeben.Android TextView nicht zunächst zu aktualisieren, funktioniert bei weiteren Anrufen
Wenn ich führen Sie den Code in der updateTime()
Methode die Zeit, um die Log ausgegeben (SUNRISE Unformatted \ SUNRISE Formatiert) korrekt ist, aber die TextView
Anzeige nicht aktualisiert, bis das Datum auf etwas anderes geändert wurde (und ist korrekt, wenn das Datum wird zurück geändert).
Gibt es irgendetwas, das dazu führen würde, dass das Protokoll die korrekte Ausgabe anzeigt, aber die TextView
sofort danach nicht?
Voll 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-Datei:
<?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>
vielleicht müssen Sie Ihr Fragment aktualisieren. Schauen Sie sich das http://stackoverflow.com/questions/20702333/refresh-fragment-at-reload an. – grant