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
Skatedawg
Posts: 39
Registered: ‎04-11-2009
My Device: Not Specified
Accepted Solution

Refresh ListField and Multiple Store Entries

[ Edited ]

I am having a problem with trying to get my program to save infomration. I recently finally got it to show an entry but i dont think im doing something right because it just shows the same entry over and over.

 

Im trying to get it to save all the expenses from the expense form and then display them in the WelcomeScreen's listbox. I will include the store file, welcomeScreen and expense file.

 

What would be the best way to store this information and display it in the listfield? Also when you return to the WelcomeScreen how can i have that refresh so the listfield is updated?

 

WelcomeScreen:

 

 

package moneyMasterApp;

import java.util.Vector;

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;


public class WelcomeScreen extends UiApplication implements StoreResource {
	//Declare Variables
	//create a button here with the text 'Submit!'
		//ButtonField.CONSUME_CLICK will prevent the Menu from showing up when the user clicks the button.
		ButtonField btnDeposit = new ButtonField("Deposit", Field.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
		ButtonField btnExpense = new ButtonField("Expense", Field.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
		UiApplication nxtscreen = UiApplication.getUiApplication();
		HorizontalFieldManager _FieldManagerTop;
		LabelField myTitleLabel;
		BitmapField bitpam = new BitmapField();
		Background bg;
		LabelField label = new LabelField("Please Select Action:");
		FieldListener listener = new FieldListener();
		private static Vector _data;
		private static PersistentObject store;
		private ListField ExpenseList = new ListField();
		
	public static void main(String[] arg) {
		WelcomeScreen app = new WelcomeScreen();
		app.enterEventDispatcher();
	}
	
	public WelcomeScreen() {
		MainScreen mainScreen = new MoneyMakerMainScreen();
		//Set the title of the screen
		mainScreen.setTitle("MoneyMaster");
		//Add labels and other items
		_FieldManagerTop = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL);
	    myTitleLabel = new LabelField("MoneyMaster");
	   	bitpam = new BitmapField();
	   	bitpam.setBitmap(Bitmap.getBitmapResource("bbicon.png"));
	    _FieldManagerTop.add(myTitleLabel);
	    _FieldManagerTop.add(bitpam);
	    bg = BackgroundFactory.createLinearGradientBackground(0x2ebaeb, 0x2ebaeb, 0x7cbaed, 0x7cbaed);
	    mainScreen.getMainManager().setBackground(bg);
	    
	    
	    
	    synchronized(store) {
	    	Vector _data = (Vector) store.getContents();
	    	int count = _data.size();
	    	ExpenseList.setEmptyString("Nothing", DrawStyle.LEFT);
	    	ExpenseList.setSize(count);
	    }
	    
	    ExpenseList.setCallback(new ExpenseListCallback());
	    
	    //Add objects to screen
	    mainScreen.add(_FieldManagerTop);
	    mainScreen.add(label);
	    mainScreen.add(new SeparatorField());
	    btnDeposit.setChangeListener(listener);
	    btnExpense.setChangeListener(listener);
	    mainScreen.add(btnDeposit);
	    mainScreen.add(btnExpense);
	    mainScreen.add(new SeparatorField());
	    mainScreen.add(ExpenseList);
	    pushScreen(mainScreen);
	    
	    		
	}
	
	static {
    	//Retrieve a reference to a persistent object
    	//and set its contents to a new new vector if it is empty
    	store = PersistentStore.getPersistentObject(0xe69c43e620187619L);
    	//Hash of: "TechStormSolutions.MoneyMaster.Application"
    	synchronized(store) {
    		if (store.getContents() == null) {
    			store.setContents(new Vector());
    			store.commit();
    		}
    	}
    	//retrieve the contents of the PersistentObject
    	//and store in a Vector
    	_data = new Vector();
    	_data = (Vector)store.getContents();
    }
	

			
	private final class MoneyMakerMainScreen extends MainScreen {
		protected void makeMenu (Menu menu, int instance) {
			menu.add(deposit);
			menu.add(expense);
			super.makeMenu(menu, instance);
		}
		
		public void close() {
			Dialog.alert("Closing Application");
			super.close();
		}
	}
	
	private MenuItem deposit = new MenuItem("Deposit", 1, 100) {
		public void run() {
			UiApplication.getUiApplication().pushScreen(new depositForm());
		}
	};
	private MenuItem expense = new MenuItem("Expense", 2, 101) {
		public void run() {
			UiApplication.getUiApplication().pushScreen(new expenseForm());
		}
	};
	
	class FieldListener implements FieldChangeListener {

		public void fieldChanged(Field f, int context) {
			//if the deposit button is clicked
			if (f==btnDeposit) {
				UiApplication.getUiApplication().pushScreen(new depositForm());
			};
			if (f==btnExpense) {
				UiApplication.getUiApplication().pushScreen(new expenseForm());
			}
		}
		
	}
	
	final class ExpenseListCallback implements ListFieldCallback {

		public void drawListRow(ListField list, Graphics g,
				int index, int y, int w) {
			int count = _data.size();
			
			synchronized(store) {
				Vector _data = (Vector) store.getContents();
				if (!_data.isEmpty()) {
					String[] ExpList = new String[count];
					for (int i = 0; i < count; ++i) {
						StoreInitial info = (StoreInitial) _data.elementAt(i);
						ExpList[i] = info.getElement(StoreInitial.AMOUNT);
					}

					g.drawText(ExpList[0], 0,y,0,w);
				}
			}
			
		}

		public Object get(ListField listField, int index) {
			return null;
		}

		public int getPreferredWidth(ListField listField) {
			return Display.getWidth();
		}

		public int indexOfList(ListField listField, String prefix, int start) {
			return listField.indexOfList(prefix, start);
		}
		
	}
}
Developer
Skatedawg
Posts: 39
Registered: ‎04-11-2009
My Device: Not Specified

Re: Refresh ListField and Multiple Store Entries

[ Edited ]

ExpenseForm:

 

 

<p>&nbsp;</p>
<p>ExpenseForm</p>
<p>&nbsp;</p>
<pre>package moneyMasterApp;


import java.util.Vector;

import net.rim.device.api.i18n.*;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;


public class expenseForm extends MainScreen implements StoreResource {
	//Class Variables
    private EditField expAmount;
    private EditField expSource;
	private DateField expDateDay;  
	private String choiceItems[] = {"Income", "Payback", "Misc"};
	private ObjectChoiceField expType;
	private TextField expNotes;
	private ButtonField btnSubmit;
	private static Vector _data;
	private static PersistentObject store;
	
