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
Super Contributor
Posts: 425
Registered: ‎01-02-2013
My Device: BOLD 9900
My Carrier: sdvsdvsd

help in List field

[ Edited ]

hi, i am getting data from json and showing in list filed , i am downloading the full data and then after downlaoding the data i am showing in list filed that takes time.., now i want show the items one bye one i mean when i download one item from json i want show that in list filed instead of getting all the data from json and then showing all the data like Lazy loading ..
i have used list.invalidate(index); method but that is calling the drawlistRow() method, i'll post my code , please tell me where should i change my code

public MyScreen()
    {        
       
        _list = new ListField();
        
        
        _list.invalidate();
		_list.setEmptyString("Please wait..", DrawStyle.HCENTER);
		_list.setRowHeight(100);
		_list.setCallback(this);
		add(_list);
		
		connectionthread = new Connection();
		connectionthread.start();
		
        
    }

 

public class Connection extends Thread{
    	
    	public void run() { 
    		try {
    			
    				
    			 hc = (HttpConnection)Connector.open(blogurl+ getConnectionString());//you have to use connection extension ";interface=wifi" this is only for wifi 
    	            URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
    	            //These are our appending values and tags 
    	            oPostData.append("p","1");
    	           
    	            hc.setRequestMethod(HttpConnection.POST);
    	            hc.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, oPostData.getContentType());
    	            byte [] postBytes = oPostData.getBytes();
    	            hc.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length));
    	            OutputStream strmOut = hc.openOutputStream();
    	            strmOut.write(postBytes);
    	            strmOut.flush();
    	            strmOut.close();

    	            String returnMessage = hc.getResponseMessage();
    	            System.out.println("=====returnMessage======="+returnMessage);
    	            if(hc.getResponseCode()==HttpConnection.HTTP_OK)
    	            {
    	                inputStream = hc.openInputStream();
    	                int c;
    	                while((c=inputStream.read())!=-1)
    	                {
    	                    stringBuffer.append((char)c);
    	                }
    	                System.out.println("\n\n>>>>>>>>>>>>>>>>>"+stringBuffer.toString());
    	                String responce = ""+stringBuffer.toString();
    	                System.out.println("\nresponce= "+responce);
    			

                JSONArray jsnarry = new JSONArray(responce);
    			
    			System.out.println("\n--length----- "+jsnarry.length());
    			
    			for (int i = 0; i < jsnarry.length(); i++){
    				
    				JSONArray inerarray = jsnarry.getJSONArray(i);
    				System.out.println("\n-innerarray-length----- "+inerarray.length());
    				
    				//for (int i1 = 0; i1 < inerarray.length(); i1++) {
    				    //System.out.println("\n-inerarray-values----- "+inerarray.getString(i1));
    				    String ID = inerarray.getString(0);
    				    String TITTLE = inerarray.getString(1);
    				    String CONTENT = inerarray.getString(2);
    				    String DATE = inerarray.getString(3);
    				    String IMAGE = inerarray.getString(4);
    				    String six = inerarray.getString(5);
    				    
    				    System.out.println("................................................");
    				    System.out.println("ID= "+ID);
    				    System.out.println("TITTLE= "+TITTLE);
    				    System.out.println("CONTENT= \n"+CONTENT);
    				    System.out.println("DATE= "+DATE);
    				    System.out.println("IMAGE= "+IMAGE);
    				    
    				    System.out.println("imagename= "+imagename);
    				    //System.out.println("six "+six);
    				    System.out.println("....................................................=");
    				   // String jsonresponse = ""+inerarray.getString(i1);
    				    //label = new LabelField(jsonresponse,LabelField.FOCUSABLE);
    			        //add(label);
    				//}
    				    if(CONTENT.equalsIgnoreCase("null")){
    						System.out.println("CONTENT nullnullnullnullnullnullnullnullnullnullnullnullnullnull");
    						CONTENT = " ";
    						
    					}
    				    elements.addElement(TITTLE);
    				    contentelements.addElement(CONTENT);
    				    datelements.addElement(DATE);
    				    Imageselements.addElement(imagename);
    				    

                              UiApplication.getUiApplication().invokeLater(new Runnable() {
					
					public void run() {
						// TODO Auto-generated method stub

						 _list.invalidate(i);
					}
				});
    				  
    				    
    			}
    			
    			
    			
    			
    			UiApplication.getUiApplication().invokeLater(new Runnable() { 
    				
    				public void run() { 
    					
    					synchronized (UiApplication.getEventLock())  {
    						
    						UiApplication.getUiApplication().popScreen(progressdialogue);
    				     
    					}
    					try {
    						
    						_list.setSize(elements.size());
    						_list.setSize(contentelements.size());
    						_list.setSize(datelements.size());
    						_list.setSize(Imageselements.size());
    						_list.invalidate();
    					
    					} catch (Exception e) {
    						// TODO Auto-generated catch block
    						System.out.println("error _list.setSize"+e.toString());
    					}
    			
    				} 
    			
    			});
    			}

            } catch (Exception e) {
                // TODO Auto-generated catch block
            	System.out.println("\nresponce code error "+e.toString());
            	UiApplication.getUiApplication().invokeLater(new Runnable() {
					
					public void run() {
						// TODO Auto-generated method stub

						Status.show("Check your internet connection!", 2000);
					}
				});
            	
            }
    		
    		
    		
    	}
    	
    }

 

 

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

