如何在asyncTask中显示进度条中的不同消息

时间:2016-03-09 09:17:40

标签: android android-asynctask

在异步任务中运行时,我必须在进度对话框中显示不同的消息。

首先,我需要显示消息“Please wait”,然后“从服务器下载”,然后“请等待一段时间”。

我尝试使用publishProgress但是当我运行应用程序时,在我的ProgressDialog上,只显示最后一条消息“Please wait for sometime”。如何显示三条消息?

private class Sample extends AsyncTask<String, String, String> {
    ProgressDialog testdialog;

    @Override
    protected void onPreExecute() {
        testdialog = new ProgressDialog(test.this);
        testdialog.setTitle("Title");
        testdialog.setMessage("Please wait ");
        testdialog.setIndeterminate(false);
        testdialog.setCancelable(false);
        testdialog.setCanceledOnTouchOutside(false);
        testdialog.show();
    }

    @Override
    protected String doInBackground(String... urls) {
        publishProgress("Downloading from server");
        publishProgress("Please wait for sometime");
        /* here I code the background downloading process*/
    }

    @Override
    protected void onProgressUpdate(String... pro) {
        testdialog.setMessage(pro[0]);
        testdialog.setMessage(pro[1]);
    }

    @Override
    protected void onPostExecute(String result) {
        testdialog.dismiss();
    }
}

1 个答案:

答案 0 :(得分:2)

#include "stdafx.h" #include "database_con.h" //////////////////////////////////////////////////////////////////////// // Show errors from the SQLHANDLE void database_con::show_error(unsigned int handletype, const SQLHANDLE& handle) { SQLWCHAR sqlstate[1024]; SQLWCHAR message[1024]; if (SQL_SUCCESS == SQLGetDiagRec(handletype, handle, 1, sqlstate, NULL, message, 1024, NULL)) wcout << "Message: " << message << "\nSQLSTATE: " << sqlstate << endl; } //////////////////////////////////////////////////////////////////////// // Builds the stored procedure query. std::wstring database_con::buildQuery(vector<std::wstring> input) { std::cout << "Building the query" << std::endl; std::wstringstream builder; builder << L"EXEC sp_addHistorical " << "@Symbol='" << L"" << input.at(0) << "'," << "@Date=" << (wstring)L"" << input.at(1) << "," << "@Open=" << (wstring)L"" << input.at(2) << "," << "@Close=" << (wstring)L"" << input.at(3) << "," << "@MaxPrice=" << (wstring)L"" << input.at(4) << "," << "@MinPrice=" << (wstring)L"" << input.at(5) << "," << "@Volume=" << (wstring)L"" << input.at(6) << ";"; return builder.str(); } //////////////////////////////////////////////////////////////////////// // Adds a substring of the string before the delimiter to a vector<wstring> that is returned. std::vector<wstring> database_con::parseData(wstring line, char delim) { size_t pos = 0; std::vector<std::wstring> vOut; while ((pos = line.find(delim)) != std::string::npos) { vOut.push_back(line.substr(0, pos)); line.erase(0, pos + 2); } vOut.push_back(line.substr(0, pos)); return vOut; } std::wstring database_con::StringToWString(const std::string& s) { std::wstring temp(s.length(), L' '); std::copy(s.begin(), s.end(), temp.begin()); return temp; } database_con::database_con(std::string historical){ /* Set up the handlers */ /* Allocate an environment handle */ SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); /* We want ODBC 3 support */ SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0); /* Allocate a connection handle */ SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc); /* Connect to the DSN */ SQLDriverConnectW(dbc, NULL, L"DRIVER={SQL Server};SERVER=ERA-PC-STUART\\JBK_DB;DATABASE=master;UID=geo;PWD=kalle123;", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE); /* Check for success */ if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt)) { show_error(SQL_HANDLE_DBC, dbc); std::cout << "Failed to connect"; } std::wstringstream stream(StringToWString(historical)); std::wstring line; while (std::getline(stream, line)) { vector<wstring> vHistorical = parseData(L"" + line, ','); std::wstring SQL = buildQuery(vHistorical); if (SQL_SUCCESS != SQLExecDirectW(stmt, const_cast<SQLWCHAR*>(SQL.c_str()), SQL_NTS)) { show_error(SQL_HANDLE_STMT, stmt); } } } database_con::~database_con() { } 中尝试此代码,它应显示所有消息,每个消息延迟2秒 除了最后一个将保留在对话框中,直到对话框被解除或隐藏

doInBackground()

同样对于@Override protected String doInBackground(String... urls) { try {//this should let "Please wait " appears for 2 secs Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } publishProgress("Downloading from server"); try {////this should let "Downloading from server" appears for 2 secs Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } publishProgress("Please wait for sometime"); try {////this should let "Please wait for sometime" appears for 2 secs Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } /* here i code the background downloading process*/ } ,该方法准备接收多个参数,但您只发送一个,所以不需要使用pro [1],删除第二个onProgressUpdate()来电

setMessage()