Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

Is there any method in java to point the cursor upto 10th record in first page like JDBC methods

presently  array size is 23 it will increase dynamically;

i want to display this numbers in 3 pages.

in first page i will display from 1 to 10 numbers ,when click next it should display from 11 to 20 and finally click next remaining 3 records at the last page(if user clicks previous it should show previous records).these records are not coming from the database.so i cant use scrollable resultset in this case.


Is there any method in java to point the cursor upto 10th record in first page(because i will display only 10 records in first page;).when user clicks next, cursor should point from 11 record onwards.(like JDBC absolute() scrollable result methods)
ASKER CERTIFIED SOLUTION
Avatar of vsubram2
vsubram2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
>>pageStartIndex=(pageNumbe-1)r*numberOfRecords;

I think you meant what i've given below and i don't think the following could be right

>>
else
endIndex=list.size();
>>

This gives the index

      int listIndex = (pageNumber - 1) * pageSize;

and if you exceed or fall below a possible index, you can wrap:

      listIndex = (listIndex >= 0 && listIndex < numberOfRecords)? listIndex : 0;

Also, bear in mind that using a List may not scale well, so you may like to manipulate your objects without holding them all in memory. A RandomAccessFile may be OK
Avatar of vsubram2
vsubram2

pageStartIndex=(pageNumbe-1)r*numberOfRecords;
yeah the above is really typo.sorry.

remaining things looks ok ,and i tested .
ie)numberOfRecords= 10;  ---->no of records to be displayed in a page.
if it is constant ,you can keep it java program or u can get it from a properties file.

pageStartIndex=(pageNumber-1)*numberOfRecords;

to check the limit ,that is we should not exceed the existing objects size
this check is for the last page ,3rd page here, which hold only three records .

if(pageStartIndex+numberOfRecords <= list.size())
endIndex=pageStartIndex+numberOfRecords;
else
endIndex=list.size(); // it is the size of the list .
and u can the sublist with
 listObject.sublist(pageStartIndex,endIndex); which will actually give u the list of all elements from pageStartIndex to endIndex-1.


>>A RandomAccessFile may be OK .yeah that's true.if your number of resultant objects is more ,it is not good to keep in memory. we can use random acess file and move the cursor with  RAF.seek.

This is a problem called "pagination" of database results. Here, you have a page in the UI showing 40 rows or more. You want to show only 10 rows at a time with previous and next buttons. There are code snippets available for that, you just have to give the datasource and query and parameters like 10(number of rows in a page)

Check these links,

http://www.javaworld.com/javaworld/jw-07-2004/jw-0726-pagination.html
http://developers.sun.com/learning/javaoneonline/2004/javatechweb/TS-1341.pdf
Common frameworks like Common Controls, strutslogic
In case you are willing to *actually* get the results from db into memory some fwks like
http://struts.application-servers.com/ 

have tags which will directly give you what you want

In case you have the complete list in memory, you just have to write a TLD and a Tag action class, which is the most *non intrusive* of achieving this.

The url i pointed above has some sample code on how to write such tag libraries
Avatar of chaitu chaitu

ASKER

vsubram2,

thanks for introducing the new method in List.