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
Trusted Contributor
kirthiga
Posts: 204
Registered: ‎07-14-2010
My Device: 8100
My Carrier: Blackberry developer
Accepted Solution

binding data to list field at runtime using http connection

hi all ,

 

am using dom parser to parse an xml as url . i need to bind only one element from that xml ,say sectionname..(its an element in my xml)

i need all the titles to get populated to a list field as soon as i  parse it ..

i got stucked in listfield part ..

 

can yu solve my issue ..

 

tjis is my code.. the list is empty .. and also i don know how to implement listfield in my code.

can anyone correct my existing code ..

import javax.microedition.io.*; 
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.system.*; 
import net.rim.device.api.xml.parsers.*; 
import org.w3c.dom.*; 
import java.util.*;
//import org.xml.sax.*; 

class myApplication extends UiApplication implements ListFieldCallback 
{ 
     MainScreen _screen= new MainScreen(); 
     String _node,_element; 
     Connection _connectionthread; 
     ListField _list = new ListField();
     private Vector listElements = new Vector();


public static void main(String arg[])
{ 
	myApplication application = new myApplication(); 
    application.enterEventDispatcher(); 
} 

public myApplication() 
{ 
    _screen.setTitle("welcome");
    _screen.add(new RichTextField("LOADING")); 
    _screen.add(new SeparatorField()); 
    ListFieldCallback _callback = null;
	_list.setCallback(_callback);
    //_screen.add(_list);
     pushScreen(_screen); 
     

    _connectionthread = new Connection(); 
    _connectionthread.start();
} 

public void drawListRow(ListField list, Graphics g, int index,
		int y, int width) {
	String text = (String)listElements.elementAt(index);
    g.drawText(text , 0, y, 0, width);
	

	
	
}

public Object get(ListField list, int index) {
	// TODO Auto-generated method stub
	 return listElements.elementAt(index);
	//return null;
	

}

public int getPreferredWidth(ListField list) {
	// TODO Auto-generated method stub
	return Graphics.getScreenWidth();

}
//public void insert(String toInsert, int index) {
   // listElements.insertElementAt(toInsert, index);
//}
public void insert(String toInsert, int index) 
{
    listElements.addElement(toInsert);
}



public int indexOfList(ListField list, String prefix, int start) {
	// TODO Auto-generated method stub
	return -1;
	//return 0;
}
public void updateField(String node, String element){ 

     String title="SectionName"; 
     //_screen.add(new RichTextField(" . "+element)); 
    listElements.addElement(element);
     _screen.add(_list);
         if(node.equals(title))
         { 
             _screen.add(new SeparatorField()); 
         } 
} 

private class Connection extends Thread
{ 
    public Connection()
    { 
       super(); 
    } 

public void run()
    { 

     Document doc; 
     StreamConnection conn; 
     try{ 
     conn=(StreamConnection)Connector.open("http://..my url .....");  
     DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance(); 
     DocumentBuilder docBuilder= docBuilderFactory.newDocumentBuilder();  
     docBuilder.isValidating(); 
     doc = docBuilder.parse(conn.openInputStream()); 
     doc.getDocumentElement ().normalize (); 
     NodeList list=doc.getElementsByTagName("SectionName"); 
    _node=new String(); 
    _element = new String(); 
     Object obj = Application.getEventLock();
     for (int i=0;i<list.getLength();i++)
     {
     synchronized(obj)
     { 
     Node value=list.item(i).getChildNodes().item(0); 
    _node=list.item(i).getNodeName(); 
    _element=value.getNodeValue(); 
     updateField(_node,_element); 
     
     
     } 
     
     }
}

catch (Exception e)
   { 
      System.out.println(e.toString()); 
   } 
myApplication.this.invokeLater(new Runnable()
{    public void run() 
{     
	_list.setSize(listElements.size());   
	_screen.invalidate();    
}
});
}
}





}

 

 

Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: binding data to list field at runtime using http connection

First U have to Store All Item in Enumaration or Vector.

 then USe this.

            feedList.setCallback(this);
            add(feedList);

        feedList.setSize(feeds.numRecords());
        feedList.invalidate();

Please use plain text.
Trusted Contributor
kirthiga
Posts: 204
Registered: ‎07-14-2010
My Device: 8100
My Carrier: Blackberry developer

Re: binding data to list field at runtime using http connection

wat does this feedlist here in yur code refer to  in my program?

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: binding data to list field at runtime using http connection

I suggest you divide this problem into three parts:

a) Create ListField, assuming that the data is already downloaded - test that by itself.

b) Create a separate Thread to download the data and parse it into the data structure that you need to display in your ListField

c) Use a call back mechanism to allow you to update your ListField from the network Thread once the data is downloaded.

 

Unfortunately trying to all three at once, especially when you are not familiar with the processing, is going to make it difficult to isolate and solve your problems in a sensible way.

Please use plain text.
Trusted Contributor
kirthiga
Posts: 204
Registered: ‎07-14-2010
My Device: 8100
My Carrier: Blackberry developer

Re: binding data to list field at runtime using http connection

download the data in the sense ,wat does this mean ? am sorry if its a silly ques ....

yu mean to imlplement a list by enterimg the datas manually ?

Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: binding data to list field at runtime using http connection

feedList is the ListField Object.

 

Please use plain text.
Trusted Contributor
kirthiga
Posts: 204
Registered: ‎07-14-2010
My Device: 8100
My Carrier: Blackberry developer

Re: binding data to list field at runtime using http connection

fine ... and another doubt .. may i know how to use the variables of one java file to another ..

that is am using a package ,in which i have say, 1.java and 2.java file .. suppose i need the variables used in 1.java in 2.java also .. how this could be done ?

Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: binding data to list field at runtime using http connection

Yes First way U can do with the getter setter Method.

Another way is U have to declare the variable as Final.

then USed of this type Like firstclass.varname;

 

 

 

 

If got the ans then click "Accept as solution" Button.

 

Thanks and Regards

Nitin Verma

Please use plain text.
Trusted Contributor
kirthiga
Posts: 204
Registered: ‎07-14-2010
My Device: 8100
My Carrier: Blackberry developer

Re: binding data to list field at runtime using http connection

thank yu .... and is listfield  available in jde5.0 version... or  any other option is available to implement to listfield ...?

Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: binding data to list field at runtime using http connection

I am not getting what r u Asking Now.

Please use plain text.