开发者

problem with AsyncTask thread

开发者 https://www.devze.com 2023-03-10 05:24 出处:网络
I\'m reading some data from a DB in an AsyncTask thread.... in this way: prot开发者_如何学Goected Void doInBackground(DBAdapter... db) {

I'm reading some data from a DB in an AsyncTask thread.... in this way:

    prot开发者_如何学Goected 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


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).

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
}

A full AsyncTask to test passing values is here:

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));
    }

}

which prints the output:

Got values '3.14159265358979' and '06-01 07:32:02.579'. pi = 3.141593


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.

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);
    }

}

}


Consider a custom object that wraps the two values and pass this object as Progress as in:

private class MyAsynch extends AsyncTask<Void,MyObject,Void>{

JAL

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号