Skip to content

Instantly share code, notes, and snippets.

@esteluk
Created December 14, 2010 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esteluk/740773 to your computer and use it in GitHub Desktop.
Save esteluk/740773 to your computer and use it in GitHub Desktop.
public class DownloadPicker extends PreferenceActivity {
DatabaseHelper locationData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
root.setTitle("Choose clock packs");
locationData = new DatabaseHelper(this);
SQLiteDatabase db = locationData.getReadableDatabase();
String[] cColumns = {"country"};
String[] cParms = {"1"};
Cursor countrys = db.query("location", cColumns, "complete=?", cParms, "country", null, "country");
countrys.moveToFirst();
while(!countrys.isAfterLast()) {
PreferenceScreen ps = getPreferenceManager().createPreferenceScreen(this);
ps.setTitle(countrys.getString(0));
root.addPreference(ps);
String[] dColumns = {"place", "_id", "downloaded"};
String[] dParms = {countrys.getString(0), "1"};
Cursor cities = db.query("location", dColumns, "country=? AND complete=?", dParms, null, null, "place");
cities.moveToFirst();
while(!cities.isAfterLast()) {
CheckBoxPreference cb = new CheckBoxPreference(this);
cb.setTitle(cities.getString(0));
cb.setKey(countrys.getString(0) + "-" + cities.getString(0));
cb.setPersistent(true);
if(cities.getInt(2) == 1)
cb.setChecked(true);
else
cb.setChecked(false);
final String[] dID = {cities.getString(1)};
cb.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
ContentValues values = new ContentValues();
DatabaseHelper data = new DatabaseHelper(DownloadPicker.this);
SQLiteDatabase write = data.getWritableDatabase();
if(((CheckBoxPreference) preference).isChecked()) {
// Code for async clock downloading here
values.put("downloaded", "1");
write.update("location", values, "_id=?", dID);
return true;
}
else {
// Delete local clock files
values.put("downloaded", "0");
write.update("location", values, "_id=?", dID);
return true;
}
}
});
ps.addPreference(cb);
cities.moveToNext();
}
countrys.moveToNext();
}
setPreferenceScreen(root);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment