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