0

I have a login page (username,password fields) that has a checkbox 'Remember me'. If the checkbox is selected, the application is required to remember the username & password for next login. That is done and working fine. However I am finding it hard to save the state of the checkbox field, i.e. whether it is checked or not. I am saving the username/password through the following code:

if (persistentObject.getContents() == null)
{
    persistentHashtable = new Hashtable();
    persistentObject.setContents(persistentHashtable);
} else {
    persistentHashtable = (Hashtable) persistentObject.getContents();
}

if (persistentHashtable.containsKey("username") && persistentHashtable.containsKey("password"))
{
    username.setText((String) persistentHashtable.get("username"));
    passwd.setText((String) persistentHashtable.get("password"));
}   

If the checkbox is selected and login is successfull, username and password are saved through the following:

if(checkBox1.getChecked() == true)
{
    persistentHashtable.put("username", user_id);
    persistentHashtable.put("password", password);
}

I tried to save the checkbox state with the line below but that is incorrect.

persistentHashtable.put("checkbox", checkBox1.setChecked(true));

Can somebody please help?

Nate
  • 31,017
  • 13
  • 83
  • 207
Sarah
  • 1,895
  • 2
  • 21
  • 39
  • 1
    Use something like this, checkBox1.getChecked() == true ? "TRUE" : "FALSE" for storing boolean value as a String. – Rupak Aug 29 '12 at 10:55

3 Answers3

2

RIM rapc.exe compiler does not support autoboxing (it works in java 1.3 compatibility mode), and then you need to wrap your boolean value to a Boolean class instance before saving it in a hashtable or passing it to persistent store.

0

Hey guys I managed to find a solution to my problem. I worked around by checking if the username field is empty, then the checkbox state should be 'unchecked' else it should be 'checked'. This is doing what I wanted. If anyone of you have a better way of doing this please do suggest. My working line of code is as below:

if(username.getText().length()==0)
                {
                    checkBox1 = new CheckboxField("Remember me",false);
                }
                else
                {
                    checkBox1 = new CheckboxField("Remember me",true);
                }

false = unchecked , true = checked

Sarah
  • 1,895
  • 2
  • 21
  • 39
0

Check box is used for user wishes , according to your code , If user have entered user name checkbox will be checked. Your Code is bit complex , First you need to setcontent code to persistent in very last , once you set your hashtable . You are having a login screen so you must have a submit button. Do like this on submit button event:

// to set persistent values
if(checkBox1.getChecked() == true)
{
 persistentHashtable = new Hashtable();
 persistentHashtable.put("username", user_id);
 persistentHashtable.put("password", password);
 persistentHashtable.put("checkbox", "true");
 persistentObject.setContents(persistentHashtable);
 persistentObject.commit() ;
}

// to get from  persistent values
if (persistentObject.getContents() != null) 
{
 persistentHashtable = (Hashtable) persistentObject.getContents();
 username.setText((String) persistentHashtable.get("username"));
 passwd.setText((String) persistentHashtable.get("password"));
 String boolval = (String) persistentHashtable.get("checkbox"); 
 if(boolval.equals("true"))
    checkBox1 = new CheckboxField("Remember me",true);
 else
    checkBox1 = new CheckboxField("Remember me",false);
}
Shashank Agarwal
  • 512
  • 3
  • 12
  • I get "The method put(Object, Object) in the type Hashtable is not applicable for the arguments (String, boolean)" when I try to put checkbox state in persistentHashtable. – Sarah Aug 30 '12 at 05:32
  • Ohh Yeah.. I haven't check the code. replace boolean value by string like "true" or "false" & check it while getting the value. It may work. – Shashank Agarwal Aug 30 '12 at 06:22