Android加载更多列表视图

时间:2013-08-19 07:48:02

标签: android facebook listview autoload

你好我的android应用程序中有一个listview。当我的应用程序打开我加载它,如果用户滚动到列表结束列表视图完全加载自己。但在第二个滚动中它不会加载自己。这是我的代码:

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)

1 个答案:

答案 0 :(得分:0)

尝试放

loadingMore2=false;

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