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
arunimi123
Posts: 182
Registered: ‎09-22-2011
My Device: 9700
My Carrier: airtel

ListField onNavigationClick problem

I have developed a CustomListField which is working fine.Each row contains a text and an image.The code is below

 

import java.util.Vector;

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class CustomListField extends VerticalFieldManager {

	String imageName = null;
	public String labelText = null;
	BitmapField bmp_;
	int _wd;
	int _ht;
    int foccolor;
    int unfoccolor;
	public CustomListField(Vector fldleft, Vector fldcentre,Vector fldright, int size, int width, int height,final int foccolor,final int unfoccolor) {
		super(VERTICAL_SCROLL);
		_wd = width;
		_ht = height;
		VerticalFieldManager vfm = new VerticalFieldManager(
				VerticalFieldManager.USE_ALL_WIDTH
						| VerticalFieldManager.VERTICAL_SCROLL);
		HorizontalFieldManager hfm;
		for (int i = 0; i < size; i++) {
			vfm.add(new SeparatorField());

			hfm = new HorizontalFieldManager(Field.FOCUSABLE | USE_ALL_WIDTH) {
				protected void paint(Graphics g) {
					if (isFocus()) {
						g.setColor(foccolor);
						g.fillRect(0, 0, getWidth(), getHeight());
					} else {
						g.setColor(unfoccolor);
						g.fillRect(0, 0, getWidth(), getHeight());
					}
					super.paint(g);
					invalidate();
				}

				protected void onFocus(int direction) {
					super.onFocus(direction);
					invalidate();
				}

				protected void onUnfocus() {
					invalidate();
					super.onUnfocus();
				}
			};

			hfm.add((Field) fldleft.elementAt(i));
			hfm.add((Field) fldcentre.elementAt(i));
			hfm.add((Field) fldright.elementAt(i));
			vfm.add(hfm);
		}
		if (Display.getHeight() != 240)
			vfm.add(new SeparatorField());
		add(vfm);
	}

	public CustomListField(Vector fldleft, Vector fldright, int size,
			int width, int height,final int foccolor,final int unfoccolor) {
		super(NO_VERTICAL_SCROLL);
		
		_wd = width;
		_ht = height;
		VerticalFieldManager vfm = new VerticalFieldManager(
				VerticalFieldManager.USE_ALL_WIDTH
						| VerticalFieldManager.VERTICAL_SCROLL);
		HorizontalFieldManager hfm;
		for (int i = 0; i < size; i++) {
			vfm.add(new SeparatorField());

			hfm = new HorizontalFieldManager(Field.FOCUSABLE | USE_ALL_WIDTH) {
				protected void paint(Graphics g) {
					if (isFocus()) {
						g.setColor(foccolor);
						g.fillRect(0, 0, getWidth(), getHeight());
					} else {
						g.setColor(unfoccolor);
						g.fillRect(0, 0, getWidth(), getHeight());
					}
					super.paint(g);
					invalidate();
				}

				protected void onFocus(int direction) {
					super.onFocus(direction);
					invalidate();
				}

				protected void onUnfocus() {
					invalidate();
					super.onUnfocus();
				}
			};

			hfm.add((Field) fldleft.elementAt(i));

			hfm.add((Field) fldright.elementAt(i));

			vfm.add(hfm);
		}
		if (Display.getHeight() != 240)
			vfm.add(new SeparatorField());
		add(vfm);
	}

	public CustomListField(Vector fldleft, int size, int width, int height,final int foccolor,final int unfoccolor) {
		super(NO_VERTICAL_SCROLL);
		_wd = width;
		_ht = height;
		VerticalFieldManager vfm = new VerticalFieldManager(
				VerticalFieldManager.USE_ALL_WIDTH
						| VerticalFieldManager.VERTICAL_SCROLL);
		HorizontalFieldManager hfm;
		for (int i = 0; i < size; i++) {
			vfm.add(new SeparatorField());

			hfm = new HorizontalFieldManager(Field.FOCUSABLE | USE_ALL_WIDTH) {
				protected void paint(Graphics g) {
					if (isFocus()) {
						g.setColor(foccolor);
						g.fillRect(0, 0, getWidth(), getHeight());
					} else {
						g.setColor(unfoccolor);
						g.fillRect(0, 0, getWidth(), getHeight());
					}
					super.paint(g);
					invalidate();
				}

				protected void onFocus(int direction) {
					super.onFocus(direction);
					invalidate();
				}

				protected void onUnfocus() {
					invalidate();
					super.onUnfocus();
				}
			};

			hfm.add((Field) fldleft.elementAt(i));

			vfm.add(hfm);
		}

		
		vfm.add(new SeparatorField());
		add(vfm);
	}
	protected boolean navigationClick(int status, int time) {
		fieldChangeNotify(1);
		return true;
	}
	protected void sublayout(int maxwidth, int maxheight) {
		super.sublayout(_wd, _ht);
		setExtent(_wd, _ht);
	}

}

 Now i want that when i click each row certain action shoulded be fired depending on the index of the row.So how can i get the row index

 

