首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >json加载更多项

json加载更多项
EN

Stack Overflow用户
提问于 2013-12-14 01:48:01
回答 2查看 184关注 0票数 0

我在JSON工作。我在一年级有两个类(私有类LoadDataToServer扩展了AsyncTask,loadMoreListView类扩展了AsyncTask),我在ListView中解析JSON并显示项,第二个类,我试图加载多个项。我的问题是当我调用新的loadMoreListView ()时。execute ();..在listView.setOnScrollListener上出现错误。我的问题是适配器这是我的代码

私有类loadMoreListView扩展了AsyncTask>> {

代码语言:javascript
复制
    @Override
    protected void onPreExecute() {
        pd.show();
    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
            Void... params) {

        jsonparser = new JSONParser();

        URL = "http://bri.ge/api/getList.aspx?count=2&time=" + dateTime;
        jsonarray = new JSONArray();

        JSONObject jsonobject = jsonparser.getJSONfromURL(URL);

        try {

            jsonarray = jsonobject.getJSONArray("data");

            for (int i = 0; i < jsonarray.length(); i++) {

                jsonobject = jsonarray.getJSONObject(i);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put("journal", jsonobject.getString(KEY_journal));
                map.put("image", jsonobject.getString(KEY_image));
                map.put("title", jsonobject.getString(KEY_title));
                map.put("description",
                        jsonobject.getString(KEY_description));
                map.put("JournalID", KEY_JournalID);

                map.put("pubDate", jsonobject.getString(KEY_pubDate));

                Content cont = new Content(jsonobject.getString("journal"),
                        jsonobject.getString("image"),
                        jsonobject.getString("title"),
                        jsonobject.getString("pubDate"),
                        jsonobject.getString("description"),
                        jsonobject.getInt("JournalID"));

                contents.add(cont);

                itemList.add(map);

            }
            // adapter.notifyDataSetChanged();
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();

        }
        dateTime = itemList.get(itemList.size() - 1).get(KEY_pubDate);
        return itemList;
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        super.onPostExecute(result);
        if (pd.isShowing()) {
            pd.dismiss();
            try {
                // itemList.clear();

                int currentPosition = list.getFirstVisiblePosition();

                // Appending new data to menuItems ArrayList

                adapter = new LazyAdapter(MainActivity.this, itemList);
                adapter.notifyDataSetChanged();
                list.setAdapter(adapter);
                // list.add

                // Setting new scroll position
                list.setSelectionFromTop(currentPosition + 1, 0);

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

        }
    }
}

私有类LoadDataToServer扩展了AsyncTask>> {

代码语言:javascript
复制
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd.show();

    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
            Void... params) {

        jsonparser = new JSONParser();

        @SuppressWarnings("static-access")
        JSONObject jsonobject = jsonparser.getJSONfromURL(URL);
        try {

            jsonarray = jsonobject.getJSONArray("data");

            for (int i = 0; i < jsonarray.length(); i++) {

                jsonobject = jsonarray.getJSONObject(i);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put("journal", jsonobject.getString(KEY_journal));
                map.put("image", jsonobject.getString(KEY_image));
                map.put("title", jsonobject.getString(KEY_title));
                map.put("description",
                        jsonobject.getString(KEY_description));
                map.put("JournalID", KEY_JournalID);
                map.put("pubDate", jsonobject.getString(KEY_pubDate));

                // contents = new ArrayList<Content>();

                Content cont = new Content(jsonobject.getString("journal"),
                        jsonobject.getString("image"),
                        jsonobject.getString("title"),
                        jsonobject.getString("pubDate"),
                        jsonobject.getString("description"),
                        jsonobject.getInt("JournalID"));

                contents.add(cont);

                itemList.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        dateTime = itemList.get(itemList.size() - 1).get(KEY_pubDate);
        return itemList;

    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        super.onPostExecute(result);

        pd.dismiss();

        // itemList.clear();

        // Appending new data to menuItems ArrayList

        adapter = new LazyAdapter(MainActivity.this, itemList);
        adapter.notifyDataSetChanged();
        list.setAdapter(adapter);
        // list.add

        // Setting new scroll position

    }

}

新建(list.setOnScrollListener OnScrollListener() {

代码语言:javascript
复制
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (scrollState == 0) {
                // new loadMoreListView().execute();

            }

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            final int lastItem = firstVisibleItem + visibleItemCount;
            if (lastItem == totalItemCount) {
                new loadMoreListView().execute();
            }

        }
    });
EN

回答 2

Stack Overflow用户

发布于 2013-12-14 02:20:44

我猜在你的网络调用(异步任务)中的某个地方,你的"itemList“在没有通知适配器的情况下得到了更新。因为您在不同线程上的"doInBackGround“中添加了项。

我建议您使用这两个库进行REST网络调用和json解析。这在内存和性能方面是高效的。

Android Asynchronous Http Client

google-gson

这些都是开源的。如有任何疑问,请向我咨询。

票数 0
EN

Stack Overflow用户

发布于 2013-12-16 18:19:30

当您获得新数据时,在将数据设置为适配器后,请调用adapter.notifyDataSetChanged();这将通知列表适配器中的数据已更改

希望这能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20572822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档