我需要在用户更改首选项后更新片段中的两个textview。我已经找了很多类似的问题,但它不会帮助我。我甚至尝试每次更新所有应用程序,但我确定它不是最好的解决方案。 将不胜感激任何帮助。
AppPreferenceActivity.java
public class AppPreferenceActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref);
}
}
}
WeatherFragment.java (我为了方便而剪了代码)
public class WeatherFragment extends Fragment implements Constants {
TextView currentTemperatureField;
TextView pressureField;
//...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_weather, container, false);
currentTemperatureField = (TextView)rootView.findViewById(R.id.current_temperature_field);
pressureField = (TextView) rootView.findViewById(R.id.pressure_field);
//...
weatherAdapter = new WeatherAdapter(getContext(), weather);
ListView listView = (ListView)rootView.findViewById(R.id.listView);
listView.setAdapter(weatherAdapter);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
updateWeatherData(new CityPreference(getActivity()).getCity());
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
tempPref = sharedPrefs.getString("tempPref", "Celsius");
pressurePref = sharedPrefs.getString("pressurePref","mmHg");
//...
}
private void renderWeather(JSONObject json){
try {
//...
if(pressurePref.equals("mmHg")) {
pressureField.setText(String.format("%.0f", (main.getDouble("pressure") / 1.333)) + " mmHg");
}else{
pressureField.setText(String.format("%.0f", main.getDouble("pressure")) + " hPa");
}
if(tempPref.equals("Celsius")) {
currentTemperatureField.setText(String.format("%.1f", main.getDouble("temp")) + " ℃");
}else{
currentTemperatureField.setText(String.format("%.1f", (main.getDouble("temp")*1.8+32)) + " ℉");
}
//...
}catch(Exception e){
//...
}
}
}
UPD 感谢@beeb。我已经使用了OnSharedPreferenceChangeListener():
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//weatherFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/weather.ttf");
updateWeatherData(new CityPreference(getActivity()).getCity());
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
tempPref = sharedPrefs.getString("tempPref", "Celsius");
pressurePref = sharedPrefs.getString("pressurePref","mmHg");
SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Log.e("WeatherActivity: ","preferences have changed");
//Implementation
}
};
sharedPrefs.registerOnSharedPreferenceChangeListener(listener);
}
但我的问题仍然是"实施"。我不知道如何将新数据设置为片段的文本视图。
答案 0 :(得分:0)
不要像这样传递Fragment,而是采用以下方法制作实例。
MyPreferenceFragment myPreferenceFragment;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myPreferenceFragment = new MyPreferenceFragment()
getFragmentManager().beginTransaction().replace(android.R.id.content, myPreferenceFragment).commit();
}
然后在片段中创建一个方法来更新textView
publich void updateText(String text)
{
textView.setText(text);
}
并在想要更新文本时调用此方法:
myPreferenceFragment.updateText(text);
答案 1 :(得分:0)
在SharedPreferences.onSharedPreferenceChangeListener()
。
WeatherFragment
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// Implementation
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
答案 2 :(得分:0)
遇到类似的问题并遇到了这个问题。为我修复的是将代码放在片段的onResume方法中。当包含视图对象的片段与设置菜单的活动不同时,这非常有用。
sony_camera_api