0

我想在 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 生成一个额外的文件,setProgressBarIndeterminateVisibility 是未定义的 GroupPageTask...

如何在单独的 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);
    }
}
4

2 回答 2

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

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

GroupPageTask groupPageTask = new GroupPageTask(this);
于 2012-11-09T07:14:08.693 回答
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);
    }
}
于 2012-11-10T09:13:27.800 回答