对 ArrayList 进行分页 [英] Paginating an ArrayList

查看:55
本文介绍了对 ArrayList 进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 ArrayList 中保存的值创建一个简单的分页例程.基本上我想做的是首先渲染 ArrayList 中的前五个元素.然后当用户点击Next(再增加5)Previous(减少5).

I am trying to create a simple pagination routine for values held in an ArrayList. Basically what I want to do is render the first five elements in the ArrayList at first go. And then when users click on Next (increment by another 5) and Previous (decrease by 5).

我的逻辑是这样的:

class foo
{
   private static final int defaultStep = 5;
   private int moveCounter;
   private List<String> values;

   public foo()
   {
     values = new ArrayList<String>();
     values.add("Fiber Channel");
     values.add("Copper Channel");
     ...
   }

  private void pageNext()
  {
   if (moveCounter > -1 && moveCounter < values.size())
   {
    int currentIndex = (moveCounter + 1);
    renderValues(currentIndex, false);
   }
  }

  private void pagePrevious()
  {
   if (moveCounter > -1 && moveCounter <= values.size())
   {
    renderValues(moveCounter-1, true);
   }
  }

 private void renderValues(int startIndex, boolean isPreviousCall)
 {
  if (startIndex > -1)
  {
   StringBuilder html = new StringBuilder();
   List<String> valuesToRender = new ArrayList<String>();
   int checkSteps = 1;
   while (startIndex < values.size())
   {
     valuesToRender.add(values.get(startIndex));
     if (checkSteps == defaultStep) break;
     startIndex++;
     checkSteps++;
   }
   moveCounter = startIndex;

   //TODO: Build html String
   ...
   }
 }
}

我在调用 pagePrevious 时遇到问题,你们能帮我构建 valuesToRender 5-steps values 数组 在将要渲染的值添加到 valuesToRender 数组之前.

I have an issue with pagePrevious call, can you guys help me build the valuesToRender 5-steps up values array before adding the value to render to the valuesToRender array.

我也尝试做这样的事情:

I tried doing something like this also:

  for (int start = startIndex, end = values.size() - 1; start < end; start++, end--)
  {
    if (isPreviousCall) valuesToRender.add(values.get(end));
    else valuesToRender.add(values.get(start));

    if (checkSteps == defaultStep) break;
    checkSteps++;
  }

但这似乎也不起作用.你们能发现并帮助我解决这个问题吗?谢谢各位.

But this doesn't seems to work neither. Can you guys spot and help me fix this issue. Thanks Guys.

推荐答案

我会这样做:

我不确定 renderValues 的作用,以及我们是否必须从 moveCounter 的上限中减去 1 或 defaultStep.

I'm not sure what renderValues does, and whether we have to substract 1 or maybe defaultStep from the upper bound of the moveCounter.

private void pageMove (int step)
{
    moveCounter = moveCounter + step;
    if (moveCounter < 0) moveCounter = 0;
    if (moveCounter > values.size ()) moveCounter = values.size ();
    renderValues (currentIndex, false);
}

private void pageNext ()
{
    pageMove (defaultStep);
}

private void pagePrevious ()
{
    pageMove (-defaultStep);
}

前 3 行可以打包成两个大的三元表达式,如下所示:

The first 3 lines could be packed into two big ternary experssions like so:

mc = ((mc + s) < 0) ? 0 : ((mc + s) > vs) ? vs : (mc + s); 

但最好遵循 3 行解决方案.

but the 3 lines solution is better to follow.

这篇关于对 ArrayList 进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