2013-08-19 58 views
1

Hello我在我的android應用程序中有一個listview。當我的應用程序打開我加載它,如果用戶滾動列表視圖列表視圖結束加載完美。但在第二次滾動它不會自己加載。這裏是我的代碼: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) 

回答

0

嘗試把

loadingMore2=false; 

到您的AsyncTask代替onScroll的postExecute方法。您現在擁有AsyncTask的方式可以一次運行多次,因爲loadingMore2在上次下載完成之前切換爲true。這可能是問題。

+0

它沒有解決我的問題。 ListView仍然加載一次。 –

相關問題