0
我无法从此类中的sharedpreferences获取值。 getDefaultSharedPreferences给出错误“PreferenceManager类型中的方法getDefaultSharedPreferences(Context)不适用于参数(HandlingXMLStuff)”无法从ListPreference中获取值
我可以从我的主类获取值,但无法将它传递给显示的类下面。
public class HandlingXMLStuff extends DefaultHandler {
String Units;
private XMLDataCollected info = new XMLDataCollected();
public String getInformation() {
return info.dataToString();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
String unit = prefs.getString("unitsofmeasure", "Fahrenheit");
Log.d("HandlingXMLStuff", "test" + unit);
// if (Units == "Farenheit") {
if (localName.equals("city")) {
String city = attributes.getValue("data");
info.setCity(city);
Log.d("DeskClockActivity", "test" + city);
} else if (localName.equals("temp_f")) {
String t = attributes.getValue("data");
int temp = Integer.parseInt(t);
info.setTemp(temp);
}
/*
* }/* else if (unit == "C") { if (localName.equals("city")) { String
* city = attributes.getValue("data"); info.setCity(city); } else if
* (localName.equals("temp_c")) { String t =
* attributes.getValue("data"); int temp = Integer.parseInt(t);
* info.setTemp(temp); } }
*/
}
}
那么是否有另一种方法可以从DefaultHandler类的sharedpreference中获取值? – MikeC 2012-04-22 19:04:06
只是将上下文传递给您的HandlingXMLStuff类并将其用于getDefaultSharedPreferences方法 – 2012-04-22 19:09:09
@Mike:当然,您的HandlingXMLStuff构造函数需要一个'Context'引用并将其用于与共享首选项的交互。或者,您可以设置一个监听器接口,在适用的情况下,您的'HandlingXMLStuff'将调用方法,并将具有共享参数的交互抽象为具有“Context”的其他类。 – 2012-04-22 19:10:15