佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1168|回复: 6

J2ME RMS问题!!!

[复制链接]
发表于 24-11-2009 02:36 PM | 显示全部楼层 |阅读模式
当按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) {
    }}}}
回复

使用道具 举报


ADVERTISEMENT

发表于 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 | 显示全部楼层

回复 5# 兔仙人 的帖子

谢谢你的回复,我再试试看
回复

使用道具 举报

Follow Us
 楼主| 发表于 26-11-2009 02:01 AM | 显示全部楼层

回复 6# mikeng 的帖子

做到了。无论如何,真的谢谢你。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 2-12-2025 06:44 PM , Processed in 0.140493 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表