如何初始化全局变量android

时间:2014-01-04 11:09:22

标签: java android

我想将表Int [] y初始化为全局变量在何处以及如何执行此操作 折线图的代码

public class LineGraph{
Context applicationContext;
Boolean Buttons;
Boolean Legend;
Boolean Grid;
public Intent getIntent(Context context) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

    Buttons = prefs.getBoolean("view", true);
    Legend = prefs.getBoolean("legend", true);
    Grid = prefs.getBoolean("Grid", false);
    // Our first data
    int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // x values!
    int[] y =  { 30, 34, 45, 57, 77, 89, 100, 111 ,123 ,145 }; // y values!
    TimeSeries series = new TimeSeries("Line1"); 
    for( int i = 0; i < x.length; i++)
    {
        series.add(x[i], y[i]);
    }

    // Our second data
    int[] x2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // x values!
    int[] y2 =  { 145, 123, 111, 100, 89, 77, 57, 45, 34, 30}; // y values!
    TimeSeries series2 = new TimeSeries("Line2"); 
    for( int i = 0; i < x2.length; i++)
    {
        series2.add(x2[i], y2[i]);
    }

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    dataset.addSeries(series);
    dataset.addSeries(series2);

    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); // Holds a collection of XYSeriesRenderer and customizes the graph
    mRenderer.setYLabels(10);
    mRenderer.setYLabelsAlign(Align.RIGHT);
    if (Buttons==true){
    mRenderer.setZoomButtonsVisible(true);}
    else{
    mRenderer.setZoomButtonsVisible(false); 
    }
    if (Legend==true){
        mRenderer.setShowLegend(true);
    }else{
        mRenderer.setShowLegend(false);
    }
    if (Grid==true){
        mRenderer.setShowGrid(true);
    }else{
        mRenderer.setShowGrid(false);
    }
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setBackgroundColor(Color.BLACK);
    XYSeriesRenderer renderer = new XYSeriesRenderer(); // This will be used to customize line 1
    XYSeriesRenderer renderer2 = new XYSeriesRenderer(); // This will be used to customize line 2
    mRenderer.addSeriesRenderer(renderer);
    mRenderer.addSeriesRenderer(renderer2);

    // Customization time for line 1!
    renderer.setColor(Color.WHITE);
    renderer.setPointStyle(PointStyle.SQUARE);
    renderer.setFillPoints(true);
    // Customization time for line 2!
    renderer2.setColor(Color.YELLOW);
    renderer2.setPointStyle(PointStyle.DIAMOND);
    renderer2.setFillPoints(true);

    Intent intent1 = new Intent (context, GraphLoading.class);
    intent1.putExtra("SomeKey",x);

    Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRenderer, "Line Graph Title");
    return intent;

}

}

我希望这个表可以被这个类看到:

public class GraphLoading extends Activity {
    private LineGraph parent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading);
    }
}

我希望GraphLoading可以编辑这个表,LineGraph类可以看到更改

3 个答案:

答案 0 :(得分:0)

将此声明放在布尔变量和getIntent函数之间:public static int[] y;并在y赋值之前删除int []。现在,您可以在包的每个方法和类中使用数组y!

答案 1 :(得分:0)

您可以创建此课程的static个对象 例如:

public class LineGraph{
   public int Number;
   public LineGraph() {}
}

然后,您应该在任何活动中创建此类的静态对象,例如ExampleActivity

public static LineGraph LG = new LineGraph();
//then set some data to LG
LG.Number = 10;

然后您可以在其他活动中使用此clas中的数据:

int Test = ExampleActivity.LG.Number;
//We will have 10



或者第二种方式。您可以使用Intent(在FirstPage活动中)发布数据:

String str = "Test";
int num = 10;
int array[] = {1,2,3};
Intent intent = new Intent(FirstPage.this, SecondPage.class);
intent.putExtra("TEST_STRING", str);
intent.putExtra("TEST_INT", num);
intent.putExtra("TEST_INT_ARRAY", array);
startActivity(intent);

然后在SecondPage活动中,您可以获得以下数据:

Intent sender = getIntent();
String some_str = sender.getExtras().getString("TEST_STRING");
int some_int = sender.getExtras().getInt("TEST_INT");
int[] some_int_array = sender.getExtras().getIntArray("TEST_INT_ARRAY");

答案 2 :(得分:0)

int[] y声明为类变量(静态字段)和public,以便它也可以在其包的其他类中访问:

public class LineGraph {
    public static int[] y =  {30, 34, 45, 57, 77, 89, 100, 111, 123, 145};
    Context applicationContext;
    // ...

然后从您的其他课程中引用它:LineGraph.y

示例:

public class GraphLoading extends Activity {
    private LineGraph parent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading);

        int y[] = LineGraph.y;
    }
}