2014-10-17 135 views
3

我想實現OnScrollListener來動態加載更多的數據,當滾動到底部時。下面的代碼是給我NullPointerException異常,但是當我宣佈Android無限滾動列表視圖

BusinessListDataAdapter adapter = new BusinessListDataAdapter(this, 
      this.imgFetcher, this.layoutInflator, this.businesses); 

setBusiness,它不顯示任何錯誤,並正常工作。這個問題是,當我滾動到頁面底部它替換當前數據list view用新的數據,但我想要的是無盡的滾動而追加新的數據,而不是代替,讓我可以看到第一個數據向上滾動

import java.util.ArrayList; 

import com.sp.sodhpuch.adapters.BusinessListDataAdapter; 
import com.sp.sodhpuch.data.BusinessListData; 
import com.sp.sodhpuch.tasks.BusinessListApiTask; 
import com.sp.sodhpuch.tasks.BusinessListIconTask; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.widget.AbsListView; 
import android.widget.AbsListView.OnScrollListener; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class ListResultActivity extends Activity { 

    private ArrayList<BusinessListData> businesses; 
    private ListView businessList; 
    private LayoutInflater layoutInflator; 
    private BusinessListIconTask imgFetcher; 
    BusinessListDataAdapter adapter = new BusinessListDataAdapter(this, 
      this.imgFetcher, this.layoutInflator, this.businesses); 

    @SuppressWarnings("unchecked") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.businesslist); 
     getData(); 
//  @SuppressWarnings("deprecation") 
//  final Object[] data = (Object[]) getLastNonConfigurationInstance(); 
//  if (data != null) { 
//   setContentView(R.layout.businesslist); 
//   this.businessList = (ListView) findViewById(R.id.lvBusinesslist); 
//   this.imgFetcher = new BusinessListIconTask(this); 
//   this.layoutInflator = LayoutInflater.from(this); 
// 
//   this.businesses = (ArrayList<BusinessListData>) data[0]; 
//   this.imgFetcher = (BusinessListIconTask) data[1]; 
//   businessList.setAdapter(adapter); 
//  } 
    } 

    @Override 
    public Object onRetainNonConfigurationInstance() { 
     Object[] myStuff = new Object[2]; 
     myStuff[0] = this.businesses; 
     myStuff[1] = this.imgFetcher; 
     return myStuff; 
    } 

    /** 
    * Bundle to hold refs to row items views. 
    * 
    */ 
    public static class MyViewHolder { 
     public TextView businessName, businessAddress, phoneNo; 
     public Button btnProfile; 
     public ImageView icon; 
     public BusinessListData business; 
    } 

    public void setBusinesses(ArrayList<BusinessListData> businesses) { 
     this.businessList = (ListView) findViewById(R.id.lvBusinesslist); 

     this.imgFetcher = new BusinessListIconTask(this); 
     this.layoutInflator = LayoutInflater.from(this); 
     this.businesses = businesses; 
     this.businessList.setAdapter(adapter); 
     businessList.setOnScrollListener(new OnScrollListener() { 
      public void onScrollStateChanged(AbsListView view, int scrollState) { 

      } 

      @Override 
      public void onScroll(AbsListView view, int firstVisibleItem, 
        int visibleItemCount, int totalItemCount) { 
       if (businessList.getLastVisiblePosition() == totalItemCount - 1) { 
        getData(); 
//     adapter.notifyDataSetChanged(); 
       } 

      } 
     }); 

    } 

    private void getData() { 
     // TODO Auto-generated method stub 
     Intent myIntent = getIntent(); 

     // gets the arguments from previously created intent 
     String metroTxt = myIntent.getStringExtra("key"); 
     String metroLoc = myIntent.getStringExtra("loc"); 
     String metroId = myIntent.getStringExtra("qt"); 

     BusinessListApiTask spTask = new BusinessListApiTask(
       ListResultActivity.this); 

     try { 
      spTask.execute(metroTxt, metroLoc, metroId); 

     } catch (Exception e) { 
      spTask.cancel(true); 
     } 
    } 

} 

