Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arrayList returns 5 records but defined with 10

Status
Not open for further replies.

RicardoPereira

Programmer
Jun 3, 2003
255
PT
Hi,

I have had created an arrayList to implement paging. The strange thing is that even if i define an increment of 10 records, i have always 5. What should be the cause of this?

Here is my code:

<%
if (
sReclamacao.equals("")){

ArrayList arrayList = null;
boolean check = true;
// session.removeAttribute("arrayList");
/* Se a arrayList estiver na sessão o Objecto evita a chamada da base de dados
e retorna a ArrayList do objecto da sessão
*/
if(null == session.getAttribute("arrayList"))
{
/** Precisa de ir buscar a arrayList à base de dados */
arrayList = form.getReclamacoesReclam(sCliente,sReclamacao);
session.setAttribute("arrayList", arrayList);
}else{
arrayList = (ArrayList)session.getAttribute("arrayList");
}

int arrayListSize = arrayList.size();
// Nº de registos a visualizar por página
// faz um incremento de x e verifica se existem apenas x registos por página
int increment = 10;
int fromIndex = 0;
int toIndex = increment;
String uri = request.getRequestURI();
String previous= "Anterior";
String next = "Próximo";
List displayList = null;
if( null != request.getParameter("next"))
{
fromIndex = Integer.parseInt(request.getParameter("next"));
toIndex = increment + fromIndex;
if( toIndex+1 > arrayListSize)
{
toIndex = arrayListSize;
check = false;
}
if( fromIndex > arrayListSize)
fromIndex = 0;
}

if( null != request.getParameter("prev"))
{
toIndex = Integer.parseInt(request.getParameter("prev"));
fromIndex = toIndex - increment;
}
if(arrayListSize > 0)
{
if(increment > arrayListSize){
toIndex = arrayListSize;
displayList = arrayList.subList(fromIndex, toIndex);
toIndex = 0;
}
else
{
displayList = arrayList.subList(fromIndex, toIndex);
}
}
if(fromIndex != 0)
previous = "<a href="+ uri +"?prev="+ fromIndex +" onmouseover=\"window.status='Anterior';return true;\"> Anterior </a>";
if(toIndex != 0 && check)
next = "<a href="+ uri +"?next="+ toIndex +" onmouseover=\"window.status='Próximo';return true;\"> Próximo </a>";

out.println("<br>");
//out.println(displayList);

if(displayList != null) {
Iterator iterator = displayList.iterator();
if(iterator != null) {
while(iterator.hasNext()){
Object tmp = iterator.next();
if(tmp != null){
QryReclamacoes qryReclam=(QryReclamacoes) iterator.next();
out.println("<tr><td><a href=javascript:eek:penPage('reclamacao.jsp?Reclamacao=" + qryReclam.getN_Reclamacao() + "') onmouseover=\"window.status='Reclamacao " + qryReclam.getN_Reclamacao() +
"';return true;\"><b>" + qryReclam.getN_Reclamacao() + "</b></a>" +
"</td><td>" + qryReclam.getNome_Pessoa() + "</td><td>" + qryReclam.getLoja() + "</td>" +
"<td>" + qryReclam.getDoc_Relacionado() + "</td><td>" + qryReclam.getData_Ocorrencia() + "</td>" +
"<td>" + qryReclam.getTipo_Reclamacao() + "</td><td>" + qryReclam.getDescricao() + "</td><td>" + qryReclam.getEstado() + "</td>");
}
else
{out.println("<tr><td colspan=\"7\">O Artigo que pretende consultar não existe na base de dados.</td></tr>");
}
}
}else
{out.println("<tr><td colspan=\"7\">A Reclamação que pretende consultar não existe na base de dados.</td></tr>");}
}else
{out.println("<tr><td colspan=\"7\" align=center class=boldwarning>Não existem reclamações para pesquisar na base de dados.</td></tr>");
}

out.println("<br></table><hr>");

out.println("<<");
out.println(previous);
out.println("&nbsp;&nbsp;");
out.println(next);
out.println(">>");
}
else
{
ArrayList ListaReclamacoesReclam=form.getReclamacoesReclam(sCliente,sReclamacao);
for( int x=0;x<ListaReclamacoesReclam.size();x++)
{
QryReclamacoes qryReclam=(QryReclamacoes) .get(x);
out.println("<tr>" +
"<td><a href=javascript:eek:penPage('reclamacao.jsp?reclamacao=" + qryReclam.getN_Reclamacao() + "') onmouseover=\"window.status='Reclamacao " + qryReclam.getN_Reclamacao() + "';return true;\"><font class='boldVerde'>" + qryReclam.getN_Reclamacao() + "</b></a>" +
"</td><td>" + qryReclam.getNome_Pessoa() +
"</td><td>" + qryReclam.getLoja() +
"</td><td>" + qryReclam.getDoc_Relacionado() +
"</td><td>" + qryReclam.getData_Ocorrencia() +
"</td><td>" + qryReclam.getTipo_Reclamacao() +
"</td><td>" + qryReclam.getDescricao() +
"</td><td>" + qryReclam.getEstado() + "</td></tr>");
}

out.println("<br></table>");
}
%>



