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

custom list field

hi, i am working with list filed , i  need to customize the List field , i'll explain bit more. i have this pic.
9700.png

 

i have get the list of installed apps with codemodule manger and now i want to show that in List filed as you can see in the above picture.you can see the lock pic in black , i need to customize that pic with onclick,
i mean when i click to any Row i want to remove that Lock pic  in black from that clicked row  in the list filed 
and when i click again to that same row i want to insert that Lock pic in  that row.
I think i have to call the drawListRow method in the onclick or sonething else ? plz guide me
here is the code i have used

public final class MyScreen extends MainScreen {
	
	CustomListField myListView;
	   
	public MyScreen() {
		   
		Bitmap listThumb = Bitmap.getBitmapResource("icon.png");
		String listTitle="Headline";
		String listDesc = "Mobile news feeds";
		String listDesc2 = " ";
		final Bitmap navBar = Bitmap.getBitmapResource("lock.png");
		Vector v = new Vector();
		for(int i=0; i<30; i++){
			v.addElement(new ListRander(listThumb, listTitle, listDesc,listDesc2, navBar));
		}
		//v.addElement(new ListRander(listThumb, listTitle, listDesc,listDesc2, navBar));
	
		myListView = new CustomListField(v){
		    
			
			protected boolean trackwheelClick (int status, int time) {
		        
				Dialog.alert(" You have selected :" + getSelectedIndex());
		       
		        return super.trackwheelClick(status, time);
		    }
		}
		;
		add(myListView);   
		      
		   
	}
}

 

package mypackage;

import net.rim.device.api.system.Bitmap;

public class ListRander {
    
	private Bitmap listThumb= null;
    private Bitmap navBar = null;
    private String listTitle=null;
    private String listDesc= null;
    private String listDesc2= null;
    
    public ListRander(Bitmap listThumb,String listTitle, String listDesc, String listDesc2, Bitmap navBar) {
        this.listDesc = listDesc;
        this.listDesc2 = listDesc2;
        this.listThumb = listThumb;
        this.listTitle = listTitle;
        this.navBar = navBar;
    }
    public Bitmap getListThumb() {
        return listThumb;
    }
    public void setListThumb(Bitmap listThumb) {
        this.listThumb = listThumb;
    }
    public Bitmap getNavBar() {
        return navBar;
    }
    public void setNavBar(Bitmap navBar) {
        this.navBar = navBar;
    }
    public String getListTitle() {
        return listTitle;
    }
    public void setListTitle(String listTitle) {
        this.listTitle = listTitle;
    }
    public String getListDesc() {
        return listDesc;
    }
    public void setListDesc(String listDesc) {
        this.listDesc = listDesc;
    }
    public String getListDesc2() {
        return listDesc2;
    }
    public void setListDesc2(String listDesc2) {
        this.listDesc2 = listDesc2;
    }
}

 

package mypackage;

import java.util.Enumeration;
import java.util.Vector;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;

public class CustomListField extends ListField implements ListFieldCallback {
	 
    private Vector _listData;
    private int _MAX_ROW_HEIGHT = 60;
    public CustomListField (Vector data) {
 
        _listData = data;
        setSize(_listData.size());
        setSearchable(true);
        setCallback(this);
        setRowHeight(_MAX_ROW_HEIGHT);
 
    }
 
    protected void drawFocus (Graphics graphics, boolean on) {
 
        XYRect rect = new XYRect();
        graphics.setGlobalAlpha(150);
        graphics.setColor(Color.BLUE);
        getFocusRect(rect);
        drawHighlightRegion(graphics,HIGHLIGHT_FOCUS,true,rect.x,rect.y,rect.width,rect.height);
 
    }
 
    public int moveFocus (int amount, int status, int time) {
 
        this.invalidate(this.getSelectedIndex());
        return super.moveFocus(amount, status, time);
 
    }
 
    public void onFocus (int direction) {
 
        super.onFocus(direction);
 
    }
 
    protected void onUnFocus () {
 
        this.invalidate(this.getSelectedIndex());
 
    }
 
    public void refresh () {
 
        this.getManager().invalidate();
 
    }
 
