AsyncTask线程的问题
我正在 AsyncTask 线程中从数据库读取一些数据...... 这样:
protected Void doInBackground(DBAdapter... db) {
try {
db[0].openDataBase();
Cursor c = db[0].getCursor3(db[0].TABLE_3, user_id);
float[] viteza = new float[c.getCount()];
String[] time = new String[c.getCount()];
if (c.moveToFirst()) {
do {
viteza[i] = Float.parseFloat(c.getString(3));
time[i] = c.getString(4);
publishProgress(????);
Thread.sleep(2500);
i++;
} while (c.moveToNext());
}
c.close();
db[0].close();
} catch (Exception e) {
Log.d("Eroare", "doInBackground", e);
}
return null;
}
我的问题是,每次我获得 viteza[i] 和 time[i] 的新值时,我必须
protected void onProgressUpdate(....?) {
}
同时将它们发送给两者......但我不知道该怎么做因为publishProgress()只能接受一个参数!!!!
有人可以帮我吗?谢谢
I'm reading some data from a DB in an AsyncTask thread....
in this way:
protected Void doInBackground(DBAdapter... db) {
try {
db[0].openDataBase();
Cursor c = db[0].getCursor3(db[0].TABLE_3, user_id);
float[] viteza = new float[c.getCount()];
String[] time = new String[c.getCount()];
if (c.moveToFirst()) {
do {
viteza[i] = Float.parseFloat(c.getString(3));
time[i] = c.getString(4);
publishProgress(????);
Thread.sleep(2500);
i++;
} while (c.moveToNext());
}
c.close();
db[0].close();
} catch (Exception e) {
Log.d("Eroare", "doInBackground", e);
}
return null;
}
My problem is that each time I get a new value for viteza[i] and time[i] I have to send them to
protected void onProgressUpdate(....?) {
}
both at the same time....but I don't know how to do that cause publishProgress() can take only one parameter!!!!
Can someone help me with this?Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您已按正确顺序获取数据值(getString(3) 应该是数字,getString(4) 应该是时间值),以下内容应该可以工作。
用于测试传递值的完整 AsyncTask 位于:
打印输出:
The following should work assuming you've got the data values in right order (getString(3) should be a number and getString(4) should be a time value).
A full AsyncTask to test passing values is here:
which prints the output:
您可以将它们保留为 String 并将 String[] 数组传递给publishProgress(),保持相同的顺序,然后在publishProgress() 中将 viteza[i] 的值转换为浮点数。如果您不想担心数组顺序,您也可以将它们放入一个超级简单的类中并传递对象,但对于这么小的东西,我会使用数组。
}
You could leave them both String and pass an array of String[] to publishProgress(), maintaining the same order, and then convert viteza[i]'s value to a float in publishProgress(). You could also put them into a super simple class and pass the object if you don't want to worry about the array order, but I would go with the array for something this small.
}
考虑一个包装这两个值的自定义对象,并将该对象作为 Progress 传递,如下所示:
JAL
Consider a custom object that wraps the two values and pass this object as Progress as in:
JAL