日期:2014-05-20  浏览次数:20676 次

如何存取数据?
用户输入的数据需要保存,请问用什么函数可以实现,谢谢

刚接触手机编程,问题很肤浅,见笑了

------解决方案--------------------
要看你保存在什么地方,你可以保存到文件,也可以保存到RMS,看你设计而定,而且,没有一个方法就能实现的
------解决方案--------------------
能举个例子么?谢谢
------解决方案--------------------
Package javax.microedition.rms

用于进行存储的包,负责数据的存取
------解决方案--------------------
class RMS {
static String missionRMS = "MISSION_RMS ";
public RMS() {

}
void storeTask(Task task) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
// write task info
dos.writeInt(task.id);
dos.writeUTF(task.label);
dos.writeUTF(task.description);
dos.writeUTF(task.rewardInfo);
dos.writeInt(task.price);
// write components info
TaskComponent[] taskComponents = task.taskComponents;
int numTaskComponents = taskComponents.length;
dos.writeInt(numTaskComponents);
for (int i = 0; i < numTaskComponents; i++) {
TaskComponent component = taskComponents[i];
NPC npc = component.npc;
// write npc info in this component
dos.writeInt(npc.id);
dos.writeInt(npc.countryID);
dos.writeInt(npc.sex);
dos.writeInt(npc.ai);
dos.writeInt(npc.level);
dos.writeUTF(npc.name);
// component info
dos.writeInt(component.numWinTimes);
dos.writeInt(component.shouldWinSequentially);
}
byte data[] = baos.toByteArray();
RecordStore rs = openRecordStore();
if (rs != null) {
if (rs.getNumRecords() <= 0) {
rs.addRecord(data, 0, data.length);
System.out.println( "Adding new record ");
} else {
rs.setRecord(1, data, 0, data.length);
System.out.println( "updating record ");
}
rs.closeRecordStore();
System.out.println( "Task saved successfully. ");
}
} catch (Exception e) {
e.printStackTrace();
}
}
Task getTask() {
try {
RecordStore rs = openRecordStore();
if (rs == null || rs.getNumRecords() <= 0) {
if (rs != null) {
rs.closeRecordStore();
}
return null;
} else {
byte[] data = rs.getRecord(1);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
int id, price, numComponents;
String label, description, rewardInfo;
TaskComponent[] taskComponents;
id = dis.readInt();
label = dis.readUTF();
description = dis.readUTF();
rewardInfo = dis.readUTF();
price = dis.readInt();
System.out.println( "id : " + id);
System.out.println( "label : " + label);
System.out.println( "description : " + description);
System.out.println( "rewardInfo " + rewardInfo);
// components
numComponents = dis.readInt();
taskComponents = new TaskComponent[numComponents];
for (int i = 0; i < numComponents; i++) {
int npcID, npcCountryID, npcSex, npcAI, npcLevel, numWinTimes, shouldWinSequentially;
String npcName;
npcID = dis.readInt();
npcCountryID = dis.readInt();
npcSex = dis.readInt();
npcAI = dis.readInt();
npcLevel = dis.readInt();
npcName = dis.readUTF();
numWinTimes = dis.readInt();