从嵌套事件侦听器

时间:2015-04-26 05:09:32

标签: android android-asynctask

以下是我要做的事情:从远程服务器获取JSON数组,并在弹出窗口中的项目列表中选择项目时,在列表视图中(在新活动中)呈现它。单击/点击活动中的元素时会创建弹出窗口。更具体地说:活动在网格视图中显示类别列表。每个类别都有一堆子类别,当用户单击某个类别时,这些子类别将显示为弹出窗口中的项目列表。当用户选择弹出窗口中的一个项目时,将创建一个新活动,该活动显示该子类别中的项目列表。项目详细信息作为JSON数组从远程服务器获取。 当从弹出窗口中选择子类别时,我创建了一个AsyncTask来从服务器获取项目详细信息。以下是此代码段。

public class Section extends Fragment {
    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ...

        mGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView <? > parent, final View v, nt position, long id) {
                ...

                PopupMenu popup = new PopupMenu(getActivity(), v);

                ...

                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        new GetProductDataFromServer_Task().execute(productURL);
                        Intent intent = new Intent(getActivity(), ProductListScreen.class);
                        startActivity(intent);

                        return true;
                    }
                });
            });
        }

        return rootView;
    }
}

Fragment是用于在网格布局中显示类别列表的活动的一部分。

而且,这是从服务器获取JSONArray的AsyncTask。

private class GetProductDataFromServer_Task extends AsyncTask < String, Long, JSONObject > {

    @Override
    protected JSONObject doInBackground(String...params) {
        HttpEntity httpEntity = null;

        HttpGet httpGet = null;
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            httpGet = new HttpGet(params[0]);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            httpEntity = httpResponse.getEntity();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Convert HttpEntity into JSON Array
        JSONObject jsonObject = null;

        if (httpEntity != null) {
          try {
                String entityResponse = EntityUtils.toString(httpEntity);
                jsonObject = new JSONObject(entityResponse);
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {
        try {
            JSONArray products = jsonObject.getJSONArray("products");
            int numProducts = products.length();
            mProductList = new ProductInfoForGridView[numProducts];
            for (int prodIdx = 0; prodIdx < numProducts; prodIdx++) {
                mProductList[prodIdx] = GetProductObjFromJSONObj(products.getJSONObject(prodIdx));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

我面临的问题是onPostExecute()由于某种原因没有被调用。 JSONArray成功从服务器获取,doInBackground()函数完成其执行,没有任何异常。我怀疑这与嵌套事件侦听器有关,因为如果我从类别侦听器启动Async任务(即onItemClick() - 调用嵌套子类别侦听器的函数setOnMenuItemClickListener()),它就可以正常工作。

提前感谢您的回答。我试图在我的问题中描述。如果需要,我很乐意提供更多细节。

1 个答案:

答案 0 :(得分:0)

您确定onPostExecute没有运行。您是否在yourGridAdapter.notifyDataSetChanged()

中尝试了
onPostExecute