    public expenseForm() {
	    setTitle("Expense Form");
	    expAmount = new EditField("Enter the Expense Amount: $","", 8, EditField.FILTER_DEFAULT);
		expSource = new EditField("Expense Source: ", "");
		expDateDay = new DateField("Expense Date: ", System.currentTimeMillis(), DateFormat.getInstance(DateFormat.DATE_DEFAULT));
	    expType = new ObjectChoiceField("Select Expense Type: ", choiceItems);
	    expNotes = new TextField();
	    btnSubmit = new ButtonField("Submit", ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK );
	    ExpenseListner explistener = new ExpenseListner();
	    btnSubmit.setChangeListener(explistener);
	    	
	    //Set the size of some of the fields
	    expAmount.setMaxSize(8);
	    expSource.setMaxSize(20);
	    
	    //Add all Fields, Managers, and Buttons to screen
	    add(expAmount);
	    add(expSource);	    
	    add(expDateDay);
	    add(expType);
	    add(expNotes);
	    add(btnSubmit);
	}
    
    static {
    	//Retrieve a reference to a persistent object
    	//and set its contents to a new new vector if it is empty
    	store = PersistentStore.getPersistentObject(0xe69c43e620187619L);
    	//Hash of: "TechStormSolutions.MoneyMaster.Application"
    	synchronized(store) {
    		if (store.getContents() == null) {
    			store.setContents(new Vector());
    			store.commit();
    		}
    	}
    	//retrieve the contents of the PersistentObject
    	//and store in a Vector
    	_data = new Vector();
    	_data = (Vector)store.getContents();
    }
    
  
    class ExpenseListner implements FieldChangeListener {

