I have a Unitconverter.java file which converts Kelvin to Celsius and Kelvin to Fahrenheit:
package com.sg.blabla;
import android.content.SharedPreferences;
import java.util.Locale;
public class UnitConvertor {
public static float convertTemperature(float temperature, SharedPreferences sp) {
if (sp.getString("unit", "C").equals("°C")) {
return UnitConvertor.kelvinToCelsius(temperature);
} else if (sp.getString("unit", "C").equals("°F")) {
return UnitConvertor.kelvinToFahrenheit(temperature);
} else {
return temperature;
}
}
public static float kelvinToCelsius(float kelvinTemp) {
return kelvinTemp - 273.15f;
}
public static float kelvinToFahrenheit(float kelvinTemp) {
return (((9 * kelvinToCelsius(kelvinTemp)) / 5) + 32);
}
}
I have a MainActivity.java activity file which will convert tempature based on user input when checkbox is checked:
float temperature = UnitConverter.convertTemperature(Float.parseFloat(todayWeather.getTemperature()), sp);
if (sp.getBoolean("temperatureInteger", false)) {
temperature = Math.round(temperature);
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
if (isNetworkAvailable()) {
getTodayWeather();
getLongTermWeather();
}
return true;
}
}
I have a activity_main.xml file which has the following code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://ift.tt/nIICcg">
<PreferenceCategory android:title="@string/settings_title_units">
<ListPreference
android:defaultValue="C"
android:entries="@array/temperatureUnits"
android:entryValues="@array/temperatureUnitsValues"
android:key="unit"
android:title="@string/setting_tempUnits" />
<CheckBoxPreference
android:defaultValue="false"
android:key="temperatureInteger"
android:title="@string/setting_showTempAsInteger" />
</PreferenceCategory>
</PreferenceScreen>
I have a arrays.xml file which has the following code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="temperatureUnits">
<item name="C">@string/setting_unit_Celsuis</item>
<item name="F">@string/setting_unit_Fahrenheit</item>
<item name="K">@string/setting_unit_Kelvin</item>
</string-array>
<string-array name="temperatureUnitsValues">
<item name="C">°C</item>
<item name="F">°F</item>
<item name="K">K</item>
</string-array>
</resources>
I have a menu.xml file which has the following code:
<menu xmlns:android="http://ift.tt/nIICcg"
xmlns:app="http://ift.tt/GEGVYd"
xmlns:tools="http://ift.tt/LrGmb4"
tools:context=".activities.MainActivity">
<item
android:id="@+id/action_refresh"
android:icon="@drawable/ic_refresh_white_24dp"
android:title="@string/action_refresh"
app:showAsAction="always" />
</menu>
Currently, after I check the checkbox in layout file, I need to go to the MainActivity page and and then if I click on Refresh, only then the MainActivity page will reflect the temperature which I had checked through checkbox.
But my problem is when the checkbox is checked in layout file, automatically the MainActivity page should get refreshed.
Aucun commentaire:
Enregistrer un commentaire