json加载更多项目

时间:2013-12-13 17:48:01

标签: android json android-listview

我工作过JSON。我有两个类(私有类LoadDataToServer扩展AsyncTask和类loadMoreListView扩展AsyncTask) 在一年级,我解析了ListView和第二个类中的JSON和show项,我正在尝试加载多个项目。 我的问题是当我调用新的loadMoreListView()时。执行 (); ..在listView.setOnScrollListener上有一个错误。 我的问题是适配器 这是我的代码

私有类loadMoreListView扩展             的AsyncTask>> {

    @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&GT;&GT; {

    @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(new OnScrollListener(){

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

        }
    });

2 个答案:

答案 0 :(得分:0)

看到我想你的网络电话(异步任务)中的某个地方你的“itemList”在没有通知适配器的情况下得到更新。因为你在“doInBackGround”中添加了不同主题的项目。

我建议你使用这两个库进行REST网络调用和json解析。 这在内存和性能方面都很有效。

Android Asynchronous Http Client

google-gson

这些都是开源的。对于任何询问问我。

答案 1 :(得分:0)

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

希望这会有所帮助