Android无限滚动列表视图

时间:2014-10-17 09:06:37

标签: android infinite-scroll custom-adapter onscrolllistener

我希望实现OnScrollListener以在动态滚动到底部时加载更多数据。下面的代码给了我NullPointerException,但是当我声明

  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)

4 个答案:

答案 0 :(得分:0)

您需要做三件事。

1)创建方法,将另外10个项添加到适配器列表。

2)在listview页脚中添加包含按钮的xml。

3)然后在activity或fragment类中为该按钮编写clickListener,在该类中调用step 1方法,然后重置listview适配器。

答案 1 :(得分:0)

来自Android - ListView to load more items when reached end

您可以在listView上添加一个页脚,该页脚将有一个名为LoadMore的按钮。这是一个完整的教程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();
    }
}
}

答案 2 :(得分:0)

嗨@carefree这里有一个解决方案可能会让你完全达到你想要的目标 https://github.com/rahulinaction/ImageGrid

答案 3 :(得分:0)

最可能出现在您的任务和适配器上的问题,而不是列表滚动。 您可以在listAdapter中实现方法addItems,如下所示:

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

在你的任务的onPostExecute或你使用的任何回调中,检查listAdapter是否有项目,然后调用addItems传递新获取的项目。否则,启动并填充提取的数据集。