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
Highlighted
Regular Contributor
Posts: 83
Registered: ‎05-24-2011
My Device: Storm 9550
My Carrier: XXX

settings getting saved unintentionally

Hello,

 

First of all, I apologize in advance about my potentially vague post, but I will do my best to describe my problem without posting extensive code.

 

I created a customlistfield for my app, and with the class I have created an int called 'county'. To change county I have written a simple setter method that goes:

public void setCounty(int County){

county = County;

}

 

When I call the setCounty method, the county is set with in the method properly - I know this by adding the line

System.out.println(county);

 

The problem is, the variable is not set later on in the code when I need it to be set. I can set the value once, then after that, county remains the value that I set with the first call.

 

Additionally, if I hit the back button at any point while running the code, and the default message pops up asking me if I want to save changes, if I choose discard, then I can once again set the county variable, but only once before it gets stuck that value.

 

I will post my customlistfield in case it helps.

 

package net.creative.core;



/**
 * CustomListField is the class that controls all the listings
 */

import java.util.Vector;

import javax.microedition.location.LocationException;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EventInjector.TrackwheelEvent;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;

public class CustomListField extends ListField {

	  	  
	  private Vector content = null;
	  private ListCallback callback = null;
	  private Font font = null;
	  private Vector business;
	  private int county;
	  private int featuredNumber;
	  private Vector distance = new Vector();
	  private int color = Color.WHITE;
	  
		
	  private int currentPosition = 0;

	   
	  public CustomListField (Vector content, int number) {  
		 
	    this.content = content;
	    initCallbackListening();
	    font = this.getFont().derive(Font.PLAIN, 15);
	    this.setFont(font);
	    this.setRowHeight(-3);  
	    business = DirectoryScreen.business;
	    featuredNumber = number;
	    for(int i = 0; i < content.size(); i++)
	    distance.addElement("");
	  }
	  private void initCallbackListening() {
	    callback = new ListCallback();
	    this.setCallback(callback);
	  }

	
	
	  
	  //this is the callback needed to implement the customlistfield
	  private class ListCallback implements ListFieldCallback {
	  
	    private Bitmap image = null;
	    
	    public ListCallback() {
	      image = Bitmap.getBitmapResource("slider_arrow_right_13x13.png");
	    }

	    
	    public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width) {
		    if(index < content.size()){
			      int fontHeight = CustomListField.this.getFont().getHeight();
			      currentPosition = index;
			      int yPos = 5 + y; // initial position will consider an offset.				
			      int xPos = 15; // left offset, fixed for all rows in the list.
			      if(index < featuredNumber){
			    	  font = font.derive(font.BOLD, 18);
			    	  graphics.setColor(0xFFFF99);
			    	  graphics.fillRect(0, y, width, 3*fontHeight);
			    	  graphics.drawBitmap(5, yPos + 10, image.getWidth(), image.getHeight(), image, 0, 0);
			    	  xPos = xPos + image.getWidth();
			    	  graphics.setColor(Color.LIGHTGRAY);   
			    	  graphics.drawRect(0, y, width, 3*fontHeight);

			    	  String text = (String)content.elementAt(index);
			    	  graphics.setColor(Color.BLACK);
			      
			    	  
			    	  graphics.drawText(text, xPos, yPos + 10);
			      
			      }
			      else{
			    	  
			    	  
			      graphics.drawBitmap(5, yPos + 10, image.getWidth(), image.getHeight(), image, 0, 0);
			      xPos = xPos + image.getWidth();
			      graphics.setColor(Color.LIGHTGRAY);   
			      graphics.drawRect(0, y, width, 3*fontHeight);

			      //draw the distances for the businesses
			      if(!distance.elementAt(index).equals("")){
			    	  String text = (String)content.elementAt(index);
			    	  int blank = Display.getWidth() - image.getWidth() - font.getAdvance(text) - font.getAdvance(distance.elementAt(index).toString());
			    	  if(featuredNumber > 0)
			    		  blank = blank + Display.getWidth() / 2 + 40;
			    	  blank = blank / font.getAdvance(" ") - 6;
			    	  String blankString = "";
			    	  for(int i = 0; i < blank; i++){
			    		  blankString = blankString + " ";
			    	  }
			    	  graphics.setColor(Color.BLACK);
			    	  //make sure the business title fits in the listing space
			    	  //Dialog.alert("the distance is " + (Display.getWidth() - xPos - font.getAdvance(distance.elementAt(index).toString()) - 10));
			    	  if(font.getAdvance(text) > (Display.getWidth() - xPos - font.getAdvance(distance.elementAt(index).toString()) - 10)){
			    		  
			    		int last_space = text.lastIndexOf(' ');
			  			String temp = text.substring(0, last_space);
			  			String temp2;
			  			while(font.getAdvance(temp
			  					) > (Display.getWidth() - xPos - font.getAdvance(distance.elementAt(index).toString()) - 10)){
							last_space = temp.lastIndexOf(' ');
							temp = temp.substring(0, last_space);
						}
			  			temp2 = text.substring(0, last_space);
			  			temp = text.substring(last_space + 1);
			  			
			  			
			  			blank = Display.getWidth() - image.getWidth() - font.getAdvance(temp2) - font.getAdvance(distance.elementAt(index).toString());
				    	  blank = blank / font.getAdvance(" ") - 6;
				    	  blankString = "";
				    	  for(int i = 0; i < blank; i++){
				    		  blankString = blankString + " ";
				    	  }
			  			
			  			graphics.drawText(temp2 + blankString + distance.elementAt(index) , xPos, yPos + 3);
			  			graphics.drawText(temp, xPos, yPos + 19);
			    		  
			    	  }
			    	  else 
			    		  graphics.drawText(text + blankString + distance.elementAt(index) , xPos, yPos + 10);
			      }
			      //draw the categories
			      else{
			    	  String text = (String)content.elementAt(index);
			    	  
			    	  graphics.setColor(Color.BLACK);
			      
			    	  graphics.drawText(text, xPos, yPos + 10);
			    	  
			      }
			      }
			      }
			    }

	    public Object get(ListField listField, int index) {
	      return content.elementAt(index);
	    }

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

	    public int indexOfList(ListField listField, String prefix, int start) {
	      return content.indexOf(prefix, start);
	    }
	    
	  }

	  //this is a specific class to be used with the CreativeCore app to properly filter the 
	  //results by county
	  public void setCounty(int County){
		  county = County;
		  //fieldChangeNotify(0);
		  System.out.println("county has been set to " + county);
	  }
	  public int getCounty(){
		  return county;
	  }

	  public int getCurrentPosition() {
		  return currentPosition;
	  }	
	  
	  public void newElements(Vector v){
		  content.removeAllElements();
		  content = v;
	  }
	  void setDistance(Vector vector){
		  distance = vector;
	  }
	  void setDistanceSize(int size){
		  distance.setSize(Math.max(1, size));
	  }

      //Override function to switch picture
      protected void onFocus(int direction){
       color = Color.BLUE;
       invalidate();
      }
      //Override function to switch picture
      protected void onUnfocus(){
       color = Color.WHITE;
       invalidate();
      }
	  protected boolean navigationClick(int status, int time) {
	        this.fieldChangeNotify(1);
	        int company = 0;
			
			for(int i = 0; i < business.size(); i++){
				
				if(((Business) business.elementAt(i)).getCategory().equals((String)content.elementAt(getCurrentPosition()))){
					company = i;
				}else if ((((Business) business.elementAt(i)).getBusinessName()).equals((String)content.elementAt(getCurrentPosition()))){
					company = i;
				}			
			}
			
			Screen screenobject = UiApplication.getUiApplication().getActiveScreen();
			 if (screenobject instanceof DirectoryScreen ){
				 ListingScreen listscreen;
				try {
					listscreen = new ListingScreen((String) content.elementAt(getCurrentPosition()), county);
					UiApplication.getUiApplication().pushScreen(listscreen);
				} catch (LocationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	     		 
			 }
			 if (screenobject instanceof ToDoScreen){
				 if(ToDoScreen.flag ==0){
				 InfoListingScreen infolistscreen = new InfoListingScreen((Business) business.elementAt(company));
				 UiApplication.getUiApplication().pushScreen(infolistscreen);
				 }else ToDoList.getInstance().getVector().removeElementAt(currentPosition);
			 }
			 if (screenobject instanceof FavoritesScreen ){
				 if(FavoritesScreen.flag ==0){
    				 InfoListingScreen infolistscreen = new InfoListingScreen((Business) business.elementAt(company));
    				 UiApplication.getUiApplication().pushScreen(infolistscreen);
    				 }else FavsList.getInstance().getVector().removeElementAt(currentPosition);
			 }
			 if (screenobject instanceof CouponScreen ){
				 InfoListingScreen infolistscreen = new InfoListingScreen((Business) business.elementAt(company));
				 UiApplication.getUiApplication().pushScreen(infolistscreen);
			 }
			 if (screenobject instanceof SearchScreen ){
				 InfoListingScreen infolistscreen = new InfoListingScreen((Business) business.elementAt(company));
				 UiApplication.getUiApplication().pushScreen(infolistscreen);
			 }
			 else if(screenobject instanceof ListingScreen){
				 InfoListingScreen infolistscreen = new InfoListingScreen((Business) business.elementAt(company));
	     		 UiApplication.getUiApplication().pushScreen(infolistscreen);
			 }
			
	        return true;
	    }

	  
	  protected boolean touchEvent(TouchEvent message) {
		    switch (message.getEvent()) {
		    case TouchEvent.CLICK:
		        fieldChangeNotify(1);
		        int company = 0;
    			
    			for(int i = 0; i < business.size(); i++){
    				if(getCurrentPosition() < content.size()){
    					if(((Business) business.elementAt(i)).getCategory().equals((String)content.elementAt(getCurrentPosition()))){
    						company = i;
    					}else if ((((Business) business.elementAt(i)).getBusinessName()).equals((String)content.elementAt(getCurrentPosition()))){
    						company = i;
    					}			
    				}
    			}
    			
    			Screen screenobject = UiApplication.getUiApplication().getActiveScreen();
    			 if (screenobject instanceof DirectoryScreen ){
    				 ListingScreen listscreen;
					try {
						System.out.println("from customlistfield, county is " + county);
						listscreen = new ListingScreen((String) content.elementAt(getCurrentPosition()), county);
						UiApplication.getUiApplication().pushScreen(listscreen);
					} catch (LocationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
    	     		 
    			 }
    			 if (screenobject instanceof ToDoScreen){
    				 if(ToDoScreen.flag ==0){
    				 InfoListingScreen infolistscreen = new InfoListingScreen((Business) business.elementAt(company));
    				 UiApplication.getUiApplication().pushScreen(infolistscreen);
    				 }else ToDoList.getInstance().getVector().removeElementAt(currentPosition);
    			 }
    			 if (screenobject instanceof FavoritesScreen ){
    				 if(FavoritesScreen.flag ==0){
        				 InfoListingScreen infolistscreen = new InfoListingScreen((Business) business.elementAt(company));
        				 UiApplication.getUiApplication().pushScreen(infolistscreen);
        				 }else FavsList.getInstance().getVector().removeElementAt(currentPosition);
    			 }
    			 if (screenobject instanceof CouponScreen ){
    				 InfoListingScreen infolistscreen = new InfoListingScreen((Business) business.elementAt(company));
    				 UiApplication.getUiApplication().pushScreen(infolistscreen);
    			 }
    			 if (screenobject instanceof SearchScreen ){
    				 InfoListingScreen infolistscreen = new InfoListingScreen((Business) business.elementAt(company));
    				 UiApplication.getUiApplication().pushScreen(infolistscreen);
    			 }
    			 else if(screenobject instanceof ListingScreen){
    				 //UiApplication.getUiApplication().popScreen(screenobject);
    				 InfoListingScreen infolistscreen = new InfoListingScreen((Business) business.elementAt(company));
    	     		 UiApplication.getUiApplication().pushScreen(infolistscreen);
    			 }
    			 
    					        
		        // fall through
		    case TouchEvent.UNCLICK:
		        return true;
		        
		    }
		    return super.touchEvent(message);
		}
	  
	}

 

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

Re: settings getting saved unintentionally

This sounds to me like an Object problem rather than a set problem.  You perhaps create two CustomListField Objects, or recreate it, and you think you are referencing your new one in places that you are actually referencing your original one.