Re: help in List field

are you sure that you receive your data one by one from json? because your code does not look that way.
i don't think you can get a speed increase the way you plan it.
----------------------------------------------------------
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
Super Contributor
Posts: 425
Registered: ‎01-02-2013
My Device: BOLD 9900
My Carrier: sdvsdvsd

Re: help in List field

here is my json response 

[
[
"123",
"Sunrays for Monday (Hindi) - Book release",
"Shri Nitin Gatkari released the Hindi translation of the 2nd of 5 already launched books on 30th August 2013 at New Delhi",
"2013-08-30",
"31aug_5.jpg",
"chandigarh,nitin gatkari,sunrays for monday,book release,"
],
[
"148",
"Sanjay Tandon in meeting with Shri Narendra Modi",
"Sanjay Tandon in official meet with Shri Narendra Modi",
"2013-06-07",
"7 june_2.jpg",
"Narendra Modi,Modi,Meeting,"
],
[
"152",
"BJP State President Sanjay Tandon in meeting with Smt. Sushma Swaraj",
"",
"2013-01-19",
"19-01-2013---02.jpg",
"Sushma Swaraj,"
],
[
"153",
"BJP State President Sanjay Tandon in meeting with Shri L.K. Advani",
"",
"2013-01-19",
"19-01-2013---01.jpg",
"LK Advani,Lal Krishan Advani,Meeting,"
],
[
"164",
"Meeting with the religious crowd in Chandigarh",
"BJP President Sanjay Tandon addressed the religious crown in a temple in Colony No. 5, Chandigarh",
"2013-04-01",
"1 april_1.jpg",
"Colony No. 5,Religious Crowd,Sanjay Tandon Addressing,"
],
[
"165",
"President BJP Chandigarh handing over the sword to Mr Amit Rana President Yuva Morcha BJP Chandigarh on the occasion of Yuva Sankalap day",
"",
"2013-04-28",
"28 april_4.jpg",
"Yuva Sankalp Day,Amit Rana,Yuva Morcha BJP,Sword,"
],
[
"166",
"Participating in Press conference addressed by Sh Anurag Thakur National President Yuva Morcha BJP.",
"BJP President Sanjay Tandon announcing Besharam Week with Yuva Morcha BJP president Anurag Thakur",
"2013-05-07",
"7 may_2.jpg",
"Yuva Morcha BJP,Anurag Thakur,Meeting,"
],
[
"175",
"Tibetan Member Parliament in meeting with Sanjay Tandon",
"Member of Tibetan parliament in exile submitted their letter to Chandigarh BJP president Sanjay Tandon to explain the atrocities committed by Chinese government on Tibetans.",
"2013-02-05",
"05-02-2013---01.jpg",
"Tibetal Member,Chinese Government,"
],
[
"176",
"Sanjay Tandon in meeting with Harsh Agnihotri",
"Sanjay Tondon, State  President BJP Chandigarh in meeting with Harsh Agnihotri  ",
"2013-02-10",
"10-02-2013---01.jpg",
"Harsh Agnihotri,Meeting,"
],
[
"177",
"BJP State President Sanjay tandon in meeting with Rajnath Singh",
"Sanjay Tandon in discussion with National President Shri Rajnath Singh Ji in New Delhi. ",
"2013-02-12",
"12-02-2013---01.jpg",
"Rajnath Singh,BJP National President,Meeting BJP,"
]
]

 and if you look at my code i'm parsing the data and adding it to a vector one by one and i'm calling list.invalidate(index); but list.invalidate(index); did not call the drawlistRow method 

