Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Regular Contributor
Posts: 52
Registered: ‎06-24-2010
My Device: T-Mobile
My Carrier: 3

Re: Persistent Store Question

Thanks,
My goal in my application especially is to create a bookmark menu for user so they can add bookmark for each page they have read or seen,view or delete the bookmark. I really need your help, Now I have used and adds the ListField class for display item in the list to the application with class name "ColoredListField", The code are :

public class ColouredListField extends ListField
{

private static final int LIGHT_TEXT = Color.WHITE;
private static final int DARK_TEXT = Color.BLACK;
private static final int BACKGROUND = Color.DARKBLUE;
private static final int HIGHLIGHT = Color.BLUE;
private final int[] cols = new int[]{BACKGROUND,HIGHLIGHT,LIGHT_TEXT,BACKGROUND};
private boolean hasFocus; //=false

public ColouredListField()
{
super();
}

public ColouredListField(int size)
{
super(size);
}

public ColouredListField(int numRows, long style)
{
super(numRows,style);
}

//Handles moving the focus within this field.
public int moveFocus(int amount, int status, int time)
{
invalidate(getSelectedIndex());
return super.moveFocus(amount,status,time);
}

//Invoked when this field receives the focus.
public void onFocus(int direction)
{
hasFocus = true;
super.onFocus(direction);
}

//Invoked when a field loses the focus.
public void onUnfocus()
{
hasFocus = false;
super.onUnfocus();
invalidate();
}

//Over ride paint to produce the alternating colours.
public void paint(Graphics graphics)
{
//Get the current clipping region as it will be the only part that requires repainting
XYRect redrawRect = graphics.getClippingRect();
if(redrawRect.y < 0)
{
throw new IllegalStateException("Clipping rectangle is wrong.");
}

//Determine the start location of the clipping region and end.
int rowHeight = getRowHeight();

int curSelected;

//If the ListeField has focus determine the selected row.
if (hasFocus)
{
curSelected = getSelectedIndex();
}
else
{
curSelected = -1;
}

int startLine = redrawRect.y / rowHeight;
int endLine = (redrawRect.y + redrawRect.height - 1) / rowHeight;
endLine = Math.min(endLine, getSize() - 1);
int y = startLine * rowHeight;

//Setup the data used for drawing.
int[] yInds = new int[]{y, y, y + rowHeight, y + rowHeight};
int[] xInds = new int[]{0, getPreferredWidth(), getPreferredWidth(), 0};

//Get the ListFieldCallback.
//This sample assumes that the object returned by the get
//method of the callback is a String or has a toString method.
//If this is not the case you will need to add the required logic
//for your implementation.
ListFieldCallback callBack = this.getCallback();

//Draw each row
for(; startLine <= endLine; ++startLine)
{
if (startLine % 2 == 0 && startLine != curSelected)
{
//Draw the even and non selected rows.
graphics.setColor(LIGHT_TEXT);
graphics.drawShadedFilledPath(xInds, yInds, null, cols, null);
graphics.drawText((String)callBack.get(this, startLine), 0, yInds[0]);
graphics.setColor(DARK_TEXT);
}
else
{
//Draw the odd or selected rows.
graphics.drawText((String)callBack.get(this, startLine), 0, yInds[0]);
}

//Assign new values to the y axis moving one row down.
y += rowHeight;
yInds[0] = y;
yInds[1] = yInds[0];
yInds[2] = y + rowHeight;
yInds[3] = yInds[2];
}
}

I used the above codes to create ListField for displaying all items from store object with the code below:

public class ViewBookmark extends MenuCov implements ListFieldCallback, BookMarkResResource{


Vector _listElements;
PersistentObject store;
public final static long key = 0x230d57d6848faeffL;
private static ResourceBundle _res =
ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

public ViewBookmark()

{
store = PersistentStore.getPersistentObject(key);
_listElements = new Vector();

setTitle(new LabelField("Bookmark"));

ColouredListField colourList = new ColouredListField();

//Set the ListFieldCallback
colourList.setCallback(this);


Vector currentinfo = (Vector) store.getContents();
if (currentinfo != null) {
//String data1 = currentinfo.
//Dialog.inform(currentinfo);
}
//int bykInfo = currentinfo.length;
//Dialog.inform(Integer.toString(bykInfo));
String _elements[] = new String[currentinfo.size()];
int elementLength = _elements.length;
for(int i = 0;i<currentinfo.size();i++)
{
_elements[i] = (currentinfo.elementAt(i)).toString() ;
colourList.insert(i);
this.insert(_elements[i], i);
}
/*for(int i=0;i<_elements.length;i++)
{
_elements= currentinfo;
}*/

// try{



// colourList.insert(NoHal-1);
/* }
catch(ArrayIndexOutOfBoundsException e)
{
e.toString();
}

*/ //Populate the ListField & ListFieldCallback with data.
/* for(int count = 0; count < elementLength; ++count)
{

colourList.insert(count);
this.insert(_elements[count], count);
//get(colourList,count-1);
}*/

//Add the ListField to the screen.

add(colourList);

//Push the screen onto the display stack.
// pushScreen(myScreen);

}

//Draws the list row.


public void drawListRow(ListField list, Graphics g, int index, int y, int w)
{

g.drawText(list.toString(), w, y);
//We don't need to draw anything here because it is handled
//by the paint method of our custom ColouredListField.
}

//Returns the object at the specified index.
public Object get(ListField list, int index)
{
return _listElements.elementAt(index);
}

//Returns the first occurence of the given String, bbeginning the search at index,
//and testing for equality using the equals method.
public int indexOfList(ListField list, String p, int s)
{
//return listElements.getSelectedIndex();
return _listElements.indexOf(p, s);
}

//Returns the screen width so the list uses the entire screen width.
public int getPreferredWidth(ListField list)
{
return Graphics.getScreenWidth();
}

//Adds a String element at the specified index.
public void insert(String toInsert, int index)
{
_listElements.insertElementAt(toInsert, index);
}

//Erases all contents.
public void erase()
{
_listElements.removeAllElements();
}

private static final class StoreInfo implements Persistable
{
private Vector _elementku;
public static final int NAME = 0;
public StoreInfo()
{
_elementku = new Vector(1);
for(int i = 0; i<_elementku.capacity(); i++)
{
_elementku.addElement(new String(""));
}
}
public String getElements(int id)
{
return (String)_elementku.elementAt(id);
}
public void setElement(int id, String value)
{
_elementku.setElementAt(value, id);
}
}

And for adding bookmark class that is called when user choose menu addbookmark, the code are as follows :

public class BookMark extends MenuCov implements BookMarkResResource
//this class is used to add bookmark
{

Vector data;
String noHal;
PersistentObject store;
public final static long key = 0x230d57d6848faeffL;
private static ResourceBundle _res =
ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);
public BookMark(int NoHal)
{
store = PersistentStore.getPersistentObject(key);
this.noHal= Integer.toString(NoHal);

simpan_data(Integer.toString(NoHal));
Dialog.inform("Sukses menambah bookmark");
// getApp().ViewBookMark();
// String[] _elements = new String[10];

}
public void simpan_data(String no) {
String [] hal1 = new String [183];
Vector data = new Vector();
if(no.equals("1"))
{
hal1[0] = "page 1 of 182" ;
data.addElement(hal1[0]);
// store.setContents(data);
}
else if(no.equals("2"))
{
hal1[1] = "page 2 of 182" ;
data.addElement(hal1[1]);
// store.setContents(data);
}
else
{
hal1[2] = "page 3 of 182" ;
data.addElement(hal1[2]);
// store.setContents(data);
}
/*for(int i=1; i<=182;i++)
{
hal1[i] = "page "+ i + " of 182";
data.addElement(hal1[i]);
}*/
store.setContents(data);
store.forceCommit();

when I add bookmark for the first time, it can be displayed by choose menu "view bookmark" which call the ViewBookmark class. But when I try add bookmark for the second and the third time, the last bookmark is replaced by the new one. I have tried to display it by Dialog.inform(.....), but only one data is displayed, that is the last bookmark that has been added.

So I think there is a mistake with my Bookmark class that is used to add page number, please help me, what is wrong with this code? I have used vector, but it doesn't work. Only one data is added to the store and it replaces the last one. Please help, I really need your help?what change should I do and make to the above code?

Best Regards,
Bagir