按数组列表自定义分页

时间:2015-10-16 13:14:57

标签: java spring arraylist pagination

我想在java代码中创建一个数组列表的自定义分页

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort.Direction;
...    

    int page = 0;
    int count = 8;
    String sortOrder = "desc";
    String sortBy = "id";

    Sort sort = new Sort(Direction.fromString(sortOrder), sortBy);
    PageRequest pageable = new PageRequest(page, count, sort);

    List<Impianto> impiantos = myService.findMyMethod(); // returned 30 objects 
    Page<Impianto> pageImpianto = new PageImpl<Impianto>(impiantos, pageable, impiantos.size()); 

上面的脚本不会返回包含8个元素的页面。为什么呢?

N.B。列表没有从db

返回

你能帮助我吗?

4 个答案:

答案 0 :(得分:2)

已解决此解决方法。

 UISearchController *searchController = [[UISearchController alloc]init];
self.searchController.searchBar.barTintColor = [UIColor whiteColor];
[self.navigationController addChildViewController:searchController];

我实现了一个新的自定义比较器类

    import org.springframework.data.domain.Sort;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageImpl;
    import org.springframework.data.domain.Sort.Direction;
    ...    

        int page = 0;
        int count = 8;
        String sortOrder = "desc";
        String sortBy = "id";

        Sort sort = new Sort(Direction.fromString(sortOrder), sortBy);
        PageRequest pageable = new PageRequest(page, count, sort);

        List<Impianto> impiantos = myService.findMyMethod(); // returned 30 objects 
        int max = (count*(page+1)>impiantos.size())? impiantos.size(): count*(page+1);

        Page<Impianto> pageImpianto = new PageImpl<Impianto>(impiantos.subList(page*count, max), pageable, impiantos.size());  

答案 1 :(得分:0)

我根据您的要求编写了自己的代码,以满足客户的要求,没有其他要求。 谢谢

控制器的外观如下

    @RequestMapping(value="/zonas", method = RequestMethod.GET)
public String zonas(ModelMap modelMap, @PageableDefault(size=3) Pageable pageable, ZonasFilter zonasFilter, Locale locale) {
    LOGGER.info("Cargando la vista del índice de Zonas.");
    List<Zona> zonas;
    try {
        zonas = zonasService.loadAll();
        int max = (pageable.getPageSize()*(pageable.getPageNumber()+1)>zonas.size())? zonas.size(): pageable.getPageSize()*(pageable.getPageNumber()+1);
        modelMap.addAttribute("zonas",new PageImpl<Zona>(zonas.subList(pageable.getPageSize()*pageable.getPageNumber(), max), pageable, zonas.size()));
    } catch (ApiException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        List<ApiError> errors=e.getErrors(); 
        for (ApiError apiError : errors) {
            modelMap.addAttribute("error", messages.getMessage(apiError.getMessage(), null, locale));
        }

    }   
    LOGGER.info("Vista del índice de  Zonascargada.");
    return "zonas/index";
}

答案 2 :(得分:0)

Flavio Troia的答案是错误的,当impiantos小于count且页面大于1时,调用subList函数fromIndex(10)> toIndex(5)时会出现异常,我这样编写了自己的代码,这也许是多余的,但对我来说,很容易理解,对吧〜

 List<UcShopCourseBizPojo> shopCourseBizPojos = addCourseCoupons(mcCouponLists, shopCourseLists);


        if (pageable.getOffset() > shopCourseBizPojos.size()) {
            long total = 0L;
            PageImpl<UcShopCourseBizPojo> emptyPage = new PageImpl<>(Lists.newArrayList(), pageable, total);
            resultDo.setResult(emptyPage);
            return resultDo;
        }

        if (pageable.getOffset() <= shopCourseBizPojos.size() && pageable.getOffset() + pageable.getPageSize() > shopCourseBizPojos.size()) {
            List<UcShopCourseBizPojo> bizPojos = shopCourseBizPojos.subList(pageable.getOffset(), shopCourseBizPojos.size());
            PageImpl<UcShopCourseBizPojo> pPage = new PageImpl<>(bizPojos, pageable, shopCourseLists.size());
            resultDo.setResult(pPage);
            return resultDo;
        }

        List<UcShopCourseBizPojo> ucShopCourseBizPojos = shopCourseBizPojos.subList(pageable.getOffset(), pageable.getOffset() + pageable.getPageSize());

        PageImpl<UcShopCourseBizPojo> pPage = new PageImpl<>(ucShopCourseBizPojos, pageable, shopCourseLists.size());
        resultDo.setResult(pPage);
        return resultDo;

答案 3 :(得分:0)

对我来说有效

Page<OrderLineDTO> toPage(List<OrderLineDTO> list, int pagesize, int pageNo) {

    int totalpages = list.size() / pagesize;
        PageRequest pageable = new PageRequest(pageNo, pagesize);

        int max = pageNo>=totalpages? list.size():pagesize*(pageNo+1);
        int min = pageNo >totalpages? max:pagesize*pageNo;

        logger.info("totalpages{} pagesize {} pageNo {}   list size {} min {}   max {} ...........", totalpages,pagesize, pageNo, list.size(),
                min, max);
        Page<OrderLineDTO> pageResponse = new PageImpl<OrderLineDTO>(list.subList(min, max), pageable,
                list.size());
        return pageResponse;
    }