简体   繁体   中英

json load more items

I worked JSON. I have two classes (private class LoadDataToServer extends AsyncTask and class loadMoreListView extends AsyncTask) in the first grade, I parsing JSON and show item in the ListView and the second class, I'm trying to load multiple items. my problem is when I call the new loadMoreListView (). execute (); .. on listView.setOnScrollListener there is an error. my problem is adapter this is my code

private class loadMoreListView extends 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();
            }

        }
    }
}

private class LoadDataToServer extends AsyncTask>> {

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

        }
    });

see i guess somewhere in your network call (Async task) your "itemList" get updated without notifying adapter. because you are adding item in "doInBackGround" which is on different thread.

I would suggest you to use these two library for making REST network call and json parsing . this is efficient in terms of memory and performance.

Android Asynchronous Http Client

google-gson

these are open source . for any query ask me.

When you get new data then after setting data to adapter please call adapter.notifyDataSetChanged(); That will notify the list that the data in adapter is changed

hope this will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM