1

您好,我的 android 应用程序中有一个列表视图。当我的应用程序打开时,我加载它,如果用户滚动到列表列表视图的末尾,它会完美地加载。但是在第二个滚动中它不会自行加载。这是我的代码:

lstinteract.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub
            int lastInScreen = firstVisibleItem + visibleItemCount;    
               if((lastInScreen == totalItemCount) && !(loadingMore2)){   
                   loadingMore2=true;

                   GetFacebookInitDatas fb = new GetFacebookInitDatas();
                   fb.execute();

                    loadingMore2=false;


               }
        }
    });



    private class GetFacebookInitDatas extends AsyncTask<String, Void, Void>
{

    @Override
    protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub

        final ArrayList<String> interactsenders = new ArrayList<String>();
        final ArrayList<String> interactposts = new ArrayList<String>();
        final ArrayList<String> interactimages = new ArrayList<String>();
        final ArrayList<String> interactlinks = new ArrayList<String>();

        try {
            inprevpage = intlinks.getString("previous");
            innextpage = intlinks.getString("next");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {

            System.out.println(innextpage);
            URL url = new URL(innextpage);
            URLConnection connection = url.openConnection();
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            int responseCode = httpConnection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream in = httpConnection.getInputStream();

                BufferedReader rd = new BufferedReader(new InputStreamReader(
                        in, Charset.forName("UTF-8")));
                String jsonText = readAll(rd);
                JSONObject data = new JSONObject(jsonText);

                if (data != null) {
                    JSONArray duyurular = null;
                    try {
                        duyurular = data.getJSONArray("data");
                        intlinks = data.getJSONObject("paging");
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    String hocaid = null;
                    String postid=null;
                    String sender = null;
                    String mesaj = null;
                    for (int i = 0; i < duyurular.length(); i++) {
                        JSONObject obj = null;
                        try {
                            obj = duyurular.getJSONObject(i);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            postid = obj.getString("id");
                            postids.add(postid);
                            mesaj = obj.getString("message");
                        } catch (JSONException e) {
                            mesaj = "This attachment may have been removed or the person who shared it may not have permission to share it with you.";
                        }
                        JSONObject fromObj = null;
                        try {
                            fromObj = obj.getJSONObject("from");
                        } catch (JSONException e1) {
                            System.out.println("Hata 2");
                            e1.printStackTrace();
                        }
                        try {
                            sender = fromObj.getString("name");
                            hocaid = fromObj.getString("id");
                        } catch (JSONException e) {
                            sender = "Name didn't recieved.";
                        }
                        if (lecturerID.equals(hocaid)) {

                        } else {
                                    interactsenders.add(sender);
                                    interactposts.add(mesaj);
                                    interactimages.add("http://graph.facebook.com/"+ hocaid + "/picture");
                                    interactlinks.add("http://www.facebook.com/544570888900558/posts/"+ postid);

                        }
                    }


                    System.out.println("Size: "+postids.size());


                    ListToList(interactsenders, profname);
                    ListToList(interactposts, intercontent);
                    ListToList(interactimages, profpic);
                    ListToList(interactlinks, facebookunlink);

                    }
                }


        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return null;
    }

    protected void ListToList(ArrayList<String> source,ArrayList<String> destination) {
        for (int i = 0; i < source.size(); i++) {
            destination.add(source.get(i));
        }
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        interaction=new AnnounceAdapter(main, profname,profpic,intercontent);
        interaction.notifyDataSetChanged();
    }
}

我的 Logcat 是:

08-19 10:38:10.172: W/System.err(4946): org.json.JSONException: No value for paging
08-19 10:38:10.172: W/System.err(4946):     at org.json.JSONObject.get(JSONObject.java:354)
08-19 10:38:10.172: W/System.err(4946):     at org.json.JSONObject.getJSONObject(JSONObject.java:569)
08-19 10:38:10.172: W/System.err(4946):     at com.invemo.vodafonelivecoursetest.MainActivity$GetFacebookAnnounceDatas.doInBackground(MainActivity.java:613)
08-19 10:38:10.172: W/System.err(4946):     at com.invemo.vodafonelivecoursetest.MainActivity$GetFacebookAnnounceDatas.doInBackground(MainActivity.java:1)
08-19 10:38:10.182: W/System.err(4946):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-19 10:38:10.182: W/System.err(4946):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-19 10:38:10.182: W/System.err(4946):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-19 10:38:10.182: W/System.err(4946):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-19 10:38:10.182: W/System.err(4946):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-19 10:38:10.182: W/System.err(4946):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-19 10:38:10.182: W/System.err(4946):     at java.lang.Thread.run(Thread.java:856)
4

1 回答 1

0

试着放

loadingMore2=false;

进入 AsyncTask 的 postExecute 方法,而不是 onScroll。你现在拥有它的方式 AsyncTask 可以一次运行多次,因为loadingMore2在上一次下载完成之前切换到 true 。这可能是问题所在。

于 2013-08-19T07:54:27.030 回答