I’m trying to work this function but I can’t. My application hasn’t got a SQL database, it has only .xml files. I would like to change file to view by the preferences settings, for example I would be able to view or the file data.xml or the file data2.xml checking or unchecking a checkbox.
Activity 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;
}
});
}
}
Main Activity:
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;
}
Then I don’t know what to do to choose the file to use when the checkbox is selected or not. I stopped here (although I don’t know if it’s correct):
@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))
{
As you can see from the code .xml files are located in res/raw. Thank you for your help.
just set up a boolean shared prefs and use it. Put these as global variables …
Then in
onCreate()putset the boolean with
get the boolean like this, where
defaultis true or false, depending on what you want it to be if it’s not set.then use something like
If
bis true it uses data.xml, if it’s false it uses data2.xml.