2017-02-12 10 views
1

私は以下のレイアウトファイルを持っています。基本的に私はグーグルマップを持っており、左上にはTextViewがあり、カウンターを15秒ごとに地図をリフレッシュするようにしておく必要があります。レイアウトは問題ありません。Androidアップデートのtextviewタイマーと地図コンテンツを更新しますか?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/map" 
    tools:context="com.example.ns.appversion1.MapActivityFragment1" 
    android:name="com.google.android.gms.maps.SupportMapFragment"/> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:weightSum="2" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="left" 
       android:padding="3dp" 
       android:id="@+id/textRefresh" 
       android:background="@drawable/border" 
       android:text="Refreshing 1" 
       android:textColor="#03A9FA" 
       android:textSize="12sp" /> 


     </LinearLayout> 
    </FrameLayout> 
</RelativeLayout> 

以下は私のコードです。ビュー関数では、カウンタを保持する関数Handlerを実装しようとしています。したがって、私はJSON関数を呼び出して最新のlat long値を取得し、それに応じてマップにプロットしたいと考えています。私は今ここで最初のステップで私はエラーが内部のクラスの中からのアクセス変数timeleftm_handlerTaskm_handlerを言うエラーを突きつけた?私はカウントダウンを表示したいので、ユーザーは次の15秒間に地図がリフレッシュされるカウンタを知るでしょうか?

public class MapActivityFragment1 extends Fragment implements OnMapReadyCallback { 
    private GoogleMap mMap; 
    TextView tv; 
    // TODO: Rename parameter arguments, choose names that match 
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 

    // TODO: Rename and change types of parameters 
    private String mParam1; 
    private String mParam2; 

    private OnFragmentInteractionListener mListener; 

    public MapActivityFragment1() { 
     // Required empty public constructor 
    } 

    // TODO: Rename and change types and number of parameters 
    public static MapActivityFragment1 newInstance(String param1, String param2) { 
     MapActivityFragment1 fragment = new MapActivityFragment1(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_map_activity_fragment1, null, false); 

     SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
     tv = (TextView) getActivity().findViewById(R.id.textRefresh); 
     Handler m_handler; 
     Runnable m_handlerTask ; 
     final int timeleft=15000; 
     final m_handler = new Handler(); 
     final m_handlerTask = new Runnable() 
     { 
      @Override 
      public void run() { 
       if(timeleft>=0) 
       { 
        // do stuff 
        Log.i("timeleft",""+timeleft); 
        timeleft--; 
        tv.setText("Refreshing in "+timeleft); 

       } 
       else 
       { 
        m_handler.removeCallbacks(m_handlerTask); // cancel run 
       } 
       m_handler.postDelayed(m_handlerTask, 1000); 
      } 
     }; 
     m_handlerTask.run(); 

     return view; 
    } 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 


    public void onAttach(Context context) { 
     super.onAttach(context); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void onFragmentInteraction(Uri uri); 
    } 
} 

次のようにCharuの解を試しました。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_map_activity_fragment1, null, false); 

     SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
     tv = (TextView) getActivity().findViewById(R.id.textRefresh); 
     MyCount counter = new MyCount(15000, 1000); 
     counter.start(); 

     return view; 
    } 
+0

あなたは多くを言っていました。正確に何をしたいのですか?)1. 15秒でマップをリフレッシュするか、カウントダウンを使用しますか? –

+0

私はちょうど私のラベルにカウントダウンを表示したい – user5313398

+0

それは15から数え、次に14まで13などを1にすると、私はマップを更新するjsonを呼び出しますか? – user5313398

答えて

1

これは、(第2の-1)15から0までカウントダウンをデモし、このタスクは、すべて15秒後に再び繰り返されます!

public class YourActivity extends Activity { 


    TextView textView; 
    android.os.Handler customHandler; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
     textView = (TextView) findViewById(R.id.text); 


     customHandler = new android.os.Handler(); 
     customHandler.postDelayed(updateTimerThread, 0); 
    } 


    // count down timer is an abstract class, so extend it and fill in methods 
    public class MyCount extends CountDownTimer { 

     public MyCount(long millisInFuture, long countDownInterval) { 
      super(millisInFuture, countDownInterval); 
     } 

     @Override 
     public void onFinish() { 
      textView.setText("done!"); 
     } 

     @Override 
     public void onTick(long millisUntilFinished) { 
      textView.setText("Left: " + millisUntilFinished/1000); 
     } 
    } 

    private Runnable updateTimerThread = new Runnable() 
    { 
     public void run() 
     { 

      // 10000 is the starting number (in milliseconds) 
      // 1000 is the number to count down each time (in milliseconds) 
      MyCount counter = new MyCount(15000, 1000); 
      counter.start(); 
      customHandler.postDelayed(this, 15000); // repeat body after 15 seconds 
     } 
    }; 
} 
+1

コメントは議論の延長ではありません。この会話は[チャットに移動]されています(http://chat.stackoverflow.com/rooms/135515/discussion-on-answer-by-charu-android-update-textview-timer-and-refresh-map-con) 。 –

関連する問題