我有一个应用程序,单击“开始”后会显示测试屏幕。现在,我添加了一个简历测试按钮,并想显示退出问题的位置。为此,我想将问题保存到内存中,以便可以从那里获取并显示它。我正在编写代码,请帮忙
package com.firstBooks.series.ui.screens;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import java.lang.String;
import com.firstBooks.series.AppConfig;
import com.firstBooks.series.db.DBMain;
import com.firstBooks.series.db.Question;
import com.firstBooks.series.db.parser.XMLParser;
import com.firstBooks.series.ui.UIinitialize;
import com.firstBooks.series.ui.managers.TopManager;
import com.vensi.api.ImgButtonField;
public class TestScreen extends ScreenManager implements AppConfig{
TopManager topManager = null;
VerticalFieldManager middleManager = null;
HorizontalFieldManager manager = null;
ImgButtonField A, B, C, D;
//static PersistentObject Store;
static int quesNum[];
int quesNumber;//quesnumberTest[];
LabelField question1, blank, choice1Label, choice2Label, choice3Label,
choice4Label;
FieldChangeListener checkAnswerListenerA, checkAnswerListenerB,
checkAnswerListenerC, checkAnswerListenerD;
public TestScreen() {
super();
drawScreen();
}
//static {
//Store = PersistentStore.getPersistentObject(0x3097b5a0b70e028fL);
//}
public void drawScreen() {
topManager = new TopManager(newTestButton, 0);
middleManager = new VerticalFieldManager(
VerticalFieldManager.VERTICAL_SCROLL
| VerticalFieldManager.VERTICAL_SCROLLBAR
| VerticalFieldManager.USE_ALL_WIDTH
| Field.FIELD_HCENTER) {
protected void sublayout(int maxWidth, int maxHeight) {
// maxwidth reduced by 15 to get padding along the edges
super.sublayout(maxWidth - 15, maxHeight - 10);
if (UIinitialize.displayType == Device_Storm) {
setExtent(maxWidth - 15, 347);
} else if (UIinitialize.displayType == Device_Tour) {
setExtent(maxWidth - 15, 263);
} else if (UIinitialize.displayType == Device_Curve) {
setExtent(maxWidth - 10, 165);
}
}
};
A = new ImgButtonField(buttonABkGrndDown, buttonABkGrnd,
Field.FOCUSABLE | Field.FIELD_HCENTER);
B = new ImgButtonField(buttonBBkGrndDown, buttonBBkGrnd,
Field.FOCUSABLE | Field.FIELD_HCENTER);
C = new ImgButtonField(buttonCBkGrndDown, buttonCBkGrnd,
Field.FOCUSABLE | Field.FIELD_HCENTER);
D = new ImgButtonField(buttonDBkGrndDown, buttonDBkGrnd,
Field.FOCUSABLE | Field.FIELD_HCENTER);
manager = new HorizontalFieldManager(Manager.NO_HORIZONTAL_SCROLL
| Manager.NO_VERTICAL_SCROLL | Field.USE_ALL_WIDTH
| Field.USE_ALL_HEIGHT) {
protected void sublayout(int maxWidth, int maxHeight) {
if (UIinitialize.displayType == Device_Storm) {
layoutChild(A, buttonABkGrnd.getWidth(), buttonABkGrnd
.getHeight());
setPositionChild(A, 25, 9);
layoutChild(B, buttonBBkGrnd.getWidth(), buttonBBkGrnd
.getHeight());
setPositionChild(B, 115, 9);
layoutChild(C, buttonCBkGrnd.getWidth(), buttonCBkGrnd
.getHeight());
setPositionChild(C, 205, 9);
layoutChild(D, buttonDBkGrnd.getWidth(), buttonDBkGrnd
.getHeight());
setPositionChild(D, 295, 9);
} else if (UIinitialize.displayType == Device_Tour) {
layoutChild(A, buttonABkGrnd.getWidth(), buttonABkGrnd
.getHeight());
setPositionChild(A, 41, 9);
layoutChild(B, buttonBBkGrnd.getWidth(), buttonBBkGrnd
.getHeight());
setPositionChild(B, 161, 9);
layoutChild(C, buttonCBkGrnd.getWidth(), buttonCBkGrnd
.getHeight());
setPositionChild(C, 281, 9);
layoutChild(D, buttonDBkGrnd.getWidth(), buttonDBkGrnd
.getHeight());
setPositionChild(D, 401, 9);
} else if (UIinitialize.displayType == Device_Curve) {
layoutChild(A, buttonABkGrnd.getWidth(), buttonABkGrnd
.getHeight());
setPositionChild(A, 33, 19);
layoutChild(B, buttonBBkGrnd.getWidth(), buttonBBkGrnd
.getHeight());
setPositionChild(B, 107, 19);
layoutChild(C, buttonCBkGrnd.getWidth(), buttonCBkGrnd
.getHeight());
setPositionChild(C, 182, 19);
layoutChild(D, buttonDBkGrnd.getWidth(), buttonDBkGrnd
.getHeight());
setPositionChild(D, 257, 19);
}
setExtent(maxWidth, maxHeight);
}
protected void paint(Graphics g) {
super.paint(g);
}
};
String title = "";
quesNumber = DBMain.getQuestionNumber();
System.out.println("the value of QUESNUMBER IS:"+quesNumber);
DBMain.curQuestion = (Question) XMLParser.questionList
.elementAt(quesNumber);
title = DBMain.curQuestion.getTitle();
String BlankSpace = " ";
String choice1 = "A. " + DBMain.curQuestion.getChoice1() + "\n" + " ";
String choice2 = "B. " + DBMain.curQuestion.getChoice2() + "\n" + " ";
String choice3 = "C. " + DBMain.curQuestion.getChoice3() + "\n" + " ";
String choice4 = "D. " + DBMain.curQuestion.getChoice4() + "\n" + " ";
question1 = new LabelField(title);
question1.setFont(myFontBoldSmall());
blank = new LabelField(BlankSpace);
blank.setFont(myFontBoldSmall());
choice1Label = new LabelField(choice1);
choice1Label.setFont(myFontPlainSmall());
choice2Label = new LabelField(choice2);
choice2Label.setFont(myFontPlainSmall());
choice3Label = new LabelField(choice3);
choice3Label.setFont(myFontPlainSmall());
choice4Label = new LabelField(choice4);
choice4Label.setFont(myFontPlainSmall());
DBMain.answer = DBMain.curQuestion.getAnswer();
//synchronized (Store) {
//Store.setContents(quesNumber+"");
//Store.commit();
//}
checkAnswerListenerA = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
DBMain.selectedAnswer = "A";
UiApplication.getUiApplication().pushScreen(new AnswerScreen());
UiApplication.getUiApplication().popScreen(getScreen());
}
};
A.setChangeListener(checkAnswerListenerA);
checkAnswerListenerB = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
DBMain.selectedAnswer = "B";
UiApplication.getUiApplication().pushScreen(new AnswerScreen());
UiApplication.getUiApplication().popScreen(getScreen());
}
};
B.setChangeListener(checkAnswerListenerB);
checkAnswerListenerC = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
DBMain.selectedAnswer = "C";
UiApplication.getUiApplication().pushScreen(new AnswerScreen());
UiApplication.getUiApplication().popScreen(getScreen());
}
};
C.setChangeListener(checkAnswerListenerC);
checkAnswerListenerD = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
DBMain.selectedAnswer = "D";
UiApplication.getUiApplication().pushScreen(new AnswerScreen());
UiApplication.getUiApplication().popScreen(getScreen());
}
};
D.setChangeListener(checkAnswerListenerD);
/*A.setChangeListener(checkAnswerListenerA);
B.setChangeListener(checkAnswerListenerB);
C.setChangeListener(checkAnswerListenerC);
D.setChangeListener(checkAnswerListenerD);*/
manager.add(new NullField(Field.FOCUSABLE));
manager.add(A);
manager.add(B);
manager.add(C);
manager.add(D);
manager.add(new NullField(Field.FOCUSABLE));
middleManager.add(question1);
middleManager.add(new NullField(Field.FOCUSABLE));
middleManager.add(blank);
middleManager.add(choice1Label);
middleManager.add(new NullField(Field.FOCUSABLE));
middleManager.add(choice2Label);
middleManager.add(new NullField(Field.FOCUSABLE));
middleManager.add(choice3Label);
middleManager.add(new NullField(Field.FOCUSABLE));
middleManager.add(choice4Label);
add(topManager);
add(middleManager);
add(manager);
}
protected void makeMenu(Menu menu, int instance) {
menu.add(new MenuItem("A", 20, 10) {
public void run() {
answerMenu("A");
}
});
menu.add(new MenuItem("B", 30, 20) {
public void run() {
answerMenu("B");
}
});
menu.add(new MenuItem("C", 40, 30) {
public void run() {
answerMenu("C");
}
});
menu.add(new MenuItem("D", 50, 40) {
public void run() {
answerMenu("D");
}
});
super.makeMenu(menu, instance);
}
private void answerMenu(String answer) {
DBMain.selectedAnswer = answer;
UiApplication.getUiApplication().pushScreen(new AnswerScreen());
UiApplication.getUiApplication().popScreen(getScreen());
}
protected boolean keyChar(char character, int status, int time) {
if (character == 'A' || character == 'a')
DBMain.selectedAnswer = "A";
else if (character == 'B' || character == 'b')
DBMain.selectedAnswer = "B";
else if (character == 'C' || character == 'c')
DBMain.selectedAnswer = "C";
else if (character == 'D' || character == 'd')
DBMain.selectedAnswer = "D";
else
return super.keyChar(character, status, time);
UiApplication.getUiApplication().pushScreen(new AnswerScreen());
UiApplication.getUiApplication().popScreen(getScreen());
return true;
}
protected void paint(Graphics g) {
g.drawBitmap(0, 0, UIinitialize.screenWidth, UIinitialize.screenHeight,
backgroundBitmap, 0, 0);
subpaint(g);
TestScreen.this.invalidate();
}
}
最佳答案
我从您的大型代码片段中看到,您有几个使用PersistentStore类但被注释掉的代码块。您所走的路是正确的-这是将数据持久保存到设备闪存的正确方法。如果您对此有更具体的问题,请完善答案以更具体。否则,请查看链接的javadoc,以获取有关如何使用该类的良好描述,包括代码示例。
关于java - 将数据保存到黑莓内存,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3162338/