Thanks
Ricardo
 
Hi

Here is the code which I have posted earlier for the paging. It work if you just chnage the increment value to a new value. If you are using a tomcat clear the localhost directory under work folder and try again.
Previous code
Code:
<%--
  Created by IntelliJ IDEA.
  User: vreddy
  Date: Apr 22, 2005
  Time: 1:47:09 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.util.ArrayList,
                 java.util.Iterator,
                 java.util.List,
                 java.util.Hashtable"%>
<%
    ArrayList arrayList = null;

    /* comment below code or remove */
    Hashtable hashtable = new Hashtable(50);
    for(int i=0; i<50 ; i++)
    {
        hashtable.put(i+"","Image Name"+i);
    }
    arrayList = new ArrayList(hashtable.values());
    /* End of Comment code that needs to be removed */

    /*
     if(null != request.getParameter("Name"))
     {
       //Get the Hashtable from the DB if requestParameter is null
       Hashtable imagenames = image.getNames(imgName, imgId, imgacc);
       //Get the values from Hashtable and store it in ArrayList
       arrayList = new ArrayList(imagenames.values());
      //Store the ArrayList into session Object so that you can avoid DB calls from second call to the same page
       session.setAttribute("arrayList", arrayList);
     }else{
      // get the ArrayList from session
        arrayList = (ArrayList)session.getAttribute("arrayList");
     }
     */
    boolean check = true;
    int arrayListSize = arrayList.size();
    // Number of Records that need to displayed per page
    // make the increment value 5 and check it will display only 5 records per page
    int increment = 10;
    int fromIndex = 0;
    int toIndex = increment;
    String uri = request.getRequestURI();
    String previous= "Previous";
    String next = "Next";
    List displayList = null;
    if( null != request.getParameter("next"))
    {
        fromIndex = Integer.parseInt(request.getParameter("next"));
        toIndex = increment + fromIndex;
        if( toIndex+1 > arrayListSize)
        {
            toIndex = arrayListSize;
            check = false;
        }
        if( fromIndex > arrayListSize)
            fromIndex = 0;
    }

    if( null != request.getParameter("prev"))
    {
        toIndex = Integer.parseInt(request.getParameter("prev"));
        fromIndex = toIndex - increment;
    }

   if(arrayListSize > 0)
    {
      if(increment > arrayListSize){
        toIndex = arrayListSize;
        displayList = arrayList.subList(fromIndex, toIndex);
        toIndex = 0;
      }else{
          displayList = arrayList.subList(fromIndex, toIndex);
      }
   }
    if(fromIndex != 0)
       previous = "<a href="+ uri +"?prev="+ fromIndex +"> Previous </a>";
    if(toIndex != 0 && check)
       next = "<a href="+ uri +"?next="+ toIndex +"> Next </a>";

    out.println("Total Size of ArrayList is " + arrayListSize);
    out.println("<br>");
    //out.println(displayList);

   /* out.println("<br>");
    out.println(next);
    out.println("<br>");
    out.println(previous); */
   
%>

<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="43%" id="AutoNumber1">
 <caption><%=arrayListSize%></caption>
  <tr>
    <td width="53%">
    <p align="center"><%=next%></td>
    <td width="47%">
    <p align="center"><%=previous%></td>
  </tr>
  <% Iterator iterator = displayList.iterator();
    while(iterator.hasNext()){
        String s = iterator.next().toString();
  %>
   <tr>
    <td width="100%" colspan="2"><%=s%></td>
   </tr>
   <%
    }%>

</table>

<%!
    static String getAbbrivate(String str, int width)
    {
        return str;
    }
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top