i have written a class which implements ListFieldCallBack like,
import java.util.Vector;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
class ListCallBack implements ListFieldCallback
{
private Vector listelements = new Vector();
public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width)
{
String text = (String)listelements.elementAt(index);
graphics.drawText(text,0,y,0,width);
}
public Object get(ListField listField, int index)
{
return listelements.elementAt(index);
}
public int indexOfList(ListField listField, String prefix, int start)
{
return listelements.indexOf(prefix, start);
}
public int getPreferredWidth(ListField listField)
{
return Graphics.getScreenWidth();
}
public void insert(String toInsert, int index)
{
listelements.addElement(toInsert);
}
public void erase()
{
listelements.removeAllElements();
}
}
And in my constructor having the main class is coded as
helloWorld()
{
mylist = new ListField();
ListCallBack myCallBack = new ListCallBack();
mylist.setCallback(myCallBack);
for(int i = 0; i<array.length;i++)//array is a string array
{
list_category.insert(i);
myCallBack.insert(array[i], i);
}
this.add(list_category);
}
this works properly..
like, i am getting output like,
Aby
Eric
Allay
vine
But i want to add another string to the next of that array in the each row displayed in list.. How could i do this?
Like, for example, i want my screen output like,
Aby : Smart
Eric : 0000
Allay : 9789
vine : Like
how could i do this?
You should change the ListFieldCallback.drawListRow(ListField listField, Graphics graphics, int index, int y, int width) to draw that.
Use net.rim.device.api.ui.Graphics API to draw whatever you want.
Related
Please anyone help me get selected listitems from a listfieldcheckbox, and add them into an arraylist. If possible, give any useful links also. Here's my code so far (I am new to blackberry application development). Please help.
package mypackage;
import java.util.Vector;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
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.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.util.IntVector;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen implements ListFieldCallback
{
private Vector _listData = new Vector();
private Vector _checkedData = new Vector();
private ListField listField;
private static final String[] _elements = {"First element", "Second element","Third element"
};
//private static final String[] _elements1 = {"hai","welcome","where r u"
//};
private MenuItem _getDataMenu,selectall,Delete;
Vector result = new Vector();
protected void makeMenu(Menu menu, int instance)
{
menu.add(_getDataMenu);
menu.add(selectall);
menu.add(Delete);
//Create the default menu.
super.makeMenu(menu, instance);
}
private class ChecklistData
{
private String _stringVal;
private boolean _checked;
ChecklistData(String stringVal, boolean checked)
{
_stringVal = stringVal;
_checked = checked;
}
//Get/set methods.
private String getStringVal()
{
return _stringVal;
}
private boolean isChecked()
{
return _checked;
}
//Toggle the checked status.
private void toggleChecked()
{
_checked = !_checked;
}
}
public Vector getCheckedItems() {
return _checkedData;
}
/**
* Creates a new MyScreen object
*/
public MyScreen()
{
// Set the displayed title of the screen
setTitle("MyTitle");
VerticalFieldManager main = new VerticalFieldManager(VerticalFieldManager.USE_ALL_HEIGHT|
VerticalFieldManager.USE_ALL_WIDTH|VerticalFieldManager.VERTICAL_SCROLL);
this.add(main);
HorizontalFieldManager hfm = new HorizontalFieldManager();
main.add(hfm);
listField = 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 the spacebar was pressed...
if (key == Characters.SPACE)
{
//Get the index of the selected row.
int index = getSelectedIndex();
//Get the ChecklistData for this row.
ChecklistData data = (ChecklistData)_listData.elementAt(index);
//Toggle its status.
data.toggleChecked();
//Update the Vector with the new ChecklistData.
_listData.setElementAt(data, index);
//Invalidate the modified row of the ListField.
invalidate(index);
//Consume this keyChar (key pressed).
retVal = true;
}
return retVal;
}
};
listField.setCallback(this);
reloadList();
int elementLength = _elements.length;
for(int count = 0; count < elementLength; ++count)
{
_listData.addElement(new ChecklistData(_elements[count], false));
//_listData.addElement(new ChecklistData(_elements1[count], false));
listField.insert(count);
}
main.add(listField);
_getDataMenu =new MenuItem("Get Data", 200, 10) {
public void run(){
int index = listField.getSelectedIndex();
ChecklistData data = (ChecklistData)_listData.elementAt(index);
String message = "Selected data: " + data.getStringVal() + ", and status: " + data.isChecked();
//Dialog.alert(message);
// get all the checked data indices
IntVector selectedIndex = new IntVector(0, 1);
//ChecklistData data;
for (int i=0;i<_listData.size();i++) {
data = (ChecklistData)_listData.elementAt(i);
if(data.isChecked()) {
selectedIndex.addElement(i);
String selectedvalues = data.getStringVal();
System.out.println("Selected items are:"+selectedvalues);
}
}
data = null;
// now selectedIndex will contain all the checked data indices.
//String message = "Selected data: " + data.getStringVal() + ", and status: " + data.isChecked();
}
};
selectall = new MenuItem("Selectall", 200, 10){
public void run(){
int elementLength = _elements.length;
for(int count = 0; count < elementLength; ++count)
{
_listData.setElementAt(new ChecklistData(_elements[count], true), count);
}
}
};
Delete = new MenuItem("Delete", 200, 10){
public void run(){
int index = listField.getSelectedIndex();
_listData.removeElementAt(index);
// update the view
listField.delete(index);
listField.invalidate(index);
}
};
}
private void reloadList() {
// TODO Auto-generated method stub
_listData.setSize(_listData.size());
}
public void drawListRow(ListField list, Graphics graphics, int index, int y, int w)
{
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);
}
//Append a couple spaces and the row's text.
rowString.append(Characters.SPACE);
rowString.append(Characters.SPACE);
rowString.append(currentRow.getStringVal());
//Draw the text.
graphics.drawText(rowString.toString(), 0, y, 0, w);
/*if (currentRow.isChecked()) {
if( -1 ==_checkedData.indexOf(currentRow))
_checkedData.addElement(currentRow);
rowString.append(Characters.BALLOT_BOX_WITH_CHECK);
}
else {
if( -1 !=_checkedData.indexOf(currentRow))
_checkedData.removeElement(currentRow);
rowString.append(Characters.BALLOT_BOX);
} */
}
//Returns the object at the specified index.
public Object get(ListField list, int index)
{
return _listData.elementAt(index);
}
public int indexOfList(ListField list, String p, int s)
{
//return listElements.getSelectedIndex();
return _listData.indexOf(p, s);
}
//Returns the screen width so the list uses the entire screen width.
public int getPreferredWidth(ListField list)
{
return Display.getWidth();
}
protected boolean navigationClick(int status, int time) {
int index1 = listField.getSelectedIndex();
/*System.out.println("Selected item index:"+index1);
//int[] list =listField.getSelection();
//String s = Integer.toString(list);
System.out.println(" items are:"+_elements[index1]);
//ChecklistData data = (ChecklistData)_listData.elementAt(index1);*/
//Get the ChecklistData for this row.
ChecklistData data = (ChecklistData)_listData.elementAt(index1);
String message = "Selected data: " + data.getStringVal() + ", and status: " + data.isChecked();
System.out.println("message is:"+message);
//Toggle its status.
data.toggleChecked();
//Update the Vector with the new ChecklistData.
_listData.setElementAt(data, index1);
//Invalidate the modified row of the ListField.
listField.invalidate(index1);
return true;
}
}
How do I display a list of items where list items can be selected for further action, in a BlackBerry application?
You want to use a ListField. Here is a sample of code that makes use of the ListField.
class CustomListField extends ListField implements ListFieldCallback
{
public static int x;
public Vector rows;
private Bitmap p1;
int z = this.getRowHeight();
public LabelField label,label2,label3,label4,label5;
public CustomListFieldCode(int rowcount,int service_No,String text1,String time)
{
super(0, ListField.MULTI_SELECT);
setRowHeight(3*z);
setEmptyString("Hooray, no tasks here!", DrawStyle.HCENTER);
setCallback(this);
rows = new Vector();
for (x = 0; x < rowcount; x++)
{
TableRowManager row = new TableRowManager();
if(x%2==0)
row.setBackground(BackgroundFactory.createSolidBackground(Color.AQUA));
label = new LabelField("Service"+x);
row.add(label);
rows.addElement(row);
}
setSize(rows.size());
}
// ListFieldCallback Implementation
public void drawListRow(ListField listField, Graphics g, int index, int y,int width)
{
CustomListFieldCode list = (CustomListFieldCode) listField;
TableRowManager rowManager = (TableRowManager) list.rows.elementAt(index);
rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}
private class TableRowManager extends Manager
{
public TableRowManager()
{
super(0);
}
// Causes the fields within this row manager to be layed out then
// painted.
public void drawRow(Graphics g, int x, int y, int width, int height)
{
// Arrange the cell fields within this row manager.
layout(width, height);
// Place this row manager within its enclosing list.
setPosition(x, y);
// Apply a translating/clipping transformation to the graphics
// context so that this row paints in the right area.
g.pushRegion(getExtent());
// Paint this manager's controlled fields.
subpaint(g);
g.setColor(0x00CACACA);
//g.drawLine(0, 0, getPreferredWidth(), 0);
// Restore the graphics context.
g.popContext();
}
// Arrages this manager's controlled fields from left to right within
// the enclosing table's columns.
protected void sublayout(int width, int height)
{
// write your code for arranging the elements of the row
}
// The preferred width of a row is defined by the list renderer.
public int getPreferredWidth()
{
return Graphics.getScreenWidth();
}
// The preferred height of a row is the "row height" as defined in the
// enclosing list.
public int getPreferredHeight()
{
return getRowHeight();
}
}
public Object get(ListField listField, int index)
{
// TODO Auto-generated method stub
return null;
}
public int getPreferredWidth(ListField listField)
{
// TODO Auto-generated method stub
return 0;
}
public int indexOfList(ListField listField, String prefix, int start)
{
// TODO Auto-generated method stub
return 0;
}
}
for handling the event Use the "TouchEvent" on each row.
I have listfield which contains several rows.
It is working fine when i am using in blackberry torch(I can scroll the listfield and can select(click) any row).
But the same application when i am using for blackberry storm 9500 I can not scroll because as soon as i am trying to scroll the row is getting selected(click).please tell me the reason why it is happening or the way to use listfield in storm
thank you
My lisfield class is
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.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
public class SpeakersList implements ListFieldCallback
{
private String[] products;
private int rgb=Color.BLACK;
Bitmap arraow;
Bitmap placeholder;
Bitmap holder[];
int i=0;
ImageLoad load;
public Bitmap _bmap;
ListField listField;
TaskWorker taskWorker;
public SpeakersList(String[] products)
{
this.products=products;
arraow= Bitmap.getBitmapResource("arrow.png");
DynamicImages images=new DynamicImages();
placeholder=Bitmap.getBitmapResource(images.defaultimage);
holder=new Bitmap[QandAScreen.imglist.length];
taskWorker = new TaskWorker();
taskWorker.addTask(new ImageDowload());
}
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width)
{
this.listField=listField;
final String text=(String) get(listField, index);
if (graphics.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
{
if(holder[index]==null)
{
holder[index]=placeholder;
}
graphics.setColor(0xC0C0C0);
graphics.fillRect(0,y+0,480,59);
graphics.setColor(rgb);
graphics.setFont(Utility.getBigFont(15));
graphics.drawBitmap(3,y+7,placeholder.getWidth(), placeholder.getHeight(),holder[index], 0, 0);
graphics.drawText(text,70,y+20);
if(Display.getWidth()==480){
graphics.drawBitmap(460,y+20,arraow.getWidth(), arraow.getHeight(),arraow, 0, 0);
}
else if(Display.getWidth()==360)
{
graphics.drawBitmap(330,y+20,arraow.getWidth(), arraow.getHeight(),arraow, 0, 0);
}
else
{
graphics.drawBitmap(300,y+20,arraow.getWidth(), arraow.getHeight(),arraow, 0, 0);
}
graphics.drawLine(0, y+59, Display.getWidth(), y+59);
}
else
{
if(holder[index]==null)
{
holder[index]=placeholder;
}
graphics.setColor(rgb);
graphics.setFont(Utility.getBigFont(15));
graphics.drawBitmap(3,y+7,placeholder.getWidth(), placeholder.getHeight(),holder[index], 0, 0);
graphics.drawText(text,70,y+20);
if(Display.getWidth()==480){
graphics.drawBitmap(460,y+20,arraow.getWidth(), arraow.getHeight(),arraow, 0, 0);
}
else if(Display.getWidth()==360)
{
graphics.drawBitmap(330,y+20,arraow.getWidth(), arraow.getHeight(),arraow, 0, 0);
}
else
{
graphics.drawBitmap(300,y+20,arraow.getWidth(), arraow.getHeight(),arraow, 0, 0);
}
graphics.drawLine(0, y+59, Display.getWidth(), y+59);
}
}
public Object get(ListField listField, int index)
{
return products[index];
}
public int getPreferredWidth(ListField listField)
{
return Display.getWidth()+10;
}
public int indexOfList(ListField listField, String prefix, int start)
{
return -1;
}
class ImageDowload extends Task
{
void doTask()
{
for(;i<QandAScreen.imglist.length;i++)
{
String imgpath=QandAScreen.imglist[i];
if(imgpath==null || imgpath.length()==0)
{
continue;
}
load=new ImageLoad(QandAScreen.imglist[i]+Const.getExtra());
if(load.getData()!=null)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
_bmap=load.getBitmap(40,40);
listField.invalidate(i-1);
holder[i-1]=_bmap;
}
});
}
}
}
}
}
Are you testing with the simulators or real devices? If my memory serves, the 9500 Storm uses the SureClick -display, where the display actually has a small microswitch underneath it, so it can detect touch and clicking (pressing the display) as separate actions. In the simulator, you need to use right mouse button to simulate touch and left to simulate click (or was it the other way around?). Torch (9800) hasn't got the SureClick-thingamabob, so the list can be scrolled with both right and left mouse buttons in the simulator (although they did have some distinction, other worked as touch and other sends continuous 'taps' to the screen or something).
I'm trying to display a TextField and a ListField below it:
And I would like to filter (aka "live search") the number of displayed rows, while the user is typing a word into the TextField.
I've tried calling ListField.setSearchable(true) but it doesn't change anything, even if I type words while having the ListField focussed.
And by the way I wonder which TextField to take. I've used AutoCompleteField because it looks exactly as I want the field to be (white field with rounded corners), but it is probably not the best choice (because I don't need AutoCompleteField's drop down list while typing).
Here is my current code -
MyScreen.java:
private ListField presetListField = new ListField();
private MyList presetList = new MyList(presetListField);
private MyScreen() {
int size;
getMainManager().setBackground(_bgOff);
setTitle("Favorites");
BasicFilteredList filterList = new BasicFilteredList();
String[] days = {"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"};
int uniqueID = 0;
filterList.addDataSet(uniqueID, days, "days",
BasicFilteredList.COMPARISON_IGNORE_CASE);
// XXX probably a bad choice here?
AutoCompleteField autoCompleteField =
new AutoCompleteField(filterList);
add(autoCompleteField);
presetListField.setEmptyString("* No Favorites *", DrawStyle.HCENTER);
add(presetListField);
presetList.insert("Monday");
presetList.insert("Tuesday");
presetList.insert("Wednesday");
for (int i = 0; i < 16; i++) {
presetList.insert("Favorite #" + (1 + i));
}
}
MyList.java:
public class MyList implements ListFieldCallback {
private Vector _preset = new Vector();
private ListField _list;
public MyList(ListField list) {
_list = list;
_list.setCallback(this);
_list.setRowHeight(-2);
// XXX does not seem to have any effect
_list.setSearchable(true);
}
public void insert(String str) {
insert(str, _preset.size());
}
public void insert(String str, int index) {
_preset.insertElementAt(str, index);
_list.insert(index);
}
public void delete(int index) {
_preset.removeElementAt(index);
_list.delete(index);
}
public void drawListRow(ListField listField,
Graphics g, int index, int y, int width) {
Font f = g.getFont();
Font b = f.derive(Font.BOLD, f.getHeight() * 2);
Font i = f.derive(Font.ITALIC, f.getHeight());
g.setColor(Color.WHITE);
g.drawText((String)_preset.elementAt(index), Display.getWidth()/3, y);
g.setFont(i);
g.setColor(Color.GRAY);
g.drawText("Click to get frequency",
Display.getWidth()/3, y + g.getFont().getHeight());
g.setFont(b);
g.setColor(Color.YELLOW);
g.drawText(String.valueOf(100f + index/10f), 0, y);
}
public Object get(ListField list, int index) {
return _preset.elementAt(index);
}
public int indexOfList(ListField list, String prefix, int start) {
return _preset.indexOf(prefix, start);
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
}
Thank you!
Alex
Have you checked the net.rim.device.api.ui.component.KeywordFilterField ?
I want to input a Hexadecimal number in a EditText. How to set keyListener in order to limit the input value?
Thank you!
Use android:inputType="number" in your EditText xml
You can also add text watcher editText.addTextChangedListener (new MyTextWatcher ());
private class MyTextWatcher implements TextWatcher {
#Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged (Editable s) {
}
}