Thursday, April 11, 2013

[android help] Update the Activity based on what item is selected in the Spinner


In the current Activity I'm printing a graph (using Androidplot) of the closing prices of a selected stock from the previous activity.
In this activity I have a spinner of a list of indicators the user can overlay.
Now I want the graph to be redrawn with this new selection from the spinner.
I did try refreshing/reloading the page onItemSelected but that causes the page to keep refreshing even without waiting for a user input.



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 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);

}
}


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...