I'm new to android and now I want to write an application which show an array of 8000 double which is sin and cos signal on a chart, every 7 seconds sin chart will change to cos and vice versa. I used GraphView class to draw the chart. but I can't change the chart every 7 second and it draw it just for first time and it won't change any more. the on create method is as follow :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.text);
text2 = (TextView)findViewById(R.id.text2);
Handler handler = new Handler();
GraphView graphView = new LineGraphView(
this
, "Chart"
);
GraphViewData [] data= new GraphViewData [8000];
for (int i = 0; i < data.length; i++) {
data [i] = new GraphViewData(i,Math.sin(i*Math.PI/20.0)*40+50);}
GraphViewSeries graphViewSeries = new GraphViewSeries(data);
graphView.setManualYAxisBounds(130, 0);
graphView.setScrollable(true);
graphView.setScalable(true);
graphView.getGraphViewStyle().setGridStyle(GridStyle.BOTH);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
graphView.getGraphViewStyle().setTextSize(11);
graphView.addSeries(graphViewSeries);
final GraphView graphView2 = graphView;
final LinearLayout layout2=layout;
runOnUiThread(new Runnable(){
@Override
public void run(){
layout2.removeAllViews();
layout2.addView(graphView2);
}
});
for (int count = 0; count < 10; count++){
final int count2=count;
if(count2%2==0){
for (int i = 0; i < data.length; i++) {
data [i] = new GraphViewData(i,Math.sin(i*Math.PI/20.0)*20+50);
}
}
else{
for (int i = 0; i < data.length; i++) {
data [i] = new GraphViewData(i,Math.cos(i*Math.PI/20.0)*45+50);
}
}
graphView.removeAllSeries();
graphViewSeries.resetData(data);
graphView.addSeries(graphViewSeries);
final GraphView graphView3=graphView;
final LinearLayout layout3=layout;
handler.postDelayed(new Runnable(){
@Override
public void run() {
text2.setText(s);
graphView3.redrawAll();
runOnUiThread(new Runnable(){
@Override
public void run(){
layout3.removeAllViews();
layout3.addView(graphView3);
}
});
}
}, 7000* (count + 1) );
}
}
I can't recognise what is it's problem. One way is that I create a new GraphView instance in the loop which should be final but I don't want to redraw all my chart every time because every time it draw it first it move to right for a second and then expand and place in the proper position. I mean I just want to redraw chart every 7 second