如何在Android列表中显示带有国家和城市名称的时区?

时间:2018-08-03 07:46:33

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

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

2 个答案:

答案 0 :(得分:0)

解决方案:-

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>

答案 1 :(得分:0)

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

您可以找到更多信息here