简体   繁体   English

json加载更多项目

[英]json load more items

I worked JSON. 我工作过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. 我在一年级有两个类(私有类LoadDataToServer扩展了AsyncTask,而类loadMoreListView扩展了AsyncTask),我解析JSON并在ListView中显示项目,第二个类,我试图加载多个项目。 my problem is when I call the new loadMoreListView (). 我的问题是当我调用新的loadMoreListView()时。 execute (); 执行 (); .. on listView.setOnScrollListener there is an error. ..在listView.setOnScrollListener上有一个错误。 my problem is adapter this is my code 我的问题是适配器,这是我的代码

private class loadMoreListView extends AsyncTask>> { 私有类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();
            }

        }
    }
}

private class LoadDataToServer extends AsyncTask>> { 私有类LoadDataToServer扩展了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() { 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. 看到我猜在您的网络呼叫(异步任务)的某个地方,您的“ itemList”在没有通知适配器的情况下得到更新。 because you are adding item in "doInBackGround" which is on different thread. 因为您要在不同线程的“ doInBackGround”中添加项目。

I would suggest you to use these two library for making REST network call and json parsing . 我建议您使用这两个库进行REST网络调用和json解析。 this is efficient in terms of memory and performance. 就内存和性能而言,这是高效的。

Android Asynchronous Http Client Android异步Http客户端

google-gson 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(); 当获取新数据时,将数据设置为适配器后,请调用adapter.notifyDataSetChanged();。 That will notify the list that the data in adapter is changed 这将通知列表适配器中的数据已更改

hope this will help 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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