Reputation: 4594
I'm using a chart taken from here http://writerbay.wordpress.com/2011/03/12/android-tutorial-displaying-chart-on-android/#comment-54
to draw a graph in android. The data used for drawing the graph is taken from a DB. On x axis I put the data and on the y-axis I put some numbers meaning the speed.
The data from DB is readed using Async thread.
What I wanna do is the following:
*read four values for data and four values for speed and put them on the chart
*reading the following four values and update my chart....until the data from the DB is readed
The problem I'm facing is that:
LineView lv; my chart has a constructor that accepts as parameters only arrays[]
So I have to pass it the array containing the whole data....which is against what I want to do.
public class InitTask extends AsyncTask<DBAdapter,String, Void> {
String TABLE_3;
protected Void doInBackground(DBAdapter... db) {
try {
db[0].createDatabase();
db[0].openDataBase();
Cursor c = db[0].getCursor3(db[0].TABLE_3, user_id);
viteza = new float[4];
time = new String[4];
if (c.moveToFirst()) {
do {
int a=(int)Double.parseDouble(c.getString(3));
publishProgress(Integer.toString(a),c.getString(4));
Thread.sleep(500);
} while (c.moveToNext());
}
c.close();
db[0].close();
} catch (Exception e) {
Log.d("Eroare", "doInBackground", e);
}
return null;
}
protected void onProgressUpdate(String...values) {
Aitem items[] = new Aitem[1];
viteza[contor]=Float.parseFloat(values[0]);
time[contor]=values[1];
items[0]=new Aitem(Color.RED, "Evolution",viteza);
lv.setAxisValueX(time);
lv.setItems(items);
setContentView(lv);
}
}
time is an array-containing my data which I put on the x-axis it must be a String
viteza-is for speed and is also an array which contains float numbers.
This is how I obtaing the graph:
items[0]=new Aitem(Color.RED, "Evolution",viteza);
lv.setItems(items);
The problem is that the Aitem() constructor needs arrays[] String as parameters....and I cannot find a way to update that array[] with the new values(four vnew values step by step)
forgetting about the old ones..
So at an moment in time only four values are displayed on my graph....
The problem is that I don't know how to do that...
Has anyone any clue of how could I do that...Thank u!
EDIT:initial value for contor=0
Upvotes: 2
Views: 2335
Reputation: 25060
You have two options that I can see. The first would be easier to implement but harder on performance and that is to wife the display and draw the graph all over again reading in as many as you want. The second option would be to read in 4 points and draw those and then read in the next 4 and draw those in a new line and so on.
-= EDIT =-
public class InitTask extends AsyncTask<DBAdapter, Double, Void>
{
String TABLE_3;
protected Void doInBackground(DBAdapter... db)
{
try
{
// Like before
do
{
double x1 = Double.parseDouble(c.getString(1));
double y1 = Double.parseDouble(c.getString(2));
double x2 = Double.parseDouble(c.getString(3));
double y2 = Double.parseDouble(c.getString(4));
double x3 = Double.parseDouble(c.getString(5));
double y3 = Double.parseDouble(c.getString(6));
double x4 = Double.parseDouble(c.getString(7));
double y4 = Double.parseDouble(c.getString(8));
publishProgress(x1, y1, x2, y2, x3, y3, x4, y4);
Thread.sleep(500);
} while (c.moveToNext());
// Like before
return;
}
protected void onProgressUpdate(Double...values)
{
double x1 = values[0];
double y1 = values[1];
double x2 = values[2];
double y2 = values[3];
double x3 = values[4];
double y3 = values[5];
double x4 = values[6];
double y4 = values[7];
}
Upvotes: 2