I'm trying to display a button, a TextView area and a real time graph (using the aChartEngine library) on the same activity.
The user enters some information on the TextView, then click on the button to send it (through a OnClickListener()
). Then this listener creates a line graph and calls a web service which will return a value. Every 5 seconds the returned value is refreshed and the graph is updated.
The problem is that I can't manage to display the graph on the MainActivity(with the button and the TextView). How can I display the three of them in the same Activity?
I tried using this but it didn't work:
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
chartView = ChartFactory.getTimeChartView(getBaseContext(), dataset, renderer, "Test");
layout.addView(chartView);
And here is my code so far:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button)this.findViewById(R.id.send);
textArea = (EditText) this.findViewById(R.id.textarea);
send.setOnClickListener(sendForm);
}
@Override
protected void onStart() {
super.onStart();
dataset.addSeries(timeSeries);
chartView = ChartFactory.getTimeChartView(this, dataset, renderer, "Test");
chartView.refreshDrawableState();
chartView.repaint();
setContentView(chartView);
}
OnClickListener sendForm = new OnClickListener() {
@Override
public void onClick(View v) {
webServiceParameter = textArea.getText().toString().trim();
dataset = new XYMultipleSeriesDataset();
renderer = new XYMultipleSeriesRenderer();
myChartSettings(renderer);
rendererSeries = new XYSeriesRenderer();
myPointSettings(rendererSeries);
renderer.addSeriesRenderer(rendererSeries);
timeSeries = new TimeSeries("");
mThread = new Thread(){
public void run(){
while(true){
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
//call the web service and return a value
timeSeries.add(new Date(), returnedValue);
chartView.repaint();
}
}
};
mThread.start();
}
};
EDIT: Problem solved, I started from scratch and deleted the onStart() function. Then on the onCreate() I just had to write this:
dataset = new XYMultipleSeriesDataset();
renderer = new XYMultipleSeriesRenderer();
rendererSeries = new XYSeriesRenderer();
myChartSettings(renderer);
myPointSettings(rendererSeries);
renderer.addSeriesRenderer(rendererSeries);
timeSeries = new TimeSeries("");
dataset.addSeries(timeSeries);
chartView = ChartFactory.getTimeChartView(this, dataset, renderer, "Test");
chartView.refreshDrawableState();
chartView.repaint();
layout.addView(chartView);