CustomListField cl=new CustomListField(text,image,count,Display.getWidth(),Display.getHeight()/2+45,Color.YELLOW,Color.WHITE){
				protected boolean navigationClick(int status, int time) {
                    if(getFieldWithFocusIndex){
                   //This is always returning me zero irrespective of what row i click
}
				return true;
				}
			};

 please help me anyone

Please use plain text.
Trusted Contributor
arunimi123
Posts: 182
Registered: ‎09-22-2011
My Device: 9700
My Carrier: airtel

Re: ListField onNavigationClick problem

This is my HomeScreen 

 

import java.util.Vector;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.VerticalFieldManager;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.imimobile.ui.CustomBitmapField;
import com.imimobile.ui.CustomLabelField;
import com.imimobile.ui.CustomListField;
import com.imimobile.ui.SectionHeaderManager;
import com.imimobile.util.Constants;

public class HomeScreen extends VerticalFieldManager {
	public String labelText = null;
	CustomLabelField label = null;
	String name;
	String shortCode;
	String parentID ;
	String catID = "" ;
    CustomListField cl = null;
    
    
	public HomeScreen(Document document,Manager viewManager) {
		SectionHeaderManager sectionField = new SectionHeaderManager("Services",18, Color.BLACK, 0xcc6666, "left");
		viewManager.add(sectionField);
		NodeList nodeList = document.getElementsByTagName("Service");
		Vector text=new Vector();;
		Vector image=new Vector();
		int count=0;
		
		for (int s = 0; s < nodeList.getLength(); s++) {
			Element element = (Element) nodeList.item(s);
			 name = element.getAttribute("name");
			 shortCode = element.getAttribute("shortCode");
			 parentID = element.getAttribute("catParentID");
			 catID = element.getAttribute("catID");
			if(shortCode.equalsIgnoreCase("n") && parentID.equalsIgnoreCase("0")){
				 label = new CustomLabelField(name,LabelField.FOCUSABLE,Color.BLACK,Display.getWidth()/2-20,16);
					label.setPadding(4,4,4,4);
					label.setFont(Constants.mediumFont);
					text.addElement(label);
					Bitmap bmp=Bitmap.getBitmapResource("greaterthan.png");
					CustomBitmapField socialImg = new CustomBitmapField(bmp);
					socialImg.setPadding(4,4,4,Display.getWidth()-170);
					image.addElement(socialImg);
					count=count+1;
			}
			
		}
	
			 cl=new CustomListField(text,image,count,Display.getWidth(),Display.getHeight()/2+45,Color.YELLOW,Color.WHITE){
				protected boolean navigationClick(int status, int time) {
				
					Dialog.alert("The text is---"+label.getText());
					return true;
				}
			};
			viewManager.add(cl);
			SectionHeaderManager footer = new SectionHeaderManager("Inbox",22, Color.BLACK, 0xcc6666, "left");
			viewManager.add(footer);
		}
 
	}

 When i click on each row 

Dialog.alert("The text is---"+label.getText());

it returns me the value of the last row only

Please use plain text.
Developer
tommy20
Posts: 249
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10

Re: ListField onNavigationClick problem

What prevents you from re-using/extending a ListField which implements a ListFieldCallback?

--tom
Please use plain text.
Trusted Contributor
arunimi123
Posts: 182
Registered: ‎09-22-2011
My Device: 9700
My Carrier: airtel

Re: ListField onNavigationClick problem

Actually i want each item inside the row to be selectable,which is a bit cubersome to achive using ListField class,so i have followed this manner

Please use plain text.
Trusted Contributor
arunimi123
Posts: 182
Registered: ‎09-22-2011
My Device: 9700
My Carrier: airtel

Re: ListField onNavigationClick problem

so how to achive it by this approach

Please use plain text.
Developer
tommy20
Posts: 249
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10

Re: ListField onNavigationClick problem

"each item inside the row to be selectable"

-Do you mean that the image and the text of each row are selectable individually?
--tom
Please use plain text.
Developer
tommy20
Posts: 249
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10

Re: ListField onNavigationClick problem

What SDK versions are you aiming for?
--tom
Please use plain text.