AsyncTask线程的问题

发布于 2024-11-11 17:28:08 字数 1063 浏览 2 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫言歌 2024-11-18 17:28:08

假设您已按正确顺序获取数据值(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

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
酷到爆炸 2024-11-18 17:28:08

您可以将它们保留为 String 并将 String[] 数组传递给publishProgress(),保持相同的顺序,然后在publishProgress() 中将 viteza[i] 的值转换为浮点数。如果您不想担心数组顺序,您也可以将它们放入一个超级简单的类中并传递对象,但对于这么小的东西,我会使用数组。

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

}

}

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

}

}

月牙弯弯 2024-11-18 17:28:08

考虑一个包装这两个值的自定义对象,并将该对象作为 Progress 传递,如下所示:

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

JAL

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文