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
Contributor
Posts: 14
Registered: ‎11-22-2008
My Device: 9360
My Carrier: T-Mobile CZ

ListField

hello,
in my application i have button and list field... if i click to button so show me listfield... now i have problem with that if i click button show me listfield and if i click button again so listfield duplicated... can i remove listfield if i clicked to button?

 

my code is:

 

FieldChangeListener buttonListener = new FieldChangeListener() 
	{
		public void fieldChanged(Field field, int context) 
		{
			if (field == btnSearch) 
			{	
				try 
				{
					int co = ((SoapObject)response.getProperty(0)).getPropertyCount();
					
					for(int in = 0; in < co; in++)
					{
						tableRow = (SoapObject)table.getProperty(in);
						Title = tableRow.getProperty(2).toString().trim();
						Desc = tableRow.getProperty(3).toString().trim();
						Desc2 = tableRow.getProperty(4).toString().trim();

						info.addElement(new ListRander(Title, Desc, Desc2));
					}
					
				} 
				catch (IOException e) 
				{
					Dialog.alert(e.toString());
				} 
				catch (XmlPullParserException e) 
				{
					Dialog.alert(e.toString());
				}

				myListView = new CustomListField(info);
				
				s.add(myListView);
				add(s);
		    }
		}
	};

 

Regards,

 

Stepan

 

 

 

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

Re: ListField

Can you do that by testing if myListView is null and only creating the new ListField if it is?

Contributor
Posts: 14
Registered: ‎11-22-2008
My Device: 9360
My Carrier: T-Mobile CZ

Re: ListField

Hi Peter,
thanks... maybe it is possible.. but I need if I again click button that ListView delete old items and shows me new items...
I search data from web services... if I click button every time, show other information showing and the previous ones do not want to see.

 

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

Re: ListField

You have two choices

1) Either update the ListField you already have on display, or

2) Create a new ListField and replace the old one

 

I generally do the later.

 

Say you have reference to your oldListField and your new ListField, called oldListField and newListField respectively.  Then you can replace old with new using code like

 

Manager m = oldListField.getMananger();

m.replace(oldListField, newListField);

 

I think this answers your question, the general idea is whatever processing you have that manages the updating needs to have references to old and new ListField (or at least to old and then it creates new).

 

Now let us discuss what you are trying to do in general. You are going to, when a button is clicked, fetch some information from the Server.  You can not do this processing on the Event Thread.  Your FieldChangeListener runs on the Event Thread, so you need to start a background process to get the data.  this raises two problems typically:

a) This means the Event Thread is left processing and so the user is not blocked.  This might be OK, in other words you might want to leave the user free to do other things while you are retrieving the information.  Alternatively you might want to block the user.  In this case you are going to have to put up a screen of some kind that blocks the user.   

b) It also means that you will have the data in a Background Thread and you will want to use that data to update the User Interface.  So you need to move this part of the processing to the Event Thread. 

 

Look round the forum for people who have had similar problems and found solutions.  You might find looking for "Please Wait Screen" useful for (a).  Look for information on the Event Thread in general for this problem.  You can use httpDemo as an example of how Network processing can be started in the background. 

Contributor
Posts: 14
Registered: ‎11-22-2008
My Device: 9360
My Carrier: T-Mobile CZ

Re: ListField

ok... thank you,

 

can you send me sample code with replace two ListField...

 

Thank you

 

Stepan

Developer
Posts: 134
Registered: ‎05-19-2012
My Device: Torch 9800, PlayBook 64GB, Dev Alpha
My Carrier: VIVO Brazil

Re: ListField

[ Edited ]

Hello,

 

I can replace the contents of the ListField by creating a for() loop, or even better a while(!ListField.isEmpty) loop, to call the ListField.remove(int) function to remove all the contents of the list, then you just call that sample you gave us in the first post to repopulate the existing list.

 

Best Regards.

If I helped you please click the "Like" button to support my work.
My apps: CherryNotes - Bookmarked - Requests
Social Me: about.me - Twitter
Highlighted
Developer
Posts: 209
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: ListField

[ Edited ]

Hi 

 

Try this code.

 

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class pawan extends MainScreen{
	VerticalFieldManager vfm ;
	ListField list;
	ButtonField btn;
	public pawan() {
		// TODO Auto-generated constructor stub
		super();
		 vfm = new VerticalFieldManager();
		
		  list = new ListField(5);
		list.setRowHeight(50);
		list.setCallback(new ListFieldCallback() {
			
			public int indexOfList(ListField listField, String prefix, int start) {
				// TODO Auto-generated method stub
				return 0;
			}
			
			public int getPreferredWidth(ListField listField) {
				// TODO Auto-generated method stub
				return 0;
			}
			
			public Object get(ListField listField, int index) {
				// TODO Auto-generated method stub
				return null;
			}
			
			public void drawListRow(ListField listField, Graphics g, int index,
					int y, int width) {
				// TODO Auto-generated method stub
				g.drawText("Pawan" +" "+"Jha" , 48, y+9, 0, width);
				g.drawLine(0, y+48, Display.getWidth(), y+48);
			}
		});
		vfm.add(list);
	 
		btn = new ButtonField("hide");
			add(btn);
		 add(vfm);
		 btn.setChangeListener(new FieldChangeListener() {
			
			public void fieldChanged(Field field, int context) {
				// TODO Auto-generated method stub
				 if(vfm.getFieldCount()==1)
				 {
					 vfm.delete(list);
				 }
				 else
				 {
						vfm.add(list);
				 }
			}
		});
	 
	}
}

 

Thanks