Paging in J2ME Canvas
I want to create paging in my j2me application.
I have pulled the xml file and stored them into a vector. So I would have 80+ items in a vector.
I want to know how I can create paging.
I do understand the logic of how to create it. I will loop through the vector and display to the user. But I want to know how I can just by pressing the right key, it will go to the next page.
My problem is the part whereby it is at the last page and when I press down, there is an exception because I am accessing an element which is not present. This is the way I think I should be doing paging.
I thought of another way, which is to just display the specific amount of data each page, parsing in a number. But for that, I need to create a new screen, is there a solution other than that?
Or Is there any other way?
[850 byte] By [
darrana] at [2007-11-27 4:24:32]

# 1
hi darran...
We have similar problems before, how to create paging in J2ME.
Your logic in making paging is correct, maybe there are some things overlooked which resulted to exceptions.
This is my solution. Hope it'll help
/* Global vector, this where your data located */
private Vector vtr;
/* These two vars are for the DIV and MOD functions */
private int nPages;
private int nRemaining;
/* You will use this in keyPressed */
private int choice = 0;
/* You will use this procedure everytime there will be a change in the vector,
when you add or remove elements of it.. */
private void GetNumPages(){
nPages = vtr.size() / 10;/* Assuming you will display 10 lines per page */
nRemaining = vtr.size() % 10;
if (numRemaining > 0)
numPages = numPages + 1;
}
public void keyPressed(int key){
switch(key){
// LEFT
case -3:
choice--;
if (choice == -1){
choice = numPages - 1;
}
repaint();
break;
// RIGHT
case -4:
choice++;
if (choice == numPages){
choice = 0;
}
repaint();
break;
}
}
/* This is the procedure that will display the data in canvas, and should be inside the paint() function */
private void displayInfo(){
int max;
int start;
if (vtr.size() <= 10){
// If the size of the vector is less than 10 or 10 loop thru it then display
}else{
// if its > 10, we will be needing to page it.
start = (choice * 10);
if (choice < numPages - 1){// the value of choice here depends on
max = (choice + 1) * 10;// the keyPressed function
}else{
max = start + numRemaining;
}
for (int i = start; i < max; i++){
// Display
}
}
}
Set choice to zero before and after you enter and leave the part that you will display your vector's data.
Hope this helps..
Ollie
Philippines
Message was edited by:
tee.pee