這裏是logcat的

10-17 04:50:51.897: W/dalvikvm(6942): threadid=1: thread exiting with uncaught exception (group=0xb1d41b20) 
10-17 04:50:51.897: E/AndroidRuntime(6942): FATAL EXCEPTION: main 
10-17 04:50:51.897: E/AndroidRuntime(6942): Process: com.sp.sodhpuch, PID: 6942 
10-17 04:50:51.897: E/AndroidRuntime(6942): java.lang.NullPointerException 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at com.sp.sodhpuch.adapters.BusinessListDataAdapter.getCount(BusinessListDataAdapter.java:53) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at android.widget.ListView.setAdapter(ListView.java:480) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at com.sp.sodhpuch.ListResultActivity.setBusinesses(ListResultActivity.java:78) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at com.sp.sodhpuch.tasks.BusinessListApiTask.onPostExecute(BusinessListApiTask.java:119) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at com.sp.sodhpuch.tasks.BusinessListApiTask.onPostExecute(BusinessListApiTask.java:1) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at android.os.AsyncTask.finish(AsyncTask.java:632) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at android.os.AsyncTask.access$600(AsyncTask.java:177) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at android.os.Handler.dispatchMessage(Handler.java:102) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at android.os.Looper.loop(Looper.java:136) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at java.lang.reflect.Method.invoke(Method.java:515) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
10-17 04:50:51.897: E/AndroidRuntime(6942):  at dalvik.system.NativeStart.main(Native Method) 
+0

可能重複的[Android - ListView加載更多的項目時,達到結束](http://stackoverflow.com/questions/20458475/android-listview-to-load-more-items-when-reached-end) – SelvaMariappan 2014-10-17 09:16:24

+0

後logcat請 – 2014-10-17 09:31:04

+0

我已附加logcat – carefree 2014-10-17 09:50:53

回答

0

你需要做三件事情。

1)創建方法將添加10個項目到適配器列表。

2)在列表視圖頁腳中添加包含按鈕的xml。

3)然後在調用步驟1方法的activity或fragment類中爲該按鈕編寫clickListener,然後重置listview適配器。

0

Android - ListView to load more items when reached end

您可以將在其上有一個名爲LoadMore按鈕ListView控件添加頁腳。這裏是一個完整的教程List With Load more也可以實現onscrolllistener()和這個監聽器添加到您的ListView

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
    this.currentFirstVisibleItem = firstVisibleItem; 
    this.currentVisibleItemCount = visibleItemCount; 
} 

public void onScrollStateChanged(AbsListView view, int scrollState) { 
    this.currentScrollState = scrollState; 
    this.isScrollCompleted(); 
} 

private void isScrollCompleted() { 
    if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) { 
     /*** In this way I detect if there's been a scroll which has completed ***/ 
     /*** do the work for load more date! ***/ 
     if(!isLoading){ 
     isLoading = true; 
     loadMoreData(); 
    } 
} 
} 
+0

我的問題是沒有實現更多或OnScrollListener加載。 OnScrollListener正在工作,但新提取的數據替換列表中的以前的數據,我想要的是追加更新的數據以追加當前數據 – carefree 2014-10-19 11:41:06

0

正是一個解決方案最有可能與你的任務和適配器不一起滾動列表的問題。 您可以實現的方法,爲addItems,在你listAdapter如下:

public void addItems(BusniessListData... newItems){ 
     for(BusniessListData newItem : newItems){ 
      this.businesses.add(newItem); 
     }   
     notifyDataSetChanged(); 
    } 

在你的任務,或任何回調您使用的onPostExecute,檢查是否有listAdapter項目,然後調用傳遞新取的物品爲addItems。否則,啓動並填充提取的數據集。