【问题标题】:How to show timezone with country and city name in list in android?如何在android列表中显示带有国家和城市名称的时区?
【发布时间】:2019-01-11 01:20:42
【问题描述】:

我正在开发一个应用程序,我必须在spinner 中显示带有国家和城市名称的时区。我已经使用了TimeZone 类。但在这种情况下,我只得到timezoneIdtimezoneName。我如何在spinner 中显示带有国家和城市名称的时区?

【问题讨论】:

  • 时区通常用 region(大陆或海洋)和城市(或岛屿(组))给出,例如 America/Adak、Asia/Bangkok、Atlantic/金丝雀,印度/马埃岛。你会这样吗?
  • @OleV.V.我想显示像加尔各答(印度)、柏林(德国)这样的时区,但就我而言,它显示为 "Europe/Berlin" 。我没有在这里得到县名。是否有任何 api 或库在列表中显示带有国家和城市名称的显示时区?
  • @crammeur 我已经检查了文档。但它无法解决我的问题。
  • 我不知道这样的 API,但可能存在。我猜你基本上需要一个城镇和国家的数据库。

标签: java android android-studio timezone timezone-offset


【解决方案1】:

您是否考虑过使用Joda-Time?它提供了一个DateTimeUtils.getDefaultTimeZoneNames() 方法,该方法返回时区名称的映射。

您可以找到更多信息here

【讨论】:

  • 如果你准备好接受一个外部库——我同意这可能有很大的好处——为什么不选择 Joda-Time 的继任者呢? java.time 已被反向移植,并且 ThreeTenABP 中的反向移植适用于 Android。在新的 Android(API 级别 26 或更高级别)上,甚至内置了 java.time,不需要外部库。
  • @OleV.V.我只是建议 Joda Time,因为这是我认识的,但我会看看 ThreeTenABP,谢谢!
【解决方案2】:

解决方案:-

private String[] timezoneArray;
private Spinner spinner_timezone;

spinner_timezone = (Spinner) findViewById(R.id.spinner_timezone);

然后调用这个

 timezoneArray = TimeZone.getAvailableIDs();
        CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), timezoneArray);
        spinner_timezone.setAdapter(customAdapter);
        for (int i = 0; i < timezoneArray.length; i++) {
            if (timezoneArray[i].equals(TimeZone.getDefault().getID())) {
                spinner_timezone.setSelection(i);
                break;
            }
        }

然后CustomAdapter

 public class CustomAdapter extends BaseAdapter {
        Context context;
        String[] countryNames;
        LayoutInflater inflter;

        public CustomAdapter(Context applicationContext, String[] countryNames) {
            this.context = applicationContext;
            this.countryNames = countryNames;
            inflter = (LayoutInflater.from(applicationContext));
        }

        @Override
        public int getCount() {
            return countryNames.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = inflter.inflate(R.layout.custom_spinner_items, null);
            TextView names = (TextView) view.findViewById(R.id.textView);
            names.setText(displayTimeZone(TimeZone.getTimeZone(countryNames[i])));
            return view;
        }

        private String displayTimeZone(TimeZone tz) {

            long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset());
            long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset())
                    - TimeUnit.HOURS.toMinutes(hours);
            // avoid -4:-30 issue
            minutes = Math.abs(minutes);

            String result = "";
            if (hours > 0) {
                result = String.format("(GMT +%d:%02d) %s", hours, minutes, tz.getID());
            } else {
                result = String.format("(GMT %d:%02d) %s", hours, minutes, tz.getID());
            }

            return result;

        }
    }

这里还有 custom_spinner_items

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:textSize="15sp"
        android:text="Wifi Airtel"
        android:textColor="#000" />
</RelativeLayout>

【讨论】:

  • 它几乎没有按照a comment 中的提问者的要求以加尔各答(印度)或柏林(德国)的格式给出时区。它还使用了早已过时且设计不佳的TimeZone 类。它的现代替代品是现代 Java 日期和时间 API java.time 中的 ZoneId 类。
  • 感谢您的体验,亲爱的,您可以更新答案或发布新答案:),我也会检查一下
猜你喜欢
  • 1970-01-01
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 2011-04-28
相关资源
最近更新 更多