Android - 如何使用SharedPreferences更改文件以进行查看

时间:2012-02-17 01:05:43

标签: android sharedpreferences

我正在尝试使用此功能,但我不能。我的应用程序没有SQL数据库,它只有.xml文件。我想通过首选项设置更改文件以查看,例如我可以查看或文件data.xml或文件data2.xml检查或取消选中复选框。

活动PreferencesFromXml:

public class PreferencesFromXml extends PreferenceActivity{

    public static final String COLORE_DEFAULT = "#000000";

    public static final String COLORE_PREF = "colore";

    public static final String TITOLO_PREF = "titolo";

    public static final String USA_TITOLO_CUSTOM_PREF = "usa_titolo_custom";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.preferences);

            SharedPreferences prefs =     PreferenceManager.getDefaultSharedPreferences(this);

            Preference titoloPrefs = findPreference(TITOLO_PREF);
            titoloPrefs.setSummary(prefs.getString(TITOLO_PREF, getString(R.string.titolo_custom)));
            titoloPrefs.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
            {
                    public boolean onPreferenceChange(Preference prefs, Object value)
                    {
                            prefs.setSummary((CharSequence) value);
                            return true;
                    }
            });
    }
}

主要活动:

public class Main extends ExpandableListActivity{
    ExpandableListAdapter mAdapter;
    private ArrayList<Group> groups;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list_main);

            groups = readGroupsFromXml();

            mAdapter = new MyExpandableListAdapter(getLayoutInflater(), groups);
            setListAdapter(mAdapter);
            registerForContextMenu(getExpandableListView());
    }

    public ArrayList<Group> readGroupsFromXml()
    {
            try
            {
                    final XmlHandler handler = new XmlHandler();
                    final SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
                    sp.parse(getApplicationContext().getResources().openRawResource(R.raw.data), handler);
                    return handler.getGroups();
            }
            catch (Exception e)
            {
                    Log.e("Error", "xml", e);
            }
            return null;
    }

然后我不知道在选中复选框时选择要使用的文件该怎么办。我在这里停了下来(虽然我不知道它是否正确):

@Override
protected void onResume(){
    super.onResume();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    Editor editor = prefs.edit();

    String usa_titolo_custom;
            if (prefs.getBoolean(PreferencesFromXml.USA_TITOLO_CUSTOM_PREF, false))
            {

从代码中可以看出.xml文件位于res / raw中。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

只需设置一个布尔共享的prefs并使用它。把这些作为全局变量......

SharedPreferences data;
public static String filename = "filename";
boolean b;

然后在onCreate() put

data = getSharedPreferences(filename, 0);

设置布尔值
SharedPreferences.Editor editor = data.edit();
editor.putBoolean("data", b);
editor.commit();

得到像这样的布尔值,其中default为真或假,取决于你希望它是什么如果它没有设置。

 b = data.getBoolean("data", default);
然后使用

之类的东西
public ArrayList<Group> readGroupsFromXml() {

b = data.getBoolean("data", false);
if (b == true){ 
    try {
        final XmlHandler handler = new XmlHandler();
        final SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
        sp.parse(getApplicationContext().getResources().openRawResource(R.raw.data), handler);
        return handler.getGroups();
    } catch (Exception e) {
        Log.e("Error", "xml", e);
    } 
} else {
    try {
        final XmlHandler handler = new XmlHandler();
        final SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
        sp.parse(getApplicationContext().getResources().openRawResource(R.raw.data2), handler);
        return handler.getGroups();
    } catch (Exception e) {
        Log.e("Error", "xml", e);
        }
    }
return null;

}

如果b为真,则使用data.xml,如果为false,则使用data2.xml。