ProgressDialog不会出现在OnPreExecute中

时间:2015-02-10 14:13:11

标签: android android-fragments android-asynctask progressdialog android-progressbar

我正在尝试在解析JSON对象期间创建ProgressDialog。所以我正在使用AsyncTask

    private class JsonUtility extends AsyncTask<String, Void, String> {

    //getting activity context like that since outer activity is not enclosing
    public JsonUtility(Activity activity){
        super();
        mActivity = activity;

    }

    public JsonUtility(){}

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        dialog = ProgressDialog.show(mActivity, "Loading...", "Please wait...", true);
    }

    @Override
    protected String doInBackground(String... params) {

//...operate with JSON

    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        dialog.hide();
    }
}

我正从JSON那里获取信息:

    public ArrayList<CoursesData> getCourses(Activity activity){
    ArrayList<CoursesData> resultArrayList = new ArrayList<>();
    try {
        JSONArray jArray = new JSONArray(new JsonUtility(activity).execute(coursesUrl).get());
//...and so on
}
当使用JsonUtility

创建Fragment时,

活动将传递到getActivity()

ArrayList<CoursesData> coursesDatas = parserJson.getCourses(getActivity());

是从Fragment传递活动上下文的正确方法吗?

或者问题可能在于其他地方?虽然加载大约需要2-3秒,但ProgressDialog似乎不会出现。

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

将mActivity更改为getParent()...它为我工作