滚动时更新相同的listView

时间:2013-01-03 12:22:13

标签: android android-layout android-listview android-asynctask android-adapter

该应用程序的目的是在用户向下滚动并更新现有ListView时获取更多数据。

处理用户滚动的代码段是:

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, 
                     int totalItemCount) {
    int totalVisible = firstVisibleItem + visibleItemCount;

     if (view.getAdapter() != null && 
        (totalVisible >= totalItemCount) && 
        totalItemCount != priorFirst && 
        visibleItemCount != totalItemCount) {
            Log.v("MyActivity", "onListEnd, extending list");
            priorFirst = totalItemCount;

            //params is array of values passed to business layer for data fetch
            String[] params = builder.toString().split("&");

            UIHandler uiHandler = new UIHandler(this, getParent());
            //start async task      
            EnquiriesRunner enquiriesRunner = new EnquiriesRunner(uiHandler);
            enquiriesRunner.execute(params);
    }

在后台,它会调用业务层。

protected ExceptionHandler<Object> doInBackground(String... params) {
    Object obj = enquiriesCaller.getEnquiries(params);
    return new ExceptionHandler<Object>(obj);
}

将结果数据传递给UI处理程序,使用自定义适配器将其呈现给listView。

ListView enquiryView = (ListView) ((Activity) childContext).findViewById(R.id.enquiryListView);
CustomAdapter adapter = new CustomAdapter(childContext, msg.obj);
enquiryView.setAdapter(adapter);

msg.obj是包装到Object中的搜索结果。这将被转换为适配器中所需的POJO引用。

如您所见,每次使用新数据集替换listView时。任何人都可以建议更新现有List的可能解决方案吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

尝试此链接对您有用。

http://benjii.me/2010/08/endless-scrolling-listview-in-android/

您可以在滚动时更新ListView ..