2011-07-31 170 views
0

你好,我有以下類來顯示圖,因爲它可以在代碼中看到我使用它的一些隨機值,我們如何刷新此活動在每一秒獲得移動圖。由於刷新活動

public class SalesGrowthChart extends AbstractDemoChart { 

    public String getName() { 
    return "Sales growth"; 
    } 


    public String getDesc() { 
    return "The sales growth across several years (time chart)"; 
    } 


    public Intent execute(Context context) { 
    String[] titles = new String[] { "Sales growth January 1995 to December 2000" }; 
    List<Date[]> dates = new ArrayList<Date[]>(); 
    List<double[]> values = new ArrayList<double[]>(); 
    Date[] dateValues = new Date[] { new Date(95, 0, 1), new Date(95, 3, 1), new Date(95, 6, 1), 
     new Date(95, 9, 1), new Date(96, 0, 1), new Date(96, 3, 1), new Date(96, 6, 1), 
     new Date(96, 9, 1), new Date(97, 0, 1), new Date(97, 3, 1), new Date(97, 6, 1), 
     new Date(97, 9, 1), new Date(98, 0, 1), new Date(98, 3, 1), new Date(98, 6, 1), 
     new Date(98, 9, 1), new Date(99, 0, 1), new Date(99, 3, 1), new Date(99, 6, 1), 
     new Date(99, 9, 1), new Date(100, 0, 1), new Date(100, 3, 1), new Date(100, 6, 1), 
     new Date(100, 9, 1), new Date(100, 11, 1) }; 
    dates.add(dateValues); 

    values.add(new double[] { (int) (Math.random() * 4), (int) (Math.random() * 4),(int) (Math.random() * 4), (int) (Math.random() * 4), (int) (Math.random() * 4), (int) (Math.random() * 4) }); 
    int[] colors = new int[] { Color.BLUE }; 
    PointStyle[] styles = new PointStyle[] { PointStyle.POINT }; 
    XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles); 
    setChartSettings(renderer, "Sales growth", "Date", "%", dateValues[0].getTime(), 
     dateValues[dateValues.length - 1].getTime(), -4, 11, Color.GRAY, Color.LTGRAY); 
    renderer.setYLabels(10); 
    return ChartFactory.getTimeChartIntent(context, buildDateDataset(titles, dates, values), 
     renderer, "MMM yyyy"); 
    } 

} 

回答

1

你要在你的主要活動,以創建一個新的線程,像這樣:

public class myActivity extends Activity { 
    boolean graphActive = true; 

    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     new Thread(new Runnable() { 
      public void run() { // Process to keep running 
       while (graphActive) { 
        SalesGrowthChart.execute(); 
        try { 
         Thread.sleep(1000) // sleeps for 1 second 
        } catch (Error e) { 
        } 
       } 
      }}).start(); 

     } 
} 

你可以通過設置graphActive =假 希望這有助於隨時隨地殺死線程。 :)