0

我有一个 AsyncTask 类。我在 AsyncTask 类中进行处理。但是我返回这个类,进度没有更新。而且我只使用类,进度是更新。

这是我的 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;
}

}

这是我的主类:

管理活动.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();”,进度条没有显示...

我能做些什么 ?

4

2 回答 2

1

Get 函数在必要时等待计算完成,然后检索其结果。这意味着您的 UI 线程在您的任务完成之前被阻塞。由于您正在阻止您的 UI 线程;进度条,永远不会显示。

于 2013-01-31T15:31:38.823 回答
0

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

this.runOnUiThread(new Runnable() {

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

        }
    });
于 2013-01-31T15:27:37.287 回答