开发者

Where do I put my preference xml files in Android?

开发者 https://www.devze.com 2023-03-16 13:45 出处:网络
I want to use a preferences.xml file for storing/retrieving application wide preferences. Where do I store the xml file so that I can use:

I want to use a preferences.xml file for storing/retrieving application wide preferences. Where do I store the xml file so that I can use:

getSharedPreferences("preferences", 0)

My preferences.xml file:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk开发者_Python百科/res/android">
    <PreferenceCategory>
        <Preference android:key="units_length" android:title="imperial" android:summary="Whatever"></Preference>
        <Preference android:title="imperial" android:key="units_weight" android:summary="Whatever"></Preference>
    </PreferenceCategory>
</PreferenceScreen>

And here is how the onCreate method looks like in my activity:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.results);

    // Get preferences
    preferences = getSharedPreferences("preferences", 0);

    // Fetch the text view
    TextView text = (TextView) findViewById(R.id.textView1);

    // Set new text
    text.setText(preferences.getString("units_length", "nothing"));

}

My application just says "nothing".

Thanks.


Put it in the res/xml directory.

0

精彩评论

暂无评论...
验证码 换一张
取 消