簡體   English   中英

json加載更多項目

[英]json load more items

我工作過JSON。 我在一年級有兩個類(私有類LoadDataToServer擴展了AsyncTask,而類loadMoreListView擴展了AsyncTask),我解析JSON並在ListView中顯示項目,第二個類,我試圖加載多個項目。 我的問題是當我調用新的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 >> {

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

        }
    });

看到我猜在您的網絡呼叫(異步任務)的某個地方,您的“ itemList”在沒有通知適配器的情況下得到更新。 因為您要在不同線程的“ doInBackGround”中添加項目。

我建議您使用這兩個庫進行REST網絡調用和json解析。 就內存和性能而言,這是高效的。

Android異步Http客戶端

Google-gson

這些都是開源的。 如有任何疑問,請問我。

當獲取新數據時,將數據設置為適配器后,請調用adapter.notifyDataSetChanged();。 這將通知列表適配器中的數據已更改

希望這會有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM