2012-01-28 6 views
1

私はサンプルクロック(アナログ)アプリケーションを開発しました。現在、私のアプリケーションは自分の携帯の現在の時刻を表示します。私はの世界時間を表示するためにそれを改善したいです。残念ながら、私はこれに多くの知識を持っていない、誰かが私を導くことができますか?アンドロイドのさまざまなタイムゾーンの時計機能を開発するのに助けが必要ですか?

答えて

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; 

    } 

} 
+0

コードをお寄せいただきありがとうございます。しかし、コンパイルを妨げる小さな問題が1つあります。そのファイルTimeZoneAdaptor.javaおよびその行:LayoutInflater vi =(LayoutInflater)TimeZonesList.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);問題は次のとおりです。** TimeZoneListをタイプ**に解決することはできません。助言がありますか? – Igor

+0

TimeZoneAdaptor.javaファイルのように見えます。同じクラスがMainActivityクラスの内部で定義されています。 – Igor

0

使用可能なすべてのタイムゾーンの名前を取得するにはjava.util.TimeZone.getAvailableIDsと呼び出すことができ、getTimeZoneは各タイムゾーンをインスタンス化することができます。そして、TimeZoneオブジェクトを使用してGregorianCalendarオブジェクトを構築します。右側のフィールドでこれを設定したら、getTimeメソッドを呼び出してDateオブジェクトを取得し、表示可能な形式でDateFormatを使用してフォーマットすることができます。

それがすべてではなく混乱だ、本当に...ここ

+0

ご返信ありがとうございます。それを行うためのサンプルコードを投稿してください。 – Aerrow

0

上記のonCreateの最後の行()

setListAdapter(timezoneAdaptor); 

それをを除いて大きいですいいえ他のクラスのいずれかではなく、JavaまたはAndroidライブラリにはありません。コードをそのままコンパイルすることはできないので、誰でもこの問題を解決する方法はありますか?私は仕事を続けるつもりで、もし私が更新するものを見つけ出すならば。

活動するのではなく、ListActivityを拡張するために必要な

編集。 Android LibraryのsetListAdapter()メソッドの検索で何も表示されないというのはまだ奇妙なことです。

+0

Draukadin:私は私の答えを編集しました、それはあなたを助けてくれることを願っています – Aerrow

+0

私はそれを働かせました。すべてのTimeZone IDが何であるかを理解するのに非常に役立ちました。これで、タイムゾーンIDが私のアプリケーションで動作するデジタル時計を手に入れようとしているのを知ったので、システム時間に関係なくGMT時間が表示されます。それほど簡単ではありません。 – Draukadin

+0

Happy Codding .... – Aerrow

関連する問題