    public void drawListRow (ListField listField, Graphics graphics, int index, int y, int w) {
 
        ListRander listRander = (ListRander)_listData.elementAt(index);
        graphics.setGlobalAlpha(255);
        graphics.setFont(Font.getDefault().getFontFamily().getFont(Font.PLAIN, 24));
        final int margin =5;
        final Bitmap thumb= listRander.getListThumb();
        final String listHeading = listRander.getListTitle();
        final String listDesc= listRander.getListDesc();
        final String listDesc2= listRander.getListDesc2();
        final Bitmap nevBar = listRander.getNavBar();
        //list border
        graphics.setColor(Color.BLACK);
        graphics.drawRect(0, y, w, _MAX_ROW_HEIGHT);
       
        //thumbnail border & thumbnail image
        graphics.setColor(Color.BLACK);
        graphics.drawRoundRect(margin-2, y+margin-2,thumb.getWidth()+2, thumb.getHeight()+2, 5, 5);
        graphics.drawBitmap(margin, y+margin, thumb.getWidth(), thumb.getHeight(), thumb, 0, 0);
        
        //drawing texts
       // graphics.setFont(FontGroup.fontBold);
        graphics.drawText(listHeading, 2*margin+thumb.getWidth(), y+margin);
        graphics.setColor(Color.BLACK);
       
        // graphics.setFont(FontGroup.smallFont);
        graphics.drawText(listDesc, 2*margin+thumb.getWidth(), y+ margin+20);
        graphics.drawText(listDesc2, 2*margin+thumb.getWidth(), y+ margin+32);
       
        //draw navigation button
        final int navBarPosY = y+(_MAX_ROW_HEIGHT/2 - nevBar.getHeight()/2);
        final int navBarPosX = Graphics.getScreenWidth()- nevBar.getWidth()+ margin;
        graphics.drawBitmap(navBarPosX-10, navBarPosY, nevBar.getWidth(), nevBar.getHeight(), nevBar, 0 , 0);
 
    }
 
    public Object get(ListField listField, int index) {
 
        String rowString = (String) _listData.elementAt(index);
        return rowString;
 
    }
 
    public int indexOfList (ListField listField, String prefix, int start) {
 
        for (Enumeration e = _listData.elements(); e.hasMoreElements(); ) {
 
            String rowString = (String) e.nextElement();
            if (rowString.startsWith(prefix)) {
 
                return _listData.indexOf(rowString);
 
            }
 
        }
 
        return 0;
 
    }
 
    public int getPreferredWidth(ListField listField) {
 
        return 3 * listField.getRowHeight();
 
    }
 
}

 



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

Re: custom list field

the list displays the item from your elements, in your case a ListRander object.
Give that object a boolean attribute, for example "locked", and check isLocked to determine what symbol you draw.
When you click on an item change its lock attribute to the opposite value and refresh the list item (invalidate with index of the item on the listfield)
----------------------------------------------------------
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: custom list field

[ Edited ]

when i click on the particular row in list, lock image on the right side should disappear. when i again click on the same row in list it should appear again.
something like in this link 
http://stackoverflow.com/questions/8222635/how-to-display-and-hide-an-image-in-list-on-clicking-on-i...

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

Re: custom list field

call the drawBitmap only if listRander.isLocked()
----------------------------------------------------------
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: 40
Registered: ‎03-29-2009
My Device: Storm 9550
My Carrier: AT&T

Re: custom list field

I answered your question where you posted it on Stack Overflow:

 

http://stackoverflow.com/a/19242909/119114

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

Re: custom list field

[ Edited ]

hi , i have change my code as you suggested above but it is not working. when i click the row to disappear the lock icon nothing happened. my requirement is when i click on particular row in the list filed, lock image in the black should disappear. when i again click on same row in the list it should appear again.

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

Re: custom list field

post the new code, then...
----------------------------------------------------------
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: custom list field

here is my code 

public final class MyScreen extends MainScreen {
	
	CustomListField myListView;
	   
	public MyScreen() {
		   
		Bitmap listThumb = Bitmap.getBitmapResource("icon.png");
		String listTitle="Headline";
		String listDesc = "Mobile news feeds";
		String listDesc2 = " ";
		final Bitmap navBar = Bitmap.getBitmapResource("lock.png");
		Vector v = new Vector();
		for(int i=0; i<30; i++){
			v.addElement(new ListRander(listThumb, listTitle, listDesc,listDesc2, navBar));
		}
		//v.addElement(new ListRander(listThumb, listTitle, listDesc,listDesc2, navBar));
	
		myListView = new CustomListField(v){
			protected boolean navigationClick(int status, int time) {
		        int index = getSelectedIndex();
		        ListRander lr = (ListRander) get(this, index);
		        // force a repaint of this row
		        invalidate(index);
		        return true;   // event consumed
		     }
			
			/*protected boolean trackwheelClick (int status, int time) {
		        
				Dialog.alert(" You have selected :" + getSelectedIndex());
		       
		        return super.trackwheelClick(status, time);
		    }*/
		}
		;
		add(myListView);   
		      
		   
	}
}

 ListRander.java

public class ListRander {
    
	private Bitmap listThumb= null;
    private Bitmap navBar = null;
    private String listTitle=null;
    private String listDesc= null;
    private String listDesc2= null;
    private boolean locked = true;  // defaults to "locked"
    
    public ListRander(Bitmap listThumb,String listTitle, String listDesc, String listDesc2, Bitmap navBar) {
        this.listDesc = listDesc;
        this.listDesc2 = listDesc2;
        this.listThumb = listThumb;
        this.listTitle = listTitle;
        this.navBar = navBar;
    }
    public boolean isLocked() {
        return locked;
     }
     public void setLocked(boolean value) {
        locked = value;
     }
    public Bitmap getListThumb() {
        return listThumb;
    }
    public void setListThumb(Bitmap listThumb) {
        this.listThumb = listThumb;
    }
    public Bitmap getNavBar() {
        return navBar;
    }
    public void setNavBar(Bitmap navBar) {
        this.navBar = navBar;
    }
    public String getListTitle() {
        return listTitle;
    }
    public void setListTitle(String listTitle) {
        this.listTitle = listTitle;
    }
    public String getListDesc() {
        return listDesc;
    }
    public void setListDesc(String listDesc) {
        this.listDesc = listDesc;
    }
    public String getListDesc2() {
        return listDesc2;
    }
    public void setListDesc2(String listDesc2) {
        this.listDesc2 = listDesc2;
    }
}

 CustomListField.java

public class CustomListField extends ListField implements ListFieldCallback {
	 
    private Vector _listData;
    private int _MAX_ROW_HEIGHT = 60;
    public CustomListField (Vector data) {
 
        _listData = data;
        setSize(_listData.size());
        setSearchable(true);
        setCallback(this);
        setRowHeight(_MAX_ROW_HEIGHT);
 
    }
 
    protected void drawFocus (Graphics graphics, boolean on) {
 
        XYRect rect = new XYRect();
        graphics.setGlobalAlpha(150);
        graphics.setColor(Color.BLUE);
        getFocusRect(rect);
        drawHighlightRegion(graphics,HIGHLIGHT_FOCUS,true,rect.x,rect.y,rect.width,rect.height);
 
    }
 
    public int moveFocus (int amount, int status, int time) {
 
        this.invalidate(this.getSelectedIndex());
        return super.moveFocus(amount, status, time);
 
    }
 
    public void onFocus (int direction) {
 
        super.onFocus(direction);
 
    }
 
    protected void onUnFocus () {
 
        this.invalidate(this.getSelectedIndex());
 
    }
 
    public void refresh () {
 
        this.getManager().invalidate();
 
    }
 
    public void drawListRow (ListField listField, Graphics graphics, int index, int y, int w) {
 
        try {
			ListRander listRander = (ListRander)_listData.elementAt(index);
			graphics.setGlobalAlpha(255);
			graphics.setFont(Font.getDefault().getFontFamily().getFont(Font.PLAIN, 24));
			final int margin =5;
			final Bitmap thumb= listRander.getListThumb();
			final String listHeading = listRander.getListTitle();
			final String listDesc= listRander.getListDesc();
			final String listDesc2= listRander.getListDesc2();
			final Bitmap nevBar = listRander.getNavBar();
			//list border
			graphics.setColor(Color.BLACK);
			graphics.drawRect(0, y, w, _MAX_ROW_HEIGHT);
      
			//thumbnail border & thumbnail image
			graphics.setColor(Color.BLACK);
			graphics.drawRoundRect(margin-2, y+margin-2,thumb.getWidth()+2, thumb.getHeight()+2, 5, 5);
			graphics.drawBitmap(margin, y+margin, thumb.getWidth(), thumb.getHeight(), thumb, 0, 0);
			
			//drawing texts
      // graphics.setFont(FontGroup.fontBold);
			graphics.drawText(listHeading, 2*margin+thumb.getWidth(), y+margin);
			graphics.setColor(Color.BLACK);
      
			// graphics.setFont(FontGroup.smallFont);
			graphics.drawText(listDesc, 2*margin+thumb.getWidth(), y+ margin+20);
			graphics.drawText(listDesc2, 2*margin+thumb.getWidth(), y+ margin+32);
      
			if (listRander.isLocked()) {
			    // draw lock button
			    final int navBarPosY = y+(_MAX_ROW_HEIGHT/2 - nevBar.getHeight()/2);
			    final int navBarPosX = Display.getWidth() - nevBar.getWidth()+ margin;         
			    graphics.drawBitmap(navBarPosX-10, navBarPosY, nevBar.getWidth(), nevBar.getHeight(), nevBar, 0 , 0);
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //draw navigation button
        /*final int navBarPosY = y+(_MAX_ROW_HEIGHT/2 - nevBar.getHeight()/2);
        final int navBarPosX = Graphics.getScreenWidth()- nevBar.getWidth()+ margin;
        graphics.drawBitmap(navBarPosX-10, navBarPosY, nevBar.getWidth(), nevBar.getHeight(), nevBar, 0 , 0);*/
 
    }
 
    public Object get(ListField listField, int index) {
 
        //String rowString = (String) _listData.elementAt(index);
        return _listData.elementAt(index);
 
    }
 
    public int indexOfList (ListField listField, String prefix, int start) {
 
        for (Enumeration e = _listData.elements(); e.hasMoreElements(); ) {
 
            String rowString = (String) e.nextElement();
            if (rowString.startsWith(prefix)) {
 
                return _listData.indexOf(rowString);
 
            }
 
        }
 
        return 0;
 
    }
 
    public int getPreferredWidth(ListField listField) {
 
        return 3 * listField.getRowHeight();
 
    }
 
}

 

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

Re: custom list field

you don't change the locked status of your ListRander element in navigationClick.
----------------------------------------------------------
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: custom list field

[ Edited ]

ok i got it