解:-

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在这里

android:layout_width="match_parent"

android:layout_height="match_parent">

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" />

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