ActionBarSherlock从外部AsyncTask更改ProgressBar

时间:2012-11-08 05:53:36

标签: android actionbarsherlock

我想在AsyncTask中设置ProgressVisibility(true)。是Main中的AsyncTask,一切都很好。

public class GlanceActivity extends SherlockActivity implements ActionBar.OnNavigationListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    //This has to be called before setContentView and you must use the
        //class in com.actionbarsherlock.view and NOT android.view
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_glance);
    }

    public class TestTask extends AsyncTask<String, String, ArrayList<String>> {
        @Override
        public void onPreExecute() {
            // Show IndeterminateProgressBar
            setSupportProgressBarIndeterminateVisibility(true);
        }

        @Override
        protected ArrayList<String> doInBackground(String... params) {
            // Load some Data...
            return null;
        }

        protected void onPostExecute(ArrayList<String> arg) {
            // Hide IndeterminateProgressBar
            setProgressBarIndeterminateVisibility(false);
        }
    }
}

但是,如果我想为AsyncTask生成额外文件,则为GroupPageTask未定义setProgressBarIndeterminateVisibility ...

如何在单独的AsyncTask文件中使用此方法?

GlanceActivity.java

public class GlanceActivity extends SherlockActivity implements ActionBar.OnNavigationListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //This has to be called before setContentView and you must use the
        //class in com.actionbarsherlock.view and NOT android.view
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_glance);
    }
}

GroupPageTask.java

public class GroupPageTask extends AsyncTask<String, String, ArrayList<String>> {
    @Override
    public void onPreExecute() {
        // Show IndeterminateProgressBar
        setSupportProgressBarIndeterminateVisibility(true);
    }

    @Override
    protected ArrayList<String> doInBackground(String... params) {
        // Load some Data...
        return null;
    }

    protected void onPostExecute(ArrayList<String> arg) {
        // Hide IndeterminateProgressBar
        setProgressBarIndeterminateVisibility(false);
    }
}

2 个答案:

答案 0 :(得分:3)

您可以在AsyncTask构造期间移交上下文:

public class GroupPageTask extends AsyncTask<String, String, ArrayList<String>> 
{
  private Context context;

  public AsyncTask(Context context) 
  {
    this.context = context;
  }

  protected void onPreExecute() 
  {
    ((Activity) context).setProgressBarIndeterminateVisibility(true);
  }

  protected void onPostExecute(ArrayList<String> arg) 
  {
    ((Activity) context).setProgressBarIndeterminateVisibility(false);
  }
}

然后使用活动中的新构造函数创建AsyncTask:

GroupPageTask groupPageTask = new GroupPageTask(this);

答案 1 :(得分:0)

通过Matt Handys的回答,我找到了正确的方法......

GlanceActivity.java

public class GlanceActivity extends SherlockActivity implements ActionBar.OnNavigationListener {
    public static Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        GroupPageTask groupPageTask = new GroupPageTask(getSherlock());
    }
}

GroupPageTask.java

public class GroupPageTask extends AsyncTask<String, String, ArrayList<Mannschaft>> {
    private ActionBarSherlock sherlock;

    @Override
    public void onPreExecute() {
        // Show IndeterminateProgressBar
        sherlock.setProgressBarIndeterminateVisibility(true);
    }

    protected void onPostExecute(ArrayList<Mannschaft> arg) {
        sherlock.setProgressBarIndeterminateVisibility(false);
    }
}