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
New Developer
Posts: 18
Registered: ‎09-05-2013
My Device: BOLD 9900
My Carrier: VodaCom

Saving

Please I have created a notepad-like app for my phone and the problem is that I can't save the note body together with the note name. The note name is an editfield which allows the user to type in the name he wants the note body to be saved with. And the notebody is a custom text field. I used persistent store in storing the note name and note body.

     This is the code for the custom text field.

import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class CustomTextField extends Manager {
	int managerWidth;
	int managerHeight;
	String _string;
	int id;
	
	private VerticalFieldManager vfm = new VerticalFieldManager(VERTICAL_SCROLLBAR|USE_ALL_WIDTH|USE_ALL_HEIGHT);
	private EditField editField = new EditField("","",EditField.DEFAULT_MAXCHARS,EditField.FOCUSABLE);

    public CustomTextField(int width, int height,String string,long style) {
		super(style|NO_VERTICAL_SCROLL|NO_HORIZONTAL_SCROLL);
		managerWidth = width;
		managerHeight = height;
		_string= string;
		add(vfm);
		vfm.add(editField);
		// TODO Auto-generated constructor stub
	}
	
	public CustomTextField(int width,int height,String string){
		this(width,height,string, 0L);
	}
    
	public String getText(){
		return editField.getText();
	}
	
	public int getPreferredHeight(){
		return managerHeight;
	}
	
	public int getPreferredWidth(){
		return managerWidth;
	}
	protected void sublayout(int w, int h) {
		if (managerWidth == 0){
			managerWidth = w;
		}
		if(managerHeight == 0){
			managerHeight = h;
		}
		int actWidth = Math.min(managerWidth, w);
		int actHeight = Math.min(managerHeight, h);
		
		layoutChild(vfm,actWidth-2,actHeight-2);
		
		setPositionChild(vfm,0,0);
		
		setExtent(actWidth,actHeight);
	}

	   protected void paint(Graphics g) {
		   int lineHeight = this.getFont().getHeight();
		   int lines = this.getHeight() / lineHeight;
		   for ( int i = 0; i < lines; i++ ) {
		   g.drawLine(0, lineHeight, this.getWidth(), lineHeight);
		   lineHeight = lineHeight + this.getFont().getHeight();
		   }
		    super.paint(g);
		    int prevColor = g.getColor();
		    g.setColor(prevColor);
		    g.drawRect(0, 0, getWidth(), getHeight()); 
		    g.setColor(prevColor);
		       }

	public CustomTextField getCustomTextField() {
		return null;
	}
	
		
	
}

 This is the code for saving

import net.rim.device.api.command.Command;
import net.rim.device.api.command.CommandHandler;
import net.rim.device.api.command.ReadOnlyCommandMetadata;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.util.StringProvider;


public class NewScreen extends MainScreen implements FieldChangeListener {

	private Manager _manager;
	int x=Display.getWidth(), y=Display.getHeight();

	ButtonField buttonField;
	GridFieldManager gfm;
	private EditField noteName;
	CustomTextField textField;
	private int _index;         
    private Penote _uiApp;
    private Notes _notes;
	
	public  NewScreen(Notes note,int index, boolean editable) {
		super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
		_notes = note;        
        _index = index;          
        
       
        _uiApp = (Penote)UiApplication.getUiApplication();
        
		
		noteName = new EditField("Note Name  ",_notes.getField(Notes.noteName));
		textField = new CustomTextField(x,475,_notes.getField(Notes.NOTES),0);
		HorizontalFieldManager hfm = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
		hfm.add(noteName);
		add(hfm);
		add(new SeparatorField());
		add(new SeparatorField());
		add(textField);
		
		
		_manager=getMainManager();
		Background bg =BackgroundFactory.createSolidBackground(Color.LIGHTYELLOW);
		_manager.setBackground(bg);
	    
				
	    MenuItem saveItem = new MenuItem(new StringProvider("Save"), 0x230020, 11);
        saveItem.setCommand(new Command(new CommandHandler() 
        {
            
            public void execute(ReadOnlyCommandMetadata metadata, Object context) 
            {
                if(onSave())
                {
                    close();                                     
                }
            }
        }));
        
        if(editable)
        {            
            setTitle("Edit Screen");   
            addMenuItem(saveItem);
            
        }
        else
        {
            setTitle(new LabelField("View Screen"));
            textField.setEditable(false);
            noteName.setEditable(false);
            
        }
        
       
	}
	 protected boolean onSave()
     {
         if(!(noteName.getText().equals("")))
         {
             _notes.setField(Notes.noteName,noteName.getText());
             _notes.setField(Notes.NOTES,textField.getText());
                                    
             _uiApp.saveNotes(_notes, _index);               
             return super.onSave();        
         }
         else
         {
        	 //Dialog.alert("Note name required");;
             return false;
         }
     }  
 protected boolean onSavePrompt(){
	 return true;
 }

public void fieldChanged(Field field, int x) {
	
	
}
}

 Thanks

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Saving

you posted a lot of code that has nothing to do with the issue.
please show the Notes class and the saveNotes function.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Developer
Posts: 18
Registered: ‎09-05-2013
My Device: BOLD 9900
My Carrier: VodaCom

Re: Saving

This is the saveNotes function

 public void saveNotes(Notes notes, int index)
    {  
        if(index >= 0)
        {
        	
            _screen.getModel().removeRowAt(index);
            _screen.getModel().insertRowAt(index, notes);
        }
        else
        {
            _screen.getModel().addRow(notes);
        }                   
    }

 And this is the Notes class

import net.rim.device.api.system.*;
import net.rim.device.api.util.*;

import java.util.*;

public class Notes implements Persistable {

	static final int noteName = 0;
	static final int NOTES = 1;
	 private static final int NUM_FIELDS = 2;
	
	 
	 private Vector _fields;
	 
	public Notes() {
		_fields = new Vector(NUM_FIELDS);
        for (int i = 0; i < NUM_FIELDS; ++i)
        {
            _fields.addElement("");
        }
	}
	
	 String getField(int id)
	    {        
	        Object encoding = _fields.elementAt(id);
	        
	        Object ticket  = PersistentContent.getTicket();
	        
	        if(ticket != null)
	        {
	            return PersistentContent.decodeString(encoding);
	        }
	        else
	        {
	            return null;
	        }    
	    }
	 
	 void setField(int id, String value)
	    {
	        Object encoding = PersistentContent.encode(value);
	        _fields.setElementAt(encoding, id);
	    } 

	 void reEncode()
	    {        
	        for (int i = 0; i < NUM_FIELDS; ++i)
	        {
	            Object encoding = _fields.elementAt(i);
	            if(!PersistentContent.checkEncoding(encoding))
	            {
	                encoding = PersistentContent.reEncode(encoding);
	                _fields.setElementAt(encoding, i);
	            }
	        }        
	    }
}

 Thanks

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Saving

to save a persistent object you have to set it to the store and call commit.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter