返回时,AsyncTask不会更新进度

时间:2013-01-31 14:58:33

标签: android android-asynctask android-progressbar

我有一个AsyncTask类。我在AsyncTask类中进行处理。但我返回这个课程,进展不是更新。我只使用课程,progres是更新。

这是我的AsyncTask类:

AsyncTaskTables.java

public class AsyncTaskTables extends AsyncTask<String, String, ArrayList<String>>{

private Context Context;

private LoginInfo AccountInfo;

private TextView ProgressStatus;
private ProgressBar ProgressValue;

public AsyncTaskTables (Context Context){
    this.Context = Context;

    AccountInfo = new LoginInfo();

    ProgressStatus = (TextView) ((Activity)Context).findViewById(R.id.tvStatus);
    ProgressValue = (ProgressBar) ((Activity)Context).findViewById(R.id.pbProgress);
}

@Override
protected void onPreExecute() {
    ProgressValue.setIndeterminate(true);
}

@Override
protected ArrayList<String> doInBackground(String... SQLCommand) {
    publishProgress("Checking Net Connection...");
    if(NetConnection()){
        try{
            publishProgress("Preparing...");

            ArrayList<String> TablesList = new ArrayList<String>();

            publishProgress("Connecting...");

            @SuppressWarnings("static-access")
            String Url = "jdbc:jtds:sqlserver://" + AccountInfo.Server + ";databaseName=" + AccountInfo.Database + "";
            String Driver = "net.sourceforge.jtds.jdbc.Driver";

            String SelectTables = "SELECT * FROM INFORMATION_SCHEMA.TABLES";

            Class.forName(Driver).newInstance();
            @SuppressWarnings("static-access")
            Connection DatabaseConnection = DriverManager.getConnection(Url, AccountInfo.UserName, AccountInfo.Password);
            Statement Command = DatabaseConnection.createStatement();

            publishProgress("Selecting tables...");
            ResultSet Results = Command.executeQuery(SelectTables);

            TablesList.clear();

            while(Results.next()){
                TablesList.add(Results.getString(3).toString());
            }

            publishProgress("Done !");

            return TablesList;
        }catch(Exception Error){
            publishProgress("Error : " + Error.toString());
            return null;
        }
    } else {
        publishProgress("Need Network Connection !");
        return null;
    }
}

@Override
protected void onProgressUpdate(String... Status) {
    ProgressStatus.setText(Status[0]);
}

@Override
protected void onPostExecute(ArrayList<String> Result) {
    ProgressValue.setIndeterminate(false);
}

@SuppressWarnings("static-access")
private boolean NetConnection() {
     ConnectivityManager NetManager = (ConnectivityManager) ((Activity)Context).getSystemService(Context.CONNECTIVITY_SERVICE);

     if (NetManager.getActiveNetworkInfo() != null && NetManager.getActiveNetworkInfo().isAvailable() && NetManager.getActiveNetworkInfo().isConnected())
         return true;
     else
         return false;
}

}

这是我的MainClass:

ManagementActivity.java

public class ManagementActivity extends Activity {

private TextView Status;

private AsyncTaskTables SelectTables;

private TablesAdapter TablesAdapter = null;
private ArrayList<String> TablesList = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_management);

    Status = (TextView) findViewById(R.id.textView1);

    try {

//Problem is here...
//I am calling only "SelectTables.execute("");", progressbar is showing.
//But I am calling "SelectTables.execute("").get();", progressbar isn't showing...

        SelectTables = new AsyncTaskTables(this);
        TablesList = SelectTables.execute("").get();
        TablesAdapter = new TablesAdapter (this, R.layout.tables_row, TablesList);

        ListView Tables = (ListView) findViewById(R.id.lvTables);
        Tables.setAdapter(TablesAdapter);
        registerForContextMenu(Tables);
    } catch (Exception Error) {
        Status.setText("Error (Select Tables) : " + Error.toString());
    }

    Button btnTablesRefresh = (Button) findViewById(R.id.btnRefresh);
    btnTablesRefresh.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            try {
                SelectTables = new AsyncTaskTables(ManagementActivity.this);
                TablesList = SelectTables.execute("").get();
                TablesAdapter = new TablesAdapter (ManagementActivity.this, R.layout.tables_row, TablesList);

                ListView Tables = (ListView) findViewById(R.id.lvTables);
                Tables.setAdapter(TablesAdapter);
                registerForContextMenu(Tables);
            } catch (Exception Error) {
                Status.setText("Error (Select Tables) : " + Error.toString());
            }
        }
    });
}
}

我只调用“SelectTables.execute(”“);”,进度条显示。 但我打电话给“SelectTables.execute(”“)。get();”,进度条没有显示......

我该怎么办?

2 个答案:

答案 0 :(得分:1)

如果需要,Get函数会等待计算完成,然后检索其结果。这意味着你的UI线程是阻塞的,直到你的任务完成。因为你正在阻止你的UI线程;进度条,永远不会显示。

答案 1 :(得分:0)

尝试在UI线程中包装您的publishProgress()方法。

this.runOnUiThread(new Runnable() {

        public void run() {
            publishProgress("Your progress");

        }
    });