我正在从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()只能带一个参数!!!!
有人可以帮我这个吗?Thx
答案 0 :(得分:0)
你可以将它们都保留为String并将String []的数组传递给publishProgress(),保持相同的顺序,然后将viteza [i]的值转换为publishProgress()中的float。您也可以将它们放入一个超级简单的类中,如果您不想担心数组顺序,则传递该对象,但是我会使用数组来寻找这么小的东西。
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyAsyncTask myTask = new MyAsyncTask();
myTask.execute();
}
private class MyAsyncTask extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
String[] first = {"1", "2", "3", "4", "5"};
String[] second = {"5", "4", "3", "2", "1"};
for(int i = 0; i < first.length; i++) {
//put together a string array with our values in the desired order
String[] vals = {first[i], second[i]};
//publishProgress is what is used to call onProgressUpdate()
publishProgress(vals);
}
return null;
}
@Override
protected void onProgressUpdate(String...strings ) {
//Strings are now in strings[0] and strings[1] in the order
//that they were put into vals array in doInBackground()
String passedValues = strings[0] + "," + strings[1];
//Do whatever UI updates.
TextView mytext = (TextView)findViewById(R.id.mytext);
mytext.setText(passedValues);
}
}
}
答案 1 :(得分:0)
假设您的数据值正确(getString(3)应为数字且getString(4)应为时间值),以下情况应该有效。
protected Void doInBackground(DBAdapter... db) {
// ... some db work
if (c.moveToFirst()) {
publishProgress(c.getString(3), c.getString(4));
// ...
}
}
protected void onProgressUpdate(String... values) {
float viteza = Float.parseFloat(values[0]);
String time = values[1];
// do something with the data that shows in UI
}
测试传递值的完整AsyncTask在这里:
private class SimpleTask extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... params) {
String floatAsString = "3.14159265358979";
String timeAsString = "06-01 07:32:02.579";
publishProgress(floatAsString, timeAsString);
return null;
}
@Override
protected void onProgressUpdate(String... values) {
float pi = Float.parseFloat(values[0]);
Log.i("SimpleTask", String.format("Got values '%s' and '%s'. pi = %f", values[0], values[1], pi));
}
}
打印输出:
Got values '3.14159265358979' and '06-01 07:32:02.579'. pi = 3.141593
答案 2 :(得分:0)
考虑一个自定义对象,它包装这两个值并将此对象作为Progress传递给:
private class MyAsynch extends AsyncTask<Void,MyObject,Void>{
JAL