根据在微调器中选择的项目更新活动

时间:2013-04-11 12:02:19

标签: android spinner android-spinner androidplot

在当前活动中,我打印了一张图表(使用Androidplot),显示了之前活动中所选股票的收盘价。
在这个活动中,我有一个用户可以叠加的指标列表的微调器 现在我想用旋转器中的这个新选项重新绘制图形 我确实尝试刷新/重新加载onItemSelected页面,但这会导致页面保持刷新,甚至无需等待用户输入。

public class DispGraph extends Activity {

private XYPlot plotstock;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.graph);

    // PRINTING SELECTED STOCK_NAME

    Bundle bundle = getIntent().getExtras();
    String sname = bundle.getString("SN");
    TextView t = (TextView) findViewById(R.id.textView1);
    t.setText(sname);

    // INDICATOR LIST

    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.Indicators, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    String iname = spinner.getSelectedItem().toString();
    spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            startActivity(getIntent());
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }});

    //PASSING STOCK-TICKER & INDICATOR TO PHP



    // PLOTTING GRAPH

    plotstock = (XYPlot) findViewById(R.id.mySimpleXYPlot);
    Number[] series1Numbers = ind;
    Number[] series2Numbers = closing;

    XYSeries series1 = new SimpleXYSeries(Arrays.asList(series1Numbers),SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, iname);
    XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers),SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Closing Price");

    plotstock.setDomainLabel("Date");
    plotstock.setRangeLabel("Price");

    plotstock.addSeries(series1,new LineAndPointFormatter(Color.rgb(0, 200, 0), Color.rgb(0,100, 0), null, new PointLabelFormatter(Color.TRANSPARENT)));
    plotstock.addSeries(series2,new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100),null, new PointLabelFormatter(Color.TRANSPARENT)));

    plotstock.setTicksPerRangeLabel(2);

}
}

1 个答案:

答案 0 :(得分:0)

使用您当前的方法,每次用户选择微调器值时,您的当前活动将再次启动,因此它将被放置在Android Stack上。很快,如果用户更改了微调器n次,则应按n + 1次以关闭应用程序。这是一种非常讨厌的行为。

好吧,我不知道AndroidPlot,但是看看它的API我找到了重绘方法。所以,我想你可以试试这个:

每次用户选择微分值时,我都可以在调用plotstock

plotstock.redraw()对您进行修改
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        // TODO Auto-generated method stub
        //Change the graphics value here!
        plotstock.redraw();
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }});