2014-09-04 41 views
0

所以,我有一個列表視圖。它一次從服務器加載10行。當用戶滾動到底部時,它將加載10個以上。當沒有更多的行要添加時,我希望它顯示「結果結束」的腳註。因此,當我設置適配器時,我不能添加footerview,因爲它會在前10行後面顯示「結果結束」,並且可能會有更多結果要添加。這是我的代碼:把頁腳放在動態增長的ListView Android應用程序

這是我試過,沒有錯誤,但footerview不顯示。

 if (finished == 1) { 
      Log.d("Test", "at the end of listview"); 
      footerView = getLayoutInflater().inflate(
       R.layout.listview_footer, listView, false); 
      listView.addFooterView(footerView); 
     } 

和測試在listview的末尾被記錄下來,所以我知道它在最後。有任何想法嗎?謝謝。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    arrayOfLocations = new ArrayList<Location>(); 

    startLoop = 0; 
    endLoop = 10; 

    listView = (ListView) findViewById(R.id.listView1); 

    adapter = new LocationAdapter(this, arrayOfLocations); 

    FillLocations myFill = new FillLocations(); 
    myFill.execute(); 

    listView.setAdapter(adapter); 
    listView.setOnScrollListener(new OnScrollListener() { 
     @Override 
     public void onScroll(AbsListView view, int firstVisibleItem, 
       int visibleItemCount, int totalItemCount) { 

      // if user scrolls... 
      if (listView != null 
        && listView.getAdapter() != null 
        && listView.getChildAt(listView.getChildCount() - 1) != null 
        && listView.getLastVisiblePosition() == listView 
          .getAdapter().getCount() - 1 
        && listView.getChildAt(listView.getChildCount() - 1) 
          .getBottom() <= listView.getHeight() 
        && finished == 0) { 
       startLoop += 10; 
       endLoop += 10; 
       FillLocations myList = new FillLocations(); 
       myList.execute(); 
      } 
     } 
    }); 
} 

private class FillLocations extends 
     AsyncTask<Integer, Void, ArrayList<Location>> { 

    int finished = 0; 

    // Decode image in background. 
    @Override 
    protected ArrayList<Location> doInBackground(Integer... params) { 

       ... 

     // parse json data 
     try { 
      JSONArray jArray = new JSONArray(result); 
      // if there are less than 10 listings left (no more results) 
      if (jArray.length() < endLoop) { 
       endLoop = jArray.length(); 
       finished = 1; 
      } 

      for (int i = startLoop; i < endLoop; i++) { 
       final JSONObject json = jArray.getJSONObject(i); 
       // add rows to adapter 
      } 

     } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
     return arrayOfLocs; 
    } 

    protected void onPostExecute(ArrayList<Location> result) { 

     //if it is at the end, add the footer 
     if (finished == 1) { 
      Log.d("End", "at the end"); 
      footerView = getLayoutInflater().inflate(
       R.layout.listview_footer, listView, false); 
      listView.addFooterView(footerView); 
     } 
     for (Location location : result) 
      adapter.add(location); 

    } 
} 

回答

0

您添加footerview到ListView控件但不刷新adapter.So你加入,你必須通知列表視圖列表視圖是用變更後:

adapter.notifyDataSetChanged(); 

所以,你的代碼必須是這樣的:

if (finished == 1) { 
     Log.d("Test", "at the end of listview"); 
     footerView = getLayoutInflater().inflate(
      R.layout.listview_footer, listView, false); 
     listView.addFooterView(footerView); 
     adapter.notifyDataSetChanged(); 
    } 

編輯:

嘗試此onPostExecute()

protected void onPostExecute(ArrayList<Location> result) { 

    arrayOfLocations.addAll(result); 
    if (finished == 1) { 
     Log.d("End", "at the end"); 
     footerView = getLayoutInflater().inflate(
      R.layout.listview_footer, listView, false); 
     listView.addFooterView(footerView); 
    } 
    adapter = new LocationAdapter(context, arrayOfLocations); 
    listview.setAdapter(adapter); 

} 

刪除此行的onCreate()

listView.setAdapter(adapter); 
+0

感謝您的回答,但不幸的是我已經試過這一點,它仍然無法正常工作 – user1282637 2014-09-04 19:32:35

+0

是如果爲真,將其記錄「末.. 。「? – OmerFaruk 2014-09-04 19:35:03

+0

是的,它的確在日誌末尾 – user1282637 2014-09-04 19:36:03