JSONArray jsnarry = new JSONArray(responce);
    			
    			System.out.println("\n--length----- "+jsnarry.length());
    			
    			for (int i = 0; i < jsnarry.length(); i++){
    				
    				JSONArray inerarray = jsnarry.getJSONArray(i);
    				System.out.println("\n-innerarray-length----- "+inerarray.length());
    				
    				//for (int i1 = 0; i1 < inerarray.length(); i1++) {
    				    //System.out.println("\n-inerarray-values----- "+inerarray.getString(i1));
    				    String ID = inerarray.getString(0);
    				    String TITTLE = inerarray.getString(1);
    				    String CONTENT = inerarray.getString(2);
    				    String DATE = inerarray.getString(3);
    				    String IMAGE = inerarray.getString(4);
    				    String six = inerarray.getString(5);
    				    
    				    System.out.println("................................................");
    				    System.out.println("ID= "+ID);
    				    System.out.println("TITTLE= "+TITTLE);
    				    System.out.println("CONTENT= \n"+CONTENT);
    				    System.out.println("DATE= "+DATE);
    				    System.out.println("IMAGE= "+IMAGE);
    				    
    				    System.out.println("imagename= "+imagename);
    				    //System.out.println("six "+six);
    				    System.out.println("....................................................=");
    				   // String jsonresponse = ""+inerarray.getString(i1);
    				    //label = new LabelField(jsonresponse,LabelField.FOCUSABLE);
    			        //add(label);
    				//}
    				    if(CONTENT.equalsIgnoreCase("null")){
    						System.out.println("CONTENT nullnullnullnullnullnullnullnullnullnullnullnullnullnull");
    						CONTENT = " ";
    						
    					}
    				    elements.addElement(TITTLE);
    				    contentelements.addElement(CONTENT);
    				    datelements.addElement(DATE);
    				    Imageselements.addElement(imagename);
    				    

                              UiApplication.getUiApplication().invokeLater(new Runnable() {
					
					public void run() {
						// TODO Auto-generated method stub

						 _list.invalidate(i);
					}
				});
    				  
    				    
    			}

 

Super Contributor
Posts: 425
Registered: ‎01-02-2013
My Device: BOLD 9900
My Carrier: sdvsdvsd

Re: help in List field

any help on above ques.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: help in List field

In this case you receive all your data in one go,  You then process it one row at a time.  But until you have processed it all, you don't create your ListField.  There is no point you invalidating one row in your ListField if in fact you have not created it. 

 

Invalidating one item of your ListField is useful in circumstances where you update parts of it.  For example, there is an image associated with your entries.  So you could get a the list created quickly and then download the images, one at a time, and as each arrived, invalidate the associated entry so that it repaints itself with the new image.   

 

As currently implement, I agree with Simon, I don't think this row invalidation approach will improve the performance. 

 

To test, simply put output statements displaying the current time at various points in your processing.  Then calculate the period between the 'add's of each element, you will find that it is very short.  If you put these statements in other places in your code, you will get a better idea regarding where the delay is occurring. 

 

Highlighted
Super Contributor
Posts: 425
Registered: ‎01-02-2013
My Device: BOLD 9900
My Carrier: sdvsdvsd

Re: help in List field

ok i'll try..