|
查看: 1168|回复: 6
|
J2ME RMS问题!!!
[复制链接]
|
|
|
当按login后,textfield成功Store入db,但如何做到choiceGroup也能呢?谢谢!以下是我的code:
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
public class RecordMIDlet extends MIDlet implements CommandListener {
private static final String kUser = "user";
private static final String kPassword = "password";
private Preferences mPreferences;
private Form mForm;
private TextField mUserField, mPasswordField;
private ChoiceGroup langGroup;
public RecordMIDlet() {
try {
mPreferences = new Preferences("LCKRecord");
} catch (RecordStoreException rse) {
return;
}
mForm = new Form("Login");
mUserField = new TextField("Name", mPreferences.get(kUser), 32, 0);
mPasswordField = new TextField("Password", mPreferences.get(kPassword),32, 0);
langGroup = new ChoiceGroup("Language", Choice.EXCLUSIVE);
langGroup.append("English", null);
langGroup.append("Chinese", null);
mForm.append(mUserField);
mForm.append(mPasswordField);
mForm.append(langGroup);
mForm.addCommand(new Command("Login", Command.OK, 0));
mForm.addCommand(new Command("Exit", Command.EXIT, 0));
mForm.setCommandListener(this);
}
class Preferences {
private String mRecordStoreName;
private Hashtable mHashtable;
public Preferences(String recordStoreName) throws RecordStoreException {
mRecordStoreName = recordStoreName;
mHashtable = new Hashtable();
load();
}
private void load() throws RecordStoreException {
RecordStore rs = null;
RecordEnumeration re = null;
try {
rs = RecordStore.openRecordStore(mRecordStoreName, true);
re = rs.enumerateRecords(null, null, false);
while (re.hasNextElement()) {
byte[] raw = re.nextRecord();
String pref = new String(raw);
int index = pref.indexOf('|');
String name = pref.substring(0, index);
String value = pref.substring(index + 1);
put(name, value);
}
} finally {
if (re != null)
re.destroy();
if (rs != null)
rs.closeRecordStore();
}
}
public String get(String key) {
return (String) mHashtable.get(key);
}
public void put(String key, String value) {
if (value == null)
value = "";
mHashtable.put(key, value);
}
public void save() throws RecordStoreException {
RecordStore rs = null;
RecordEnumeration re = null;
try {
rs = RecordStore.openRecordStore(mRecordStoreName, true);
re = rs.enumerateRecords(null, null, false);
while (re.hasNextElement()) {
int id = re.nextRecordId();
rs.deleteRecord(id);
}
Enumeration keys = mHashtable.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = get(key);
String pref = key + "|" + value;
byte[] raw = pref.getBytes();
rs.addRecord(raw, 0, raw.length);
}
} finally {
if (re != null)
re.destroy();
if (rs != null)
rs.closeRecordStore();
} } }
public void startApp() {
Display.getDisplay(this).setCurrent(mForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT) {
destroyApp(true);
notifyDestroyed();
}
if (c.getCommandType() == Command.OK) {
mPreferences.put(kUser, mUserField.getString());
mPreferences.put(kPassword, mPasswordField.getString());
try {
mPreferences.save();
} catch (RecordStoreException rse) {
}}}} |
|
|
|
|
|
|
|
|
|
|
发表于 24-11-2009 05:27 PM
|
显示全部楼层
如果 没记错 , CHOICEGROUP 是 可以 拿到 他的 VALUE 的 。。。。
int selectedType = group.getSelectedIndex();
[ 本帖最后由 兔仙人 于 24-11-2009 05:31 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 24-11-2009 10:39 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 25-11-2009 10:26 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 25-11-2009 10:53 PM
|
显示全部楼层
|
你 试试 看 你 有没有 getValue 之类的 。 只要 PASS HIM 你的 SELECTED INDEX 就 可以 拿到 你 的 VALUE 了 。。。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 25-11-2009 11:42 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 26-11-2009 02:01 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|