2013-02-18 58 views
0

我有一個獲取GPS信號並將結果發送到自定義視圖的片段。TextView在自定義視圖中沒有更新查看

我可以顯示從片段中獲取的信號的輸出,並看到它更新得很好。下面是片段1的代碼:

 @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 

      RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.gps_stamp_fragment, container,false); 
      RelativeLayout rl1 = new RelativeLayout(view.getContext()); 
      TextView tView1 = new TextView(view.getContext()); 
      tView1.setText("waiting..."); //this is the only thing custom view gets! 
      rl1.addView(tView1); 
      rl1.setId(1); 
      tView1.setId(2); 
      view.addView (rl1); 

      lm =(LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this); 


      return view; 
     } 

//And here's where I get the GPS signal from and use setText again to update to the GPS signal. 

    @Override 
    public void onLocationChanged(Location arg0) { 
     String lat = String.valueOf(arg0.getLatitude()); 

     RelativeLayout rl1 = (RelativeLayout) getView().findViewById(1); 
     TextView tView1 = (TextView) rl1.findViewById(2); 
     tView1.setText(lat); 

    } 

然後,我有以下的代碼,發現相對佈局和相關文本瀏覽和獲取文本自定義視圖。

RelativeLayout rl1 = (RelativeLayout) getRootView().findViewById(1); 
TextView tView1 = (TextView) rl1.findViewById(2); 
tView1.getText(); 
getRootView().invalidate(); 

問題是在這個自定義視圖中出現的唯一文本是「等待...」。 GPS測量從不出現。

我讀過類似的問題,我需要.invalidate()視圖這就是爲什麼我添加了最後一行,但這似乎並沒有爲我工作。

爲什麼TextView在從fragment1中調用時會正確更新,而不是從自定義視圖中調用時?

謝謝。

回答

2

invalidate應該是內部onLocationChanged

+0

謝謝你,布萊恩!這確實是問題! – lynvie 2013-02-18 18:35:47