开发者

Launch Time Zone list in Android for pick/result

I see that it's possible to launch the date & time settings开发者_Go百科 via an intent in Android, but what I'd like to do is launch just the list that shows the time zones (clicking "Select time zone") and get back the selected value without having the selection modify the user's date & time settings. Any idea how to do this?


If you are talking about a spinner you could do something like this:

ArrayAdapter <CharSequence> adapter =
          new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item );
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    String[]TZ = TimeZone.getAvailableIDs();
    ArrayList<String> TZ1 = new ArrayList<String>();
    for(int i = 0; i < TZ.length; i++) {
        if(!(TZ1.contains(TimeZone.getTimeZone(TZ[i]).getDisplayName()))) {
            TZ1.add(TimeZone.getTimeZone(TZ[i]).getDisplayName());
        }
    }
    for(int i = 0; i < TZ1.size(); i++) {
        adapter.add(TZ1.get(i));
    }
    final Spinner TZone = (Spinner)findViewById(R.id.TimeZoneEntry);
    TZone.setAdapter(adapter);
    for(int i = 0; i < TZ1.size(); i++) {
        if(TZ1.get(i).equals(TimeZone.getDefault().getDisplayName())) {
            TZone.setSelection(i);
        }
    }

Take a look here for more literature on TimeZone


Inspired by CornCats example above but a slightly different and shorter implementation with adapters that worked for me:

private void populateAndUpdateTimeZone() {

   //populate spinner with all timezones
    mSpinner = (Spinner) findViewById(R.id.mytimezonespinner);
    String[] idArray = TimeZone.getAvailableIDs();
    idAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
            idArray);
    idAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mSpinner.setAdapter(idAdapter);

     // now set the spinner to default timezone from the time zone settings
    for(int i = 0; i < idAdapter.getCount(); i++) {
        if(idAdapter.getItem(i).equals(TimeZone.getDefault().getID())) {
            mSpinner.setSelection(i);
        }
    }


I know this is old, but I am also interested...

I have been looking at this and I do not think it is possible. I would really like to be able to do the same thing. It has come down to either porting some of the GPL Android code to my app (ack), finding a perconfigured DB and code (JAR or java), or lastly writing this code myself (double ack). The timezones are a minefeild and I hope not to have to write the code myself. There are a lot of exceptions based on users locations.


Android system classes ZoneList.java and ZonePicker.java are used in building TimeZones list in "Date & Time" settings. Link to source ZoneList.java

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