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
Posts: 18
Registered: ‎05-22-2013
My Device: 8900
My Carrier: vodafone

how to add the radio button in the list view

Hi 

 

i want to add the rado button to the list view .i able to add the check box with the list view 

 

ChecklistData currentRow = (ChecklistData)this.get(list, index);
StringBuffer rowString = new StringBuffer();

if (currentRow.isChecked())
{
rowString.append(Characters.BALLOT_BOX_WITH_CHECK);
}
else
{
rowString.append(Characters.BALLOT_BOX);
}

 

but coucl not able to add the radio button to the list view.

 

Please help me in implementing the list view with radio button and want to select the value of selected radi button

 

i am attaching the screenshot which i able to do and also sending the code for list view listview.jpg

import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import net.rim.blackberry.api.browser.Browser;
import net.rim.device.api.database.Row;
import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
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;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.picker.DateTimePicker;

public class ListPopUp extends PopupScreen implements KeyListener,ListFieldCallback
{
	FocusButtonField openDoc = null,bookmarkFocus = null,emailToMe = null;
	
	FocusButtonField decision_yes= null,decision_no = null;
	CustomLabel DecisionMaker,BlankLabel;
	HorizontalFieldManager hfm = null;
	static String pi_ID = null, link = null;
	static String PI_PPI_REM;
	static String PI_PPI_REM_Bookmark;
	private ListField _checkList;
	private ListField _checkList11;
	private MenuItem _toggleItem;
	
	private Vector _listData = new Vector();
	String[] _CatID;
	String[] _CatName;
	String[] PI_ID_value;
	VerticalFieldManager topVFM;
	

	public ListPopUp(final String[] _CatID,final String[] _CatName,final String[] PI_ID_value)
	{
		super(new VerticalFieldManager(),Field.FOCUSABLE);
		this._CatID = _CatID;
		this._CatName = _CatName;
		this.PI_ID_value = PI_ID_value;
		
		openDoc = new FocusButtonField("", 0,StringTags.redbtnSqBG,StringTags.redbtnSqBG,SizeSpec.setY23,SizeSpec.setX200,SizeSpec.setY30,Color.BLACK,Color.WHITE,false);
		
		setBackground(BackgroundFactory.createSolidTransparentBackground(Color.BLACK,192));
		
		listFieldView(_CatName);
		
		_checkList = new ListField()
		{
			//Allow the space bar to toggle the status of the selected row.
			protected boolean keyChar(char key, int status, int time)
			{
				boolean retVal = false;
				if (key == Characters.SPACE)
				{
					int index = getSelectedIndex();
					ChecklistData data = (ChecklistData)_listData.elementAt(index);
					data.toggleChecked();
					_listData.setElementAt(data, index);
					invalidate(index);
					retVal = true;
				}
				return retVal;
			}
		};
		_checkList.setEmptyString("No Data Available.",DrawStyle.HCENTER);
		_checkList.setRowHeight(SizeSpec.setY50);
		_checkList.setCallback(this);

		int elementLength = _CatName.length;

		for(int count = 0; count < elementLength; ++count)
		{
			if(_CatName[count]!=null)
			{
				_listData.addElement(new ChecklistData(_CatName[count], false));
				_checkList.insert(count);
			}

		}

		topVFM = new VerticalFieldManager(Manager.VERTICAL_SCROLL)
		{
			protected void sublayout(int width, int height)
			{
				super.sublayout(uiApp.dWidth,SizeSpec.setY320);
				super.setExtent(uiApp.dWidth,SizeSpec.setY320);
			}

		};
		topVFM.add(_checkList);
		add(topVFM);

		
	}
	
	public void listFieldView(String[] _CatName)
	{
		_checkList = new ListField()
		{
			protected void onFocus(int direction) 
			{
				_checkList.invalidate();
			}
			protected void onUnfocus() 
			{
				_checkList.invalidate();
			}
			public int moveFocus(int amount, int status, int time) 
			{
				_checkList.invalidate(_checkList.getSelectedIndex());
				if(amount >= 3)
				{
					amount = 1 ;
				}
				else if(amount <= -3)
				{
					amount = -1;
				}
				_checkList.invalidate();
				return super.moveFocus(amount,status,time);
			}
		};

		//Set the ListFieldCallback
		_checkList.setCallback(this);

		int elementLength = _CatName.length;

		//Populate the ListField & Vector with data.
		for(int count = 0; count < elementLength; ++count)
		{
			if(_CatName[count]!=null)
			{
				_listData.addElement(new ChecklistData(_CatName[count], false));
				_checkList.insert(count);
			}
		}
	}
	protected boolean navigationClick(int status, int time)
	{
		Field field = UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus();
		
		
		if(field == _checkList) 
		{
			listClickevent();
		}
		
		if(field == bookmarkFocus)
		{
			
		}
		
		if(field == openDoc)
		{
			
		}
		if(field == decision_no)
		{
			
		}
		if(field == decision_yes)
		{

		}
		if(field == emailToMe)
		{
			
		}
		return true;
	}

	/////////////////////////////////////
	/// implementation of Keylistener
	/////////////////////////////////////
	public boolean keyChar(char key, int status, int time) 
	{
		boolean retval = false;
		if(key == Characters.ESCAPE)
		{
			retval = true;
			UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
		}
		else if(key == Characters.ENTER)
		{
			navigationClick(status, time);
			return true;
		}
		else 
		{
			retval = super.keyChar(key,status,time);
		}
		return retval;
	}
	/** Implementation of KeyListener.keyDown */
	public boolean keyDown(int keycode, int time) {
		return false;
	}
	/** Implementation of KeyListener.keyRepeat */
	public boolean keyRepeat(int keycode, int time) {
		return false;
	}
	/** Implementation of KeyListener.keyStatus */
	public boolean keyStatus(int keycode, int time) {
		return false;
	}
	/** Implementation of KeyListener.keyUp */
	public boolean keyUp(int keycode, int time) {
		return false;
	}

	public void drawListRow(ListField list, Graphics graphics, int index, int y, int w) 
	{

		if(uiApp.dWidth==360 && uiApp.dHeight==480)
		{
			graphics.setFont(Font.getDefault().derive(Font.PLAIN,SizeSpec.setY13));
		}
		else
		{
			graphics.setFont(Font.getDefault().derive(Font.PLAIN,SizeSpec.setY20));
		}

		if(index%2==0)
		{
			//				graphics.setColor(0xe6e6e7);
			graphics.setColor(Color.BLACK);
			graphics.fillRect(0,0,SizeSpec.setX480,y+SizeSpec.setY50);
		}

		if(graphics.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
		{
			if(index%2==0)
			{
				graphics.setColor(Color.BLUE);
				graphics.fillRect(0,0,SizeSpec.setX480,y+SizeSpec.setY50);
			}
		}

		graphics.setColor(Color.WHITE);
		//Get the ChecklistData for the current row.
		ChecklistData currentRow = (ChecklistData)this.get(list, index);
		StringBuffer rowString = new StringBuffer();

		if (currentRow.isChecked()) 
		{
			rowString.append(Characters.BALLOT_BOX_WITH_CHECK);			
		}
		else 
		{
			rowString.append(Characters.BALLOT_BOX);
		}
//      rowString.append(Characters.BALLOT_BOX);
        rowString.append(Characters.SPACE);
        rowString.append(Characters.SPACE);
        
		rowString.append(currentRow.getStringVal());
		//Draw the text.
		Vector lines = wrap(rowString.toString(),uiApp.dWidth);
		if(lines.size() == 1)
			graphics.drawText((String)lines.elementAt(0), 0, y+SizeSpec.setY15, 0, w);
		else if(lines.size() >= 2)
		{
			graphics.drawText((String)lines.elementAt(0), 0, y+2, 0, w);
			graphics.drawText((String)lines.elementAt(1), 0, y+SizeSpec.setY25, 0, w);
		}
		else
			graphics.drawText((String)lines.elementAt(0), 0, y+SizeSpec.setY15, 0, w);

		Bitmap head_Bitmap;
		head_Bitmap = Utility.getScaledBitmapImage(StringTags.arrow,SizeSpec.setX20,SizeSpec.setY20);
		graphics.drawBitmap(SizeSpec.setX460, y+SizeSpec.setY15, SizeSpec.setX20,SizeSpec.setY20, head_Bitmap,0, 0);

		graphics.drawLine(0,y+SizeSpec.setY48,w,y+SizeSpec.setY48);

	}	

	public Object get(ListField listField, int index) {
		// TODO Auto-generated method stub
		return _listData.elementAt(index);
	}

	public int getPreferredWidth(ListField list) 
	{
		return Graphics.getScreenWidth();
	}

	public int indexOfList(ListField list, String p, int s) {
		// TODO Auto-generated method stub
		return _listData.indexOf(p, s);
	}
	
	
	
	
	private Vector wrap (String text, int width) 
	{
		Vector result = new Vector ();
		if (text ==null)
			return result;

		boolean hasMore = true;

		// The current index of the cursor
		int current = 0;

		// The next line break index
		int lineBreak = -1;

		// The space after line break
		int nextSpace = -1;

		while (hasMore) 
		{
			//Find the line break
			while (true) 
			{
				lineBreak = nextSpace;
				if (lineBreak == text.length() - 1) 
				{
					// We have reached the last line
					hasMore = false;
					break;
				} 
				else 
				{
					nextSpace = text.indexOf(' ', lineBreak+1);
					if (nextSpace == -1)
						nextSpace = text.length() -1;
					int linewidth = this.getFont().getAdvance(text,current, nextSpace-current);
					// If too long, break out of the find loop
					if (linewidth > width) 
						break;
				}
			}
			String line = text.substring(current, lineBreak + 1);
			result.addElement(line);
			current = lineBreak + 1;
		}
		return result;
	}
	
	
	public void listClickevent()
	{		
		
		int index = _checkList.getSelectedIndex();		
		ChecklistData data = (ChecklistData)_listData .elementAt(index);
        data.toggleChecked();
        _listData.setElementAt(data, index);
        _checkList.invalidate(index);
        return;
//		Utility.pi_ID = PI_ID_value[index];
//		Utility.link = _CatID[index];
//		String url1 = uiApp.baseUserManagerURL+"/wsCheckDocBookmarkStatus?documentID=0&userID="+uiApp.userId+"&prescInfoID="+PI_ID_value[index]+"&pType=pi";
//		ConnectWeb obj = new ConnectWeb();
//		obj.WebServiceConnect("checkBookMarkPIStatus",url1);

	}
	
	
}

 

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

Re: how to add the radio button in the list view

Sorry, you can't put a working radio button in a ListField.

 

If you have a relatively small number of options (say less than 20), then I recommend that you actually just use a RadioButtonGroup and a number of RadioButtonFields.

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

Re: how to add the radio button in the list view

he does not put the field in, he puts the character (ballot box unchecked/checked) in, which is a nice solution.

i don't see an obvious issue with the code and would suggest to inspect the actual input to the drawText method using the debugger.
----------------------------------------------------------
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
Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to add the radio button in the list view

Understood Simon, but this is probably overkill if the OP really wants a radio button that works like a radio button. 

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

Re: how to add the radio button in the list view

on re-reading i suspect you are right, he has a checkbox and now wants a radio button.
no way to integrate the radio button in there. you could use pictures of one and toggle them, similar to the checkbox character.
----------------------------------------------------------
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
Developer
Posts: 18
Registered: ‎05-22-2013
My Device: 8900
My Carrier: vodafone

Re: how to add the radio button in the list view

Hi 

 

I use the images of radio_on or radion_off in the list view.but the problem is still there 

 

I want to select only one row and that should be seleected and get the value of that particular row,please help me in suggesting the logic for implementing this thing 

 

I am attaching the image for refernceradioimage.jpg

Regards

Amrinder Singh

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

Re: how to add the radio button in the list view

if you select one row you should iterate through your data model and unselect all others.
----------------------------------------------------------
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
Developer
Posts: 18
Registered: ‎05-22-2013
My Device: 8900
My Carrier: vodafone

Re: how to add the radio button in the list view

hi 

 

I have posted the code above ,could you please give me little bit hint , how i should itertae the through data model and unselect all.i was trying to invalidate ,but could not able to do so .please help me in this 

 

Regards

Amrinder Singh

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

Re: how to add the radio button in the list view

[ Edited ]

I am suggesting that if you want a RadioButton, then discard all the code above, and use a RadioButton.  

 

If this approach does not work for you, please can you describe, in words, what changes you would like to the current implementation.  Describe what you a user currently sees, and what is wrong with that and what you would like a user to see.  

Developer
Posts: 18
Registered: ‎05-22-2013
My Device: 8900
My Carrier: vodafone

Re: how to add the radio button in the list view

Hi

 

in the code ,there is no wrong .

 

I want to show the images of radio button ,that i able to show .

 

when user select the row ,then particular row's radio button images to be selected as black .

 

,if user select another particular row ,then the previous radi button images to be get white and new clicked particlaur becomes black in the circle.

 

i mean to say ,only one row to be selected in the list view with the help of toggling the image of radio button and retrieve the value of particular row 

 

for now,i able to select the multiple row ,i want to get the value of only one row .

 

 

 

Regards

Amrinder SIngh