显示来自非活动Volley类的ProgressDialog

时间:2015-09-30 16:53:01

标签: java android android-volley progressdialog

我正在尝试在我的应用从Web服务获取数据时显示ProgressDialog。当DataController完成从解析Web服务添加对象时,我还想在MapsActivity(主活动)中调用一个方法。

在我的代码中没有任何事情发生(即没有出现ProgressDialog),我仍然坚持如何让MapsActivity知道对象已经创建。

正如您所看到的,我已尝试使用

显示ProgressDialog
pd.show(context, "text", "text")

MapsActivity.java

public class MapsActivity extends FragmentActivity {
    private DataController controller = new DataController(MapsActivity.this);

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        setUpMapIfNeeded();

        sharedPref = new SharedPreference(MapsActivity.this);
        storedLikes = controller.readLike(sharedPref);
        //This runs getLikedPos() in DataController

        /Lots of code....

DataController.Java

public class DataController {
// Fields

public DataController(MapsActivity mapsActivity) {
    this.context = mapsActivity;
}

public void getLikedPos() {

    this.url = getUrl();

    JsonObjectRequest jrequest = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    try {
                        ProgressDialog pd = ProgressDialog.show(context, "In progress", "Loading");
                        int success = response.getInt("success");

                        if (success == 1) {
                            JSONArray ja = response.getJSONArray("spots");

                            for (int i=0; i < ja.length(); i++) {
                                JSONObject spot = ja.getJSONObject(i);

                                Location location = new Location("");
                                location.setLatitude(spot.getDouble(LIKE_LAT));
                                location.setLongitude(spot.getDouble(LIKE_LONG));

                                int id = spot.getInt(LIKE_ID);

                                String time = spot.getString(LIKE_TIMESTAMP);
                                Calendar parsedTime = convertDate(time);
                                likeArr.add(new Like(location, id, parsedTime));
                            }
                            System.out.println("Size of LikeArr ArrayList after parsing: " + likeArr.size());
                            pd.dismiss();
                            //context.updateMap();   ??
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();    
                    }

                }
            }
            , new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });

0 个答案:

没有答案