Android新手求助:如何在RecyclerView中获取带GMT的时区?
Hey there! As someone who’s worked with Android RecyclerViews and time zone handling, let me walk you through a simple, step-by-step way to get those GMT-tagged time zones and show them in a RecyclerView.
Step 1: Fetch All GMT Time Zones
First, we need to grab all time zones that have the GMT identifier. Android leverages the Java TimeZone class, which has a handy method to get all available time zone IDs. We’ll filter these to keep only the ones starting with "GMT" (since those are formatted like GMT+03:00 or GMT-05:00).
Here’s the code to do that:
// Initialize a list to hold our GMT time zones List<String> gmtTimeZones = new ArrayList<>(); // Get all available time zone IDs String[] allTimeZoneIds = TimeZone.getAvailableIDs(); // Filter for GMT-only entries for (String id : allTimeZoneIds) { if (id.startsWith("GMT")) { gmtTimeZones.add(id); } } // Optional: Sort the list to arrange time zones from west to east Collections.sort(gmtTimeZones);
Step 2: Create a RecyclerView Adapter
Next, we need an adapter to connect our time zone list to the RecyclerView. First, make a simple layout for each list item (res/layout/item_time_zone.xml):
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tv_time_zone" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingVertical="12dp" android:paddingHorizontal="16dp" android:textSize="16sp"/>
Then create the adapter class:
public class TimeZoneAdapter extends RecyclerView.Adapter<TimeZoneAdapter.TimeZoneViewHolder> { private List<String> mTimeZoneList; // Constructor to pass in our time zone data public TimeZoneAdapter(List<String> timeZoneList) { mTimeZoneList = timeZoneList; } @NonNull @Override public TimeZoneViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // Inflate the item layout View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_time_zone, parent, false); return new TimeZoneViewHolder(itemView); } @Override public void onBindViewHolder(@NonNull TimeZoneViewHolder holder, int position) { // Bind the time zone ID to the TextView String timeZoneId = mTimeZoneList.get(position); holder.tvTimeZone.setText(timeZoneId); // Optional: Show a friendlier display name alongside GMT (e.g., GMT+01:00 (CET)) // TimeZone timeZone = TimeZone.getTimeZone(timeZoneId); // String displayName = timeZone.getDisplayName(false, TimeZone.SHORT); // holder.tvTimeZone.setText(timeZoneId + " (" + displayName + ")"); } @Override public int getItemCount() { return mTimeZoneList.size(); } // ViewHolder class to hold references to our views public static class TimeZoneViewHolder extends RecyclerView.ViewHolder { TextView tvTimeZone; public TimeZoneViewHolder(@NonNull View itemView) { super(itemView); tvTimeZone = itemView.findViewById(R.id.tv_time_zone); } } }
Step 3: Set Up the RecyclerView in Your Activity/Fragment
Finally, wire everything together in your Activity or Fragment. Make sure you have a RecyclerView in your layout (res/layout/activity_main.xml for example):
<androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_time_zones" android:layout_width="match_parent" android:layout_height="match_parent"/>
Then initialize it in your code:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get reference to the RecyclerView RecyclerView recyclerView = findViewById(R.id.recycler_time_zones); // Use a LinearLayoutManager to show items in a vertical list recyclerView.setLayoutManager(new LinearLayoutManager(this)); // Fetch the GMT time zones (from Step 1) List<String> gmtTimeZones = new ArrayList<>(); String[] allTimeZoneIds = TimeZone.getAvailableIDs(); for (String id : allTimeZoneIds) { if (id.startsWith("GMT")) { gmtTimeZones.add(id); } } Collections.sort(gmtTimeZones); // Attach the adapter to the RecyclerView TimeZoneAdapter adapter = new TimeZoneAdapter(gmtTimeZones); recyclerView.setAdapter(adapter); }
Extra Tips
- If you need to get the current device's GMT time zone, use
TimeZone.getDefault().getID()— this will return something likeGMT+02:00depending on the device's location. - The optional code in
onBindViewHolderlets you display a more user-friendly name (like "CET" for Central European Time) alongside the GMT offset, which can make the list more useful for users.
内容的提问来源于stack exchange,提问作者Pooja SWS