		public void fieldChanged(Field f, int context) {
			if (f == btnSubmit) {
				//Create new object
				StoreInitial info = new StoreInitial();
				
				//Set StoreInitial elements by retrieving text that the user entered
				info.setElement(StoreInitial.AMOUNT, expAmount.getText());
				info.setElement(StoreInitial.SOURCE, expSource.getText());
				info.setElement(StoreInitial.DATE, String.valueOf(expDateDay.getDate()));
				info.setElement(StoreInitial.TRANSTYPE, "Expense");
				info.setElement(StoreInitial.TYPE, expType.getChoice(expType.getSelectedIndex()).toString());
				info.setElement(StoreInitial.NOTES, expNotes.getText());
				
				//add the new StoreInitial object to the vector
				_data.addElement(info);
				
				//add the vector of storeInfo objects to the persistent store
				synchronized(store) {
					store.setContents(_data);
					store.commit();
				}
				
				//display a dialog box that indicates that data was saved successfully
				Dialog.inform(expSource.getText() + "expense was saved successfully");
				
				//reset the UI fields to empty
				expAmount.setText(null);
				expSource.setText(null);
				expDateDay.setDate(null);
				expType.setChoices(null);
				expNotes.setText(null);
			}
		}
	}
		
}</pre>
<h6><br /></h6>
<p>&nbsp;</p>
<p>&nbsp;</p>

 

StoreInitial:

 

package moneyMasterApp;

import java.util.Vector;

import net.rim.device.api.util.Persistable;

public class StoreInitial implements Persistable {
	private Vector _elements;
		
		//Fields
		public static final int AMOUNT = 0;
		public static final int SOURCE = 1;
		public static final int DATE = 2;
		public static final int TRANSTYPE = 3;
		public static final int TYPE = 4;
		public static final int NOTES = 5;
		
		public StoreInitial() {
			_elements = new Vector(6);
			for (int i=0; i < _elements.capacity(); ++i) {
				_elements.addElement(new String(""));
			}
		}
		
		public void setElement(int id, String value) {
			_elements.setElementAt(value, id);
		}
		
		public String getElement(int id) {
			return (String) _elements.elementAt(id);
		}
}

 

 

 

New Developer
New Developer
CEC
Posts: 23
Registered: ‎04-22-2009
My Device: Not Specified

Re: Refresh ListField and Multiple Store Entries

g.drawText(ExpList[0], 0,y,0,w);

 

You are not incrementing through your results and instead always print the first item.

 

 

Developer
Skatedawg
Posts: 39
Registered: ‎04-11-2009
My Device: Not Specified

Re: Refresh ListField and Multiple Store Entries

That was just to make sure that it was printing something out on the list. Im not exactly sure how to get it so each deposit i put it can show up.

Developer
Skatedawg
Posts: 39
Registered: ‎04-11-2009
My Device: Not Specified

Re: Refresh ListField and Multiple Store Entries

For some reason when the list shows up now after modifying the code a little bit it shows all the information on top of each other. Different entries. For example. If you did a form with with the text "Hello World" and then did another form with "World Hello", it will show them on top of each other but there will be two rows like its suppose to.

Developer
Skatedawg
Posts: 39
Registered: ‎04-11-2009
My Device: Not Specified

Re: Refresh ListField and Multiple Store Entries

In my listcallback i needed to change that loop and take it out so that the index was the counter.

 

synchronized(store) {
				Vector _data = (Vector) store.getContents();
				if (!_data.isEmpty()) {
					String[] ExpList = new String[index];
					StoreInitial info = (StoreInitial) _data.elementAt(i);
					g.drawText(info.getElement(StoreInitial.AMOUNT), 0,y,DrawStyle.RIGHT,w);
				}
			}