Abdulkadir NURKALEM
Abdulkadir NURKALEM

Reputation: 749

AsyncTask not Update Progress on return

I have a AsyncTask class. I do process in AsyncTask class. But I return this class, Progress isn't update. And I only using class, progres is update.

This is my AsyncTask class:

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

}

This is my 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());
            }
        }
    });
}
}

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

What can I do ?

Upvotes: 0

Views: 487

Answers (2)

Munish Katoch
Munish Katoch

Reputation: 517

The Get function waits if necessary for the computation to complete, and then retrieves its result. That's means that your UI Thread is block till your task is completed. Since your are blocking your UI thread; progress bar, will never be shown.

Upvotes: 1

Nieldev
Nieldev

Reputation: 211

Try wrapping your publishProgress() methods in the UI Thread.

this.runOnUiThread(new Runnable() {

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

        }
    });

Upvotes: 0

Related Questions