我开发了一个采样时钟(模拟)应用程序来显示时间。现在我的应用程序显示了我的手机的当前时间。我希望通过展示世界时间来改进它。不幸的是,我对此并不了解,有人可以指导我吗?
答案 0 :(得分:2)
以下是我用来显示所有城市时间的代码。
<强> main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<强> TIMEZONE.XML 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="vertical" >
<TextView
android:id="@+id/timezone_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip" />
<TextView
android:id="@+id/timezone_time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dip" />
</LinearLayout>
班级档案是......
<强> MainActivity.java 强>
package com.practice.secondhand;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MainActivity extends ListActivity {
protected static final int UPDATE_UI = 0;
TimeZoneAdaptor timezoneAdaptor = null;
List<TimeZoneData> timezonelist = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timezonelist = new ArrayList<TimeZoneData>();
timezoneAdaptor = new TimeZoneAdaptor(this, R.layout.timezoneview,
timezonelist);
setListAdapter(timezoneAdaptor);
}
@Override
protected void onStart() {
super.onStart();
String[] listItems = TimeZone.getAvailableIDs();
TimeZone timezone = null;
SimpleDateFormat format = new SimpleDateFormat(
"EEE, MMM d, yyyy h:mm a");
Date now = new Date();
for (int index = 0; index < listItems.length; ++index) {
timezone = TimeZone.getTimeZone(listItems[index]);
format.setTimeZone(timezone);
timezonelist.add(new TimeZoneData(getDiaplayName(listItems[index]),
format.format(now)));
timezone = null;
}
}
private String getDiaplayName(String timezonename) {
String displayname = timezonename;
int sep = timezonename.indexOf('/');
if (-1 != sep) {
displayname = timezonename.substring(0, sep) + ", "
+ timezonename.substring(sep + 1);
displayname = displayname.replace("_", " ");
}
return displayname;
}
public class TimeZoneAdaptor extends ArrayAdapter<TimeZoneData> {
List<TimeZoneData> objects = null;
public TimeZoneAdaptor(Context context, int textViewResourceId,
List<TimeZoneData> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
@Override
public int getCount() {
return ((null != objects) ? objects.size() : 0);
}
@Override
public TimeZoneData getItem(int position) {
return ((null != objects) ? objects.get(position) : null);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater vi = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.timezoneview, null);
}
TimeZoneData data = objects.get(position);
if (null != data) {
TextView textName = (TextView) view
.findViewById(R.id.timezone_name);
TextView textTime = (TextView) view
.findViewById(R.id.timezone_time);
textName.setText(data.name);
textTime.setText(data.time);
}
return view;
}
}
}
<强> TimeZoneData.java 强>
package com.practice.secondhand;
public class TimeZoneData {
public String name;
public String time;
public TimeZoneData(String name,String time) {
this.name = name;
this.time = time;
}
}
TimeZoneAdaptor .java
public class TimeZoneAdaptor extends ArrayAdapter<TimeZoneData> {
List<TimeZoneData> objects = null;
public TimeZoneAdaptor(Context context, int textViewResourceId,
List<TimeZoneData> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
public int getCount() {
return ((null != objects) ? objects.size() : 0);
}
public TimeZoneData getItem(int position) {
return ((null != objects) ? objects.get(position) : null);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater vi = (LayoutInflater) TimeZonesList.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.timezoneview, null);
}
TimeZoneData data = objects.get(position);
if (null != data) {
TextView textName = (TextView) view
.findViewById(R.id.timezone_name);
TextView textTime = (TextView) view
.findViewById(R.id.timezone_time);
textName.setTextSize(18);
textName.setTextColor(Color.GREEN);
textName.setText(data.name);
textTime.setText(data.time);
textTime.setTextSize(15);
}
return view;
}
}
答案 1 :(得分:0)
您可以调用java.util.TimeZone.getAvailableIDs来获取所有可用时区的名称,并getTimeZone来实例化每个时区。然后使用TimeZone对象构造GregorianCalendar对象。使用正确的字段进行设置后,您可以调用其getTime方法获取Date对象,然后您可以使用DateFormat以可显示的格式进行格式化。
这一切都很混乱,真的......
答案 2 :(得分:0)
除了onCreate()
中的最后一行之外,上面的内容很棒 setListAdapter(timezoneAdaptor);
它不在任何其他类中,也不在Java或Android库中。任何人都有任何关于如何修复此问题的想法,因为代码无法按原样编译?我将继续工作,如果我弄清楚我会更新的东西。
修改强>
需要扩展ListActivity而不是Activity。对于用于setListAdapter()方法的Android库的搜索没有任何结果,这仍然很奇怪。