PDA

View Full Version : ถาม เรื่องการทำ list สินค้า แล้วเอาไปเก็บใน RMS



fangoogle
13-02-2009, 07:19 AM
คือ ผมเอา โค๊ด textbox ที่ สามารถ เพิ่ม ลบ ได้เอามา แปลง ให้เป็น list ซึ่งจะเอาไปเก็บใน ฐานข้อมูล RMS เหมือน textbox แตมันติด error ครับ

คือผม เพิ่งจะหัดเล่นอ่ะคราฟ ไม่ค่อยจะเข้าใจสักหน่อยอ่ะคราฟ ยังไงรบกวน ด้วยนะคราฟ

fangoogle
13-02-2009, 08:37 AM
เพิ่มเติมคับ

นี่ตัวอย่างโค้ด ที่เป็น text boxx อ่ะคับ

อย่างให้แปลงเป็น List แบบ Multi



import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class RMSTest extends MIDlet implements CommandListener{

private Display mDisplay;
private Form mForm1, mForm2;
private TextField nameText, numText;
private StringItem listString;
private Command saveCommand, listCommand, deleteCommand,
exitCommand, backCommand;
private static String DATABASE = "order";

public RMSTest(){
mForm1 = new Form("Record Manager");
nameText = new TextField("Order", "", 20, TextField.ANY);
numText = new TextField("Price", "", 20, TextField.NUMERIC);

saveCommand = new Command("Save", Command.SCREEN, 1);
listCommand = new Command("List all", Command.SCREEN, 2);
deleteCommand = new Command("Delete all", Command.SCREEN, 3);
exitCommand = new Command("Exit", Command.EXIT, 4);

mForm1 .append(nameText);
mForm1 .append(numText);

mForm1 .addCommand(saveCommand);
mForm1 .setCommandListener(this);
mForm1 .addCommand(listCommand);
mForm1 .setCommandListener(this);
mForm1 .addCommand(deleteCommand);
mForm1 .setCommandListener(this);
mForm1 .addCommand(exitCommand);
mForm1 .setCommandListener(this);

mForm2 = new Form("List Record");

listString = new StringItem(null,null);

backCommand = new Command("Back", Command.SCREEN, 0);

mForm2.append(listString);
mForm2.addCommand(backCommand);
mForm2.setCommandListener(this);


}

public void startApp(){

mDisplay = Display.getDisplay(this);
mDisplay.setCurrent(mForm1);

}

public void pauseApp(){}
public void destroyApp(boolean uncondition){}
public void commandAction(Command c, Displayable d){

if (c == exitCommand){
destroyApp(false);
notifyDestroyed();
}


if (c == saveCommand){
SaveData();
}



if (c == listCommand){
ListData();
}



if (c == deleteCommand){
DeleteData();
}


if (c == backCommand){
mDisplay.setCurrent(mForm1);
}


}




public void SaveData(){

RecordStore rs;
String name = nameText.getString();
String num = numText.getString();
String str = name + " [" + num + "]";

byte[] data = str.getBytes();

try{

rs = RecordStore.openRecordStore(DATABASE, true);
rs.addRecord(data, 0, data.length);
rs.closeRecordStore();

nameText.setString(null);
numText.setString(null);

}
catch (RecordStoreException e){
System.out.println(e);
}
}


public void ListData(){

RecordStore rs;
StringBuffer strRecord = new StringBuffer();
StringBuffer strAll = new StringBuffer();
mDisplay.setCurrent(mForm2);
listString.setText(null);

try{
rs = RecordStore.openRecordStore(DATABASE, true);
try{
int lastID = rs.getNumRecords();
byte[] data = new byte[100];
int size;

for (int i = 1; i <=lastID; ++i )
{
try{
size = rs.getRecordSize(i);
rs.getRecord(i, data, 0);
strRecord.setLength(0);

for (int j = 0; j < size; j++){
strRecord.append((char) data[j]);
}
strAll.append(i + ". " + strRecord + "\n");

}catch (InvalidRecordIDException e){
continue;
}
}

}catch (RecordStoreException e){
System.out.println("Exception reading record store: " + e);
}

listString.setText(strAll.toString());
rs.closeRecordStore();


}catch (RecordStoreException e){
System.out.println(e);
}
}



public void DeleteData(){
try
{
RecordStore.deleteRecordStore(DATABASE);
}
catch (RecordStoreException e){
System.out.println(e);
}
}




};