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
eraman
Posts: 34
Registered: ‎03-08-2012
My Device: Simulator
My Carrier: Developer

Showing Parsed data in object choice field.

I have done XML Parsing and it is shown on the screen in a text format.Now I want to access that data and show it in an object choice field. In my Application there are continents and their respective citites and on the basis of selection of the continents the cities should be filtered and shown in another choice field.Kindly hep me........I m attaching the code with it...please help me out here.....thanks in advance.

 

       

package mypackage;

import java.io.InputStream;
import javax.microedition.lcdui.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.Arrays;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;

public final class MyScreen extends MainScreen implements FieldChangeListener
{
	ObjectChoiceField basecity;
	ObjectChoiceField city;
	String region;
	
	List cities;
	String mCities[];
	private static final String xmlFileName = "/xml/city.xml";
	 private static final int _tab = 4;
	 
	Arrays arr; 
	 
    {        
        setTitle("****************filtering cities***************");
   //     cities = new List();
        try 
        {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputStream inputStream = getClass().getResourceAsStream( xmlFileName );
            Document document = builder.parse( inputStream );
            
            Element rootElement = document.getDocumentElement();
            rootElement.normalize();
            
            displayNode( rootElement, 0 );
        } 
        catch ( Exception e ) 
        {
            System.out.println( e.toString() );
        }
                
        basecity = new ObjectChoiceField("Select Region:",new String[] {"Asia","Europe","North America"});
        add(basecity);
        basecity.setChangeListener(this);
        
        city = new ObjectChoiceField("Select city:",new String[]{});
        add(city);
        
          
   }
	 private void displayNode( Node node, int depth ) 
	    {        
	        if ( node.getNodeType() == Node.ELEMENT_NODE ) 
	        {
	            StringBuffer buffer = new StringBuffer();
	            indentStringBuffer( buffer, depth );
	            NodeList childNodes = node.getChildNodes();
	            int numChildren = childNodes.getLength();
	            Node firstChild = childNodes.item( 0 );
	            
	            if ( numChildren == 1 && firstChild.getNodeType() == Node.TEXT_NODE ) 
	            {
	                buffer.append( firstChild.getNodeValue() );
	                
	                if(firstChild.getNodeName().toString() == "region"){
	                	region=node.getNodeValue();
	                }
	               
	                if(buffer.toString().equals("city_name")){
	                	if(region == "1"){
	                		int count =mCities[0].length;
	                		System.out.println("tct : "+count);
	                		mCities[0][count]=node.getNodeValue().toString();
	                		
	                	}else if(region == "2"){
	                		int count =mCities[1].length;
	                		mCities[1][count]=node.getNodeValue().toString();
	                		
	                	}else if(region == "3"){
	                		int count =mCities[2].length;
	                		mCities[2][count]=node.getNodeValue().toString();
	                	}
	                
	                	//arr.add(0, )
	                	int ct = mCities.length;
	                	mCities[ct]=buffer.toString();
	                	 add( new RichTextField( buffer.toString() ) );
	                }
	               	                
	            } 
	            else 
	            {
	                buffer.append( node.getNodeName() );
	                add( new RichTextField( buffer.toString() ) );
	                
	                for ( int i = 0; i < numChildren; ++i ) 
	                
	                {
	                    displayNode( childNodes.item( i ), depth + 1 );
	                }
	            }
	        } 
	        else 
	        {
	            String nodeValue = node.getNodeValue();
	            if ( nodeValue.trim().length() != 0 ) 
	            {
	                StringBuffer buffer = new StringBuffer();
	                indentStringBuffer( buffer, depth );
	                buffer.append( nodeValue );
	                if(node.getNodeName().toString() == "region"){
	                	region = node.getNodeValue();
	                }
	               
	                if(buffer.toString().equals("city_name")){
	                	if(region == "1"){
	                		int count =mCities[0].length;
	                		System.out.println("tct : "+count);
	                		mCities[0][count]=node.getNodeValue().toString();
	                	}else if(region == "2"){
	                		int count =mCities[1].length;
	                		mCities[1][count]=node.getNodeValue().toString();
	                	}else if(region == "3"){
	                		int count =mCities[2].length;
	                		mCities[2][count]=node.getNodeValue().toString();
	                	}
	                
	        }
	            	int ct = mCities.length;
                	mCities[ct]=buffer.toString();
             
	                add( new RichTextField( buffer.toString() ) );
	            }
	        }
	    }
	    
	 private static void indentStringBuffer( StringBuffer buffer, int depth ) 
	    {
	        int indent = depth * _tab;
	        
	        for ( int i = 0; i < indent; ++i ) 
	        {
	            buffer.append( ' ' );
	        }
	    }
	    
    public void fieldChanged(Field field, int context){
    	if(field == basecity){
    		String s = basecity.getChoice(basecity.getSelectedIndex()).toString();
    		if(s == "Asia"){
    			city.setChoices(mCities[0]);
    		}else if (s == "Europe"){
    			city.setChoices(mCities[1]);
    			
    		}else if (s == "North America"){
    			city.setChoices(mCities[2]);
    			
    		}
    	}
    }
}

 

Please use plain text.
Developer
simon_hain
Posts: 15,806
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Showing Parsed data in object choice field.

xml parsing is a complex operation and should be done on a separate thread.
it is useful to finish the parsing and have a complete set of data, this can be presented on the UI.
instead of writing directly into richtextfields i would suggest to create data beans and fill them in your parser, when it is finished hand the beans over to the screen who can display them.
----------------------------------------------------------
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
Please use plain text.
Developer
eraman
Posts: 34
Registered: ‎03-08-2012
My Device: Simulator
My Carrier: Developer

Re: Showing Parsed data in object choice field.

I donot have any idea of creating a data bean.Can u plz give me a demo or any other logical way to solve my problem???

Please use plain text.
Developer
simon_hain
Posts: 15,806
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Showing Parsed data in object choice field.

most likely you just don't know the term "bean", you can google it, it's just a simple object holding your data.

in your case it seems that you retrieve cities, a bean would be called "City" and have setters and getters for attributes like name etc.

main concept is that you parse your data first, and display it afterwards.
----------------------------------------------------------
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
Please use plain text.
Developer
eraman
Posts: 34
Registered: ‎03-08-2012
My Device: Simulator
My Carrier: Developer

Re: Showing Parsed data in object choice field.

i m using this code but it is showing error can u plz correct my code here........

i m just passing values here and i have made a diferent class"City.java" in which declairation is done.......but there is a problem in the class below.....may be in declairation of variables on top....or any where else....

 

import java.io.InputStream;

import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.xml.parsers.*;
import org.w3c.dom.*;


public final class Xmlparsing extends MainScreen {
	public List city;//here i have declaired a city class in which i //have getters & setters
	City citiesList;
	private static final String xmlFileName = "/xml/city_list.xml";// assigning path of the xml file to the string
	private static final int tab = 4;

	public Xmlparsing() {
	try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			InputStream inputStream = getClass().getResourceAsStream( xmlFileName );
			Document document = builder.parse(inputStream);

			Element rootElement =                           document.getDocumentElement();
			rootElement.normalize();
                        displayNode(rootElement, 0);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}


	private void displayNode( Node node, int depth ) 
    {        

    	if ( node.getNodeType() == Node.ELEMENT_NODE ) 
        {
            StringBuffer buffer = new StringBuffer();
            indentStringBuffer( buffer, depth );
            NodeList childNodes = node.getChildNodes();
            int numChildren = childNodes.getLength();
            Node firstChild = childNodes.item( 0 );
            
            if ( numChildren == 1 && firstChild.getNodeType() == Node.TEXT_NODE ) 
            {
                buffer.append( node.getNodeName() ).append( " = \"" ).append( firstChild.getNodeValue() ).append( '"' );
                add( new RichTextField( buffer.toString() ) );
            } 
            else 
            {
                buffer.append( node.getNodeName() );
                add( new RichTextField( buffer.toString() ) );
                
                // Recursively visit to all this node's children.
                for ( int i = 0; i < numChildren; ++i ) 
                {
                    displayNode( childNodes.item( i ), depth + 1 );
                }
            }
        } 
        else 
        {
            String nodeValue = node.getNodeValue();
       
        	if ( nodeValue.trim().length() != 0 ) 
            {
                StringBuffer buffer = new StringBuffer();
                indentStringBuffer( buffer, depth );
                buffer.append( '"' ).append( nodeValue ).append( '"' );
              
/////////////inserting values in getters & setters////////////// 
               city = new getLocalCityData()
                {
                   citiesList = new ArrayList.cityfun();
            		
            		try{
            			
            			XmlPullParser xpp = getResources().getXml( xmlFileName );
            			
            			
            			 while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) {
            							City city = new City();
            			        
            							if (xpp.getEventType()==XmlPullParser.START_TAG) {
            			        	 
            				            if (xpp.getName().equals("city_id")) {
            				            	city.set_city_id(Integer.parseInt(xpp.getText()));
            				            }
            				            if (xpp.getName().equals("region_id")) {
            				            	city.set_region_id(Integer.parseInt(xpp.getText()));
            				            }
            				            if (xpp.getName().equals("city_name")) {
            				            	city.set_city_name(xpp.getText());
            				            }
            				            if (xpp.getName().equals("is_dst")) {
            				            	city.set_is_dst(Integer.parseInt(xpp.getText()));
            				            }
            				            if (xpp.getName().equals("end_date")) {
            				            	city.set_end_date(xpp.getText());
            				            }
            				            if (xpp.getName().equals("dst_time_interval")) {
            				            	city.set_dst_time_interval(Integer.parseInt(xpp.getText()));
            				    
            			            }//end of if(xpp)
            			        CitiesList.add(city);
            			 }//end of while
            			
            		}catch(Exception e){
            			
            		}//end of try catch
            		return CitiesList;
                }//end of function localcitydata()
              
        }//end of if(node value)
   }// end of else


	private static void indentStringBuffer(StringBuffer buffer, int depth) {
		int indent = depth * tab;

		for (int i = 0; i < indent; ++i) {
			buffer.append(' ');
		}
	}
}

 

Please use plain text.