BlackBerry 应用程序中的持久对象

发布于 2024-11-13 08:07:32 字数 3076 浏览 3 评论 0原文

我一直在尝试在黑莓应用程序中使用持久对象。 我正在制作一个登录页面,可以在应用程序中保存用户名和密码。以下是我在登录页面中使用的代码

public final class LoginScreen extends MainScreen
{
    private BasicEditField useremailField;
    private PasswordEditField passwordField;
    public HorizontalFieldManager hfm = new HorizontalFieldManager (FIELD_VCENTER);


    public Hashtable persistentHashtable;
    
    public PersistentObject persistentObject;
    
    public static final long KEY = 0x9df9f961bc6d6baL;

    public LoginScreen()
    {        
        super( MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR );
        // Set the displayed title of the screen       
        setTitle(new LabelField("Log In",  LabelField.HCENTER| LabelField.USE_ALL_WIDTH));
    
        persistentObject = PersistentStore.getPersistentObject(KEY); 
        
        useremailField = new BasicEditField( "Email Address:", "", 100, BasicEditField.EDITABLE );
        passwordField = new PasswordEditField( "Password: ", "", 100, PasswordEditField.EDITABLE);
        
        ButtonField registerField = new ButtonField( "Register", ButtonField.CONSUME_CLICK | ButtonField.FIELD_LEFT);
        ButtonField loginField = new ButtonField( "Login", ButtonField.CONSUME_CLICK | ButtonField.FIELD_RIGHT);
                
        
        add(new LabelField("\n\n"));
        add( useremailField );
        add(new LabelField("\n"));
        add( passwordField);
        add(new LabelField("\n"));
       
        hfm.add(registerField);
        hfm.add(loginField);
     
        setStatus(hfm);
     
        loginField.setChangeListener( new FieldChangeListener() 
        {
            public void fieldChanged( Field arg0, int arg1 )
            {
                login();
            }
        } );
        persistentHashtable.put("UsernameData", useremailField);
        persistentHashtable.put("PasswordData", passwordField);
        
        persistentObject.commit();
        
    }
    private void login() {
        UiApplication ui = UiApplication.getUiApplication();
        ui.pushScreen(new Loggedin());
            
    }
}

以下是登录页面的代码

public class Loggedin extends MainScreen
{
    private LabelField username;
    private LabelField password;
    
    public static PersistentObject persistentObject;
    public Loggedin()
    {
        super( MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR );
        setTitle( "Registration Form" );
         
        if (persistentObject.getContents() == null) {
            persistentHashtable = new Hashtable();
            persistentObject.setContents(persistentHashtable);
        }
        else {
            persistentHashtable = (Hashtable)persistentObject.getContents();
        }
        
        if (persistentHashtable.containsKey("EditData")) {
            username.setText((String)persistentHashtable.get("UsernameData"));
        }
        if (persistentHashtable.containsKey("BoolData")) {
            password.setText((String)persistentHashtable.get("PasswordData"));
        }
    }

}

我无法纠正该问题。

I have been trying to use the persistent object in BlackBerry Application.
I am making a login page where I can save the username and password in the application. Below is the code I used in the login page

public final class LoginScreen extends MainScreen
{
    private BasicEditField useremailField;
    private PasswordEditField passwordField;
    public HorizontalFieldManager hfm = new HorizontalFieldManager (FIELD_VCENTER);


    public Hashtable persistentHashtable;
    
    public PersistentObject persistentObject;
    
    public static final long KEY = 0x9df9f961bc6d6baL;

    public LoginScreen()
    {        
        super( MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR );
        // Set the displayed title of the screen       
        setTitle(new LabelField("Log In",  LabelField.HCENTER| LabelField.USE_ALL_WIDTH));
    
        persistentObject = PersistentStore.getPersistentObject(KEY); 
        
        useremailField = new BasicEditField( "Email Address:", "", 100, BasicEditField.EDITABLE );
        passwordField = new PasswordEditField( "Password: ", "", 100, PasswordEditField.EDITABLE);
        
        ButtonField registerField = new ButtonField( "Register", ButtonField.CONSUME_CLICK | ButtonField.FIELD_LEFT);
        ButtonField loginField = new ButtonField( "Login", ButtonField.CONSUME_CLICK | ButtonField.FIELD_RIGHT);
                
        
        add(new LabelField("\n\n"));
        add( useremailField );
        add(new LabelField("\n"));
        add( passwordField);
        add(new LabelField("\n"));
       
        hfm.add(registerField);
        hfm.add(loginField);
     
        setStatus(hfm);
     
        loginField.setChangeListener( new FieldChangeListener() 
        {
            public void fieldChanged( Field arg0, int arg1 )
            {
                login();
            }
        } );
        persistentHashtable.put("UsernameData", useremailField);
        persistentHashtable.put("PasswordData", passwordField);
        
        persistentObject.commit();
        
    }
    private void login() {
        UiApplication ui = UiApplication.getUiApplication();
        ui.pushScreen(new Loggedin());
            
    }
}

Below is the code of the logged in page

public class Loggedin extends MainScreen
{
    private LabelField username;
    private LabelField password;
    
    public static PersistentObject persistentObject;
    public Loggedin()
    {
        super( MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR );
        setTitle( "Registration Form" );
         
        if (persistentObject.getContents() == null) {
            persistentHashtable = new Hashtable();
            persistentObject.setContents(persistentHashtable);
        }
        else {
            persistentHashtable = (Hashtable)persistentObject.getContents();
        }
        
        if (persistentHashtable.containsKey("EditData")) {
            username.setText((String)persistentHashtable.get("UsernameData"));
        }
        if (persistentHashtable.containsKey("BoolData")) {
            password.setText((String)persistentHashtable.get("PasswordData"));
        }
    }

}

I haven't been able to rectify the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不喜欢何必死缠烂打 2024-11-20 08:07:32

您将您的信息放入哈希表 =O

 persistentHashtable.put("UsernameData", useremailField);
 persistentHashtable.put("PasswordData", passwordField);

并提交到持久对象!

 persistentObject.commit();

我认为这就是您不保存价值观的原因!

看这个例子:

    private static PersistentObject persistentStore;
    private static Object[] data = new Object[1];

//Load Username & Password
    public static void LoadContents(){
        Object[] temp;
        try{
            persistentStore = PersistentStore.getPersistentObject(GI.PERSISTANCEID);
            if(persistentStore.getContents()!=null){
                temp = (Object[]) persistentStore.getContents();
                if(temp!=null){
                    data[0] = temp[0];
                    data[1] = temp[1];                  
                }

                              username.setText((data[0]!=null)?(String)data[0]:"");
                              password.setText((data[1]!=null)?(String)data[1]:"");

            }else{
                data = new Object[1];
            }
        }catch(NullPointerException e){
            System.out.println("CA: S NPE "+e.getMessage());
        }catch(Exception e){
            System.out.println("CA: S E "+e.getMessage());
        }
    }



       //Save Username & Password
    public static void saveUsername(String version){
data[0] = version;
    persistentStore.setContents(data);
    persistentStore.commit();
    }

    public static void savePassword(String password){
        data[1] = password;
    persistentStore.setContents(data);
    persistentStore.commit();
    }

You are putting your info into a hashtable =O

 persistentHashtable.put("UsernameData", useremailField);
 persistentHashtable.put("PasswordData", passwordField);

and doing the commit to the persistentObject!

 persistentObject.commit();

I think thats the reason you are not saving the values!

See this example:

    private static PersistentObject persistentStore;
    private static Object[] data = new Object[1];

//Load Username & Password
    public static void LoadContents(){
        Object[] temp;
        try{
            persistentStore = PersistentStore.getPersistentObject(GI.PERSISTANCEID);
            if(persistentStore.getContents()!=null){
                temp = (Object[]) persistentStore.getContents();
                if(temp!=null){
                    data[0] = temp[0];
                    data[1] = temp[1];                  
                }

                              username.setText((data[0]!=null)?(String)data[0]:"");
                              password.setText((data[1]!=null)?(String)data[1]:"");

            }else{
                data = new Object[1];
            }
        }catch(NullPointerException e){
            System.out.println("CA: S NPE "+e.getMessage());
        }catch(Exception e){
            System.out.println("CA: S E "+e.getMessage());
        }
    }



       //Save Username & Password
    public static void saveUsername(String version){
data[0] = version;
    persistentStore.setContents(data);
    persistentStore.commit();
    }

    public static void savePassword(String password){
        data[1] = password;
    persistentStore.setContents(data);
    persistentStore.commit();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文