Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
ashish117
Posts: 40
Registered: ‎07-20-2010
My Device: Not Specified
Accepted Solution

Saving contents of EditField

Hey,

 

I am trying to get a user to enter some information in an EditField() and then save the data in a sqlite database. this is what i have so far -

 

EditField fname = new EditField();


// there is more code here

 

        ver1.add(new RichTextField("First Name: "));
        ver1.add(fname);


// there is more code here

 

 final String fname_value = fname.getText();


        ButtonField save = new ButtonField("Save");
        
        FieldChangeListener listensave = new FieldChangeListener()
        {
            public void fieldChanged(Field field, int context)
            {
                savedatabase(fname_value,lname_value,pnumber_value);
            }
        };


        save.setChangeListener(listensave);

 

// there is more code here

 

    private void savedatabase(String a, String b, String c)
    {
        
        try
        {
            URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
                                   "MyTestDatabase.db");
            Database d;
            d = DatabaseFactory.open(myURI);
            Statement st = d.createStatement("INSERT INTO Data(FName,LName,Phone) VALUES('"+a+"','"+b+"',"+"223"+")");
            st.prepare();
            st.execute();
            st.close();
            d.close();
        }
        catch (Exception e)
        {
            // nothing
        }

 

 

When I run the code in the simulator, I can see that the value "223" is present in the last column of the database table. However, I cannot see any values in the first two columns.Can anyone please help me out with this one ?

 

Thanks a lot.

 

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Saving contents of EditField

Code like this makes more sense to me:

 

        ButtonField save = new ButtonField("Save");
       
        FieldChangeListener listensave = new FieldChangeListener()
        {
            public void fieldChanged(Field field, int context)
            {
                String fname_value = fname.getText();

               // set lname_value,in here too.
                savedatabase(fname_value,lname_value,pnumber_value);
            }
        };

 


        save.setChangeListener(listensave);

Developer
JCarty
Posts: 1,055
Registered: ‎01-25-2009
My Device: BlackBerry Bold 9650/BlackBerry Torch 9800

Re: Saving contents of EditField

It looks like you're retrieving the values before the person may type them in. Try to set your fname_value and lname_value just before calling your savedatabase method.

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev / #BlackberryDev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful
Developer
ashish117
Posts: 40
Registered: ‎07-20-2010
My Device: Not Specified

Re: Saving contents of EditField

Thank you. It worked.

Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: Saving contents of EditField

@peter_strange:

Some minor corrections in your code:

 

ButtonField save = new ButtonField("Save", ButtonField.CONSUME_CLICK);
        
FieldChangeListener listensave = new FieldChangeListener()
{
    public void fieldChanged(Field field, int context)
    {
        String fname_value = fname.getText();
        // set lname_value,in here too.
        savedatabase(fname_value,lname_value,pnumber_value);
        fname.setDirty(false);
        save.setDirty(false);
    }
};

Otherwise - totally agree with both you and Jerome.

 

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!