【问题标题】:AsyncTask not Update Progress on returnAsyncTask 在返回时不更新进度
【发布时间】:2013-01-31 14:58:32
【问题描述】:

我有一个 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;
}

}

这是我的主类:

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

我能做什么?

【问题讨论】:

  • 你为什么打电话给SelectTables.execute("").get();
  • 因为我需要填写我的 ArrayList...

标签: android android-asynctask android-progressbar


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    this.runOnUiThread(new Runnable() {
    
            public void run() {
                publishProgress("Your progress");
    
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      相关资源
      最近更新 更多