I'm trying to display an extra row at the top of a ListField - called "Add Item" and it almost works, but the very last row "Item 4" of my data is not displayed.
How would you fix this issue?
Please see my very simple test code MyList.java below:
package mypackage;
import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.util.*;
public class MyList extends UiApplication {
public static void main(String args[]) {
MyList app = new MyList();
app.enterEventDispatcher();
}
public MyList() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
ObjectListField myList = new ObjectListField() {
public void drawListRow(ListField list,
Graphics g,
int index,
int y,
int width) {
if (index == 0) {
Font i = getFont().derive(Font.ITALIC);
g.setFont(i);
g.drawText("Add Item", 0, y);
} else {
String str = (String) get(this, index - 1);
g.drawText(str, 0, y);
}
}
protected boolean navigationClick(int status, int time) {
int index = myList.getSelectedIndex();
String str = (index > 0 ? (String) get(this, index - 1) :
"Add item");
Status.show("You have clicked: " + str);
return true;
}
};
public MyScreen() {
setTitle("How to display an extra row?");
myList.set(new String[]{"Item 1","Item 2","Item 3","Item 4",});
// myList.setSize(5);
add(myList);
}
}
I've tried adding myList.setSize(5); but get an ArrayIndexOutOfBoundsException thrown by Vector.elementAt(4)
UPDATE:
On Michael's suggestion I've switched from ObjectListField to KeywordFilterField (which is what I actually use in my real program) and now I can call myList.setSize(5) without any exception being thrown.
But the last row is still not displayed:
package mypackage;
import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.util.*;
public class MyList extends UiApplication {
public static void main(String args[]) {
MyList app = new MyList();
app.enterEventDispatcher();
}
public MyList() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
static final int EXTRA_ROWS = 1;
MyItemList myItems = new MyItemList();
KeywordFilterField myList = new KeywordFilterField() {
protected boolean navigationClick(int status, int time) {
int index = getSelectedIndex();
String str = (index < EXTRA_ROWS ? "Add item" :
((MyItem) getElementAt(index - EXTRA_ROWS)).toString());
Status.show("You have clicked - " + str);
return true;
}
};
public MyScreen() {
setTitle(myList.getKeywordField());
myList.setSourceList(myItems, new MyItem.MyProvider());
myItems.doAdd(new MyItem(1, "Eins"));
myItems.doAdd(new MyItem(2, "Zwei"));
myItems.doAdd(new MyItem(3, "Drei"));
myItems.doAdd(new MyItem(4, "Vier"));
myList.setSourceList(myItems, new MyItem.MyProvider());
myList.setCallback(new MyListFieldCallback());
myList.setSize(myItems.size() + EXTRA_ROWS);
myList.updateList();
add(myList);
}
private class MyListFieldCallback implements ListFieldCallback {
public void drawListRow(ListField list, Graphics g, int index, int y, int width) {
if (index < EXTRA_ROWS) {
Font i = getFont().derive(Font.ITALIC);
g.setFont(i);
g.drawText("Add Item", 0, y);
return;
}
if (index >= EXTRA_ROWS) {
MyItem item = (MyItem) ((KeywordFilterField) list).getElementAt(index - EXTRA_ROWS);
g.drawText(item.toString(), 0, y);
return;
}
g.drawText(list.getEmptyString(), 0, y);
}
public Object get(ListField listField, int index) {
return null;
}
public int getPreferredWidth(ListField listField) {
return 0;
}
public int indexOfList(ListField listField, String prefix, int start) {
return 0;
}
}
}
class MyItemList extends SortedReadableList {
public MyItemList() {
super(new MyItem.MyComparator());
}
protected void doAdd(Object obj) {
super.doAdd(obj);
}
protected boolean doRemove(Object obj) {
return super.doRemove(obj);
}
}
class MyItem {
int _num;
String _name;
public MyItem(int num, String name) {
_num = num;
_name = name;
}
public String toString() {
return _num + ": " + _name;
}
static class MyComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
MyItem item1 = (MyItem) obj1;
MyItem item2 = (MyItem) obj2;
return item1.toString().compareTo(item2.toString());
}
}
static class MyProvider implements KeywordProvider {
public String[] getKeywords(Object obj) {
MyItem item = (MyItem) obj;
return new String[]{ Integer.toString(item._num), item._name };
}
}
}
I have a feeling, that setSize(4) (instead of 5) is called inbetween - because I see the last row for a moment and then it disappears again.
Thank you!
Alex
You don't want to use an ObjectListField, go up one level in the type hierarchy to ListField, and then you can do setSize() as you please.
I guess this should work:
public MyScreen() {
setTitle("How to display an extra row?");
myList.set(new String[] { "Add Item", "Item 1","Item 2","Item 3","Item 4"});
add(myList);
}
Or am I missing smth? :)
Here is my own solution - override the setSize() of the ListField:
(the screenshot above has EXTRA_ROWS = 3, which still works ok).
package mypackage;
import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.util.*;
public class MyList extends UiApplication {
public static void main(String args[]) {
MyList app = new MyList();
app.enterEventDispatcher();
}
public MyList() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
static final int EXTRA_ROWS = 3;
MyItemList myItems = new MyItemList();
KeywordFilterField myList = new KeywordFilterField() {
protected boolean navigationClick(int status, int time) {
int index = getSelectedIndex();
String str = (index < EXTRA_ROWS ? "Add item" :
((MyItem) getElementAt(index - EXTRA_ROWS)).toString());
Status.show("You have clicked - " + str);
return true;
}
public void setSize(int count) {
super.setSize(count + EXTRA_ROWS);
}
};
public MyScreen() {
setTitle(myList.getKeywordField());
myList.setSourceList(myItems, new MyItem.MyProvider());
myItems.doAdd(new MyItem(1, "Eins"));
myItems.doAdd(new MyItem(2, "Zwei"));
myItems.doAdd(new MyItem(3, "Drei"));
myItems.doAdd(new MyItem(4, "Vier"));
myList.setSourceList(myItems, new MyItem.MyProvider());
myList.setCallback(new MyListFieldCallback());
add(myList);
}
private class MyListFieldCallback implements ListFieldCallback {
public void drawListRow(ListField list, Graphics g, int index, int y, int width) {
if (index < EXTRA_ROWS) {
Font i = getFont().derive(Font.ITALIC);
g.setFont(i);
g.drawText("Add Item", 0, y);
return;
}
if (index >= EXTRA_ROWS) {
MyItem item = (MyItem) ((KeywordFilterField) list).getElementAt(index - EXTRA_ROWS);
g.drawText(item.toString(), 0, y);
return;
}
g.drawText(list.getEmptyString(), 0, y);
}
public Object get(ListField listField, int index) {
return null;
}
public int getPreferredWidth(ListField listField) {
return 0;
}
public int indexOfList(ListField listField, String prefix, int start) {
return 0;
}
}
}
class MyItemList extends SortedReadableList {
public MyItemList() {
super(new MyItem.MyComparator());
}
protected void doAdd(Object obj) {
super.doAdd(obj);
}
protected boolean doRemove(Object obj) {
return super.doRemove(obj);
}
}
class MyItem {
int _num;
String _name;
public MyItem(int num, String name) {
_num = num;
_name = name;
}
public String toString() {
return _num + ": " + _name;
}
static class MyComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
MyItem item1 = (MyItem) obj1;
MyItem item2 = (MyItem) obj2;
return item1.toString().compareTo(item2.toString());
}
}
static class MyProvider implements KeywordProvider {
public String[] getKeywords(Object obj) {
MyItem item = (MyItem) obj;
return new String[]{ Integer.toString(item._num), item._name };
}
}
}
Related
I have made a custom auto suggest list.
I want to set the selcted value in edit field.
I am using this Place holder text on a AutoCompleteField in blackberry
CustomAutoCompleteField.js
package mypackage;
import net.rim.device.api.collection.util.BasicFilteredList;
import net.rim.device.api.collection.util.BasicFilteredListResult;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.AutoCompleteField;
import net.rim.device.api.ui.component.ListField;
class CustomAutoCompleteField extends AutoCompleteField {
private int yOffset = 0;
private int xOffset = 0;
public CustomAutoCompleteField(BasicFilteredList filteredList) {
super(filteredList);
}
protected void paint(Graphics g) {
super.paint(g);
if (xOffset == 0) {
// initialize text offsets once
xOffset = getEditField().getContentLeft();
yOffset = getEditField().getContentTop();
}
String text = getEditField().getText();
if (text == null || text.length() == 0) {
int oldColor = g.getColor();
g.setColor(Color.GRAY);
g.drawText("enter text", xOffset, yOffset);
g.setColor(oldColor);
}
}
public void onSelect(Object selection, int SELECT_TRACKWHEEL_CLICK) {
ListField _list = getListField();
if (_list.getSelectedIndex() > -1) {
final String selectedText = getEditField().getText();
if(selectedText!=null){
final BasicFilteredListResult result = (BasicFilteredListResult) selection;
Application.getApplication().invokeLater(new Runnable() {
public void run() {
selectedText.setText(result._object.toString());
}
});
// selectedText.setText(result._object.toString());
}
}
}
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
super.sublayout(250, 250);
}
}
myscreen.js
public final class MyScreen extends MainScreen
{
/**
* Creates a new MyScreen object
*/
public MyScreen()
{
// Set the displayed title of the screen
BasicFilteredList filterList = new BasicFilteredList();
String[] days = {"Monday(TAS)","Tuesday-(PAQ)","Wednesday-(MAN)",
"Thursday","Friday","Saturday","Sunday(I_)"};
filterList.addDataSet(1,days,"days",BasicFilteredList.COMPARISON_IGNORE_CASE);
HorizontalFieldManager hr =new HorizontalFieldManager();
LabelField userName= new LabelField("hjsdhas");
hr.add(userName);
CustomAutoCompleteField autoCompleteField = new CustomAutoCompleteField(filterList);
hr.add(autoCompleteField);
add(hr);
}
}
CustomAutoCompleteField.java is my custom java class and I call the class from myscreen.java class
Can anyone please help me to create a custom drop down similar to the image attached in blackberry 5.0. I have used object choice field but it is coming along with label. Guide me to create custom drop down as show in image below
I have created my customized ChoiceField, using popup Screen. Here is the below code sample
package com.src.java.rim.ui;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
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.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class FWCustomChoiceField extends HorizontalFieldManager {
/*space between text and icon*/
final int padding = 10;
String arrowBitmapName = "arrow_state_grey_right.png";
Object choice[] = null;
int index = -1;
String text = "Please select one option";
ListField choiceField = null;
public FWCustomChoiceField(final Object choice[]) {
this.choice = new Object[choice.length];
this.choice = choice;
choiceField = new ListField(){
protected boolean navigationClick(int status, int time) {
Field focus = UiApplication.getUiApplication().getActiveScreen() .getLeafFieldWithFocus();
if (focus instanceof ListField) {
ChoicePopupScreen popup = new ChoicePopupScreen(10, 80, choice);
popup.setChoice(choice);
UiApplication.getUiApplication().pushScreen(popup);
}
return super.navigationClick(status, time);
}
};
choiceField.setSize(1);
choiceField.setCallback(new TestListCallback());
add(choiceField);
}
public void setSelIndex(int index){
this.index = index;
this.text = choice[index].toString();
choiceField.invalidate();
}
public int getSelectedIndex(){
return index;
}
final class TestListCallback implements ListFieldCallback {
TestListCallback() {
}
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
Bitmap bitmap = Bitmap.getBitmapResource(arrowBitmapName);
int fontWidth = getFont().getAdvance(text);
int width = Display.getWidth() ;
int posX = (width-(fontWidth + padding + bitmap.getWidth()))/2;
int height = list.getRowHeight();
int posY = (height - bitmap.getHeight())/2;
g.drawText(text, posX, y, 0, w);
g.drawBitmap(posX+fontWidth+padding,posY, bitmap.getWidth(), bitmap.getHeight(), bitmap, 0, 0);
}
public Object get(ListField listField, int index) {
return null;
}
public int getPreferredWidth(ListField listField) {
return Graphics.getScreenWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
return listField.indexOfList(prefix, start);
}
}
public class ChoicePopupScreen extends PopupScreen {
Object []choice = null;
/*holds the position for popup screen*/
private int _posX = 0;
private int _posY = 0;
public ChoicePopupScreen(int posx,int posy, Object[] choice) {
/*calling super class constructor*/
super(new VerticalFieldManager(), Field.FOCUSABLE);
this.setMargin(new XYEdges(1,1,1,1));
this.choice = new Object[choice.length];
this.choice = choice;
/*Setting position for popup screen*/
_posX = posx;
_posY = posy;
/*list field customized to display as choice picker*/
ListField choiceList = new ListField(){
/*list field event handling using navigation click*/
protected boolean navigationClick(int status, int time) {
/*getting the selected list index value*/
int index = this.getSelectedIndex();
/*returning the selected index to the main field*/
setSelIndex(index);
/*removing the popup screen from the screen stack*/
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
/*required for event handling*/
return super.navigationClick(status, time);
}
};
/*displays the message when list is empty*/
choiceList.setEmptyString("List is empty", DrawStyle.LEFT);
choiceList.setSize(choice.length);
choiceList.setCallback(new PopUpListCallback());
add(choiceList);
Border border = BorderFactory.createSimpleBorder( new XYEdges(), Border.STYLE_TRANSPARENT);
this.setBorder(border);
}
protected void setChoice(Object []choice) {
this.choice = new Object[choice.length];
this.choice = choice;
}
protected void sublayout(int width, int height) {
super.sublayout(width, height);
/*setting the position for the popup screen*/
setPosition(_posX , _posY);
}
}
private class PopUpListCallback implements ListFieldCallback {
PopUpListCallback() {
}
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
String text = choice[index].toString();
g.drawText(text, padding, y, 0, w);
}
public Object get(ListField listField, int index) {
return null;
}
public int getPreferredWidth(ListField listField) {
return Graphics.getScreenWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
return listField.indexOfList(prefix, start);
}
}
}
To use the above class
String choicestrs[] = {"Opt 1", "Opt 2", "Opt 3"};
add(new FWCustomChoiceField(choicestrs));
In screen1 i have 2 buttons a and b.i have given field change listener for both such that each pushes corresponding screen .When i press button 'a' it pushes say screen2 where i have used a keywordFilter to search words from sqlite database and is listed.These all are perfect when i run the application ,but when i press the back(or previous key) and then press the button 'a' from screen1 again i dont find any results from database.same thing happens to button b also .What cud b the pblm pls help.
Thank u so much in advance
Below is the class called after button 'a' is clicked
import net.rim.device.api.ui.*;
import net.rim.device.api.io.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.system.*;
import java.util.*;
import net.rim.device.api.database.Database;
import net.rim.device.api.database.DatabaseFactory;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;
public final class KeywordFilter
{
private KeywordFilterField _keywordFilterField;
private WordList _wordList;
private Vector _words;
public KeywordFilter()
{
_words = getDataFromDatabase();
if(_words != null)
{
_wordList = new WordList(_words);
_keywordFilterField = new KeywordFilterField();
_keywordFilterField.setSourceList(_wordList, _wordList);
CustomKeywordField customSearchField = new CustomKeywordField();
_keywordFilterField.setKeywordField(customSearchField);
KeywordFilterScreen screen = new KeywordFilterScreen(this);
screen.setTitle(_keywordFilterField.getKeywordField());
screen.add(_keywordFilterField);
UiApplication ui = UiApplication.getUiApplication();
ui.pushScreen(screen);
}
else
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Error reading data file.");
System.exit(0);
}
});
}
}
KeywordFilterField getKeywordFilterField()
{
return _keywordFilterField;
}
private Vector getDataFromDatabase()
{
Vector words = new Vector();
Database d;
for(;;)
{
try
{
URI myURI=URI.create("file:///SDCard/Databases/MyTestDatabase.db");
d=DatabaseFactory.open(myURI);
Statement st=d.createStatement("SELECT eng,mal FROM Malayalam m,English e where e.Ecode=m.Mcode");
st.prepare();
net.rim.device.api.database.Cursor c=st.getCursor();
Row r;
while(c.next())
{
r=c.getRow();
String w=r.getString(0);
String meaning=r.getString(1);
words.addElement(new Word(w,meaning));
}}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
return words;
}
}
void addElementToList(Word w)
{
_wordList.addElement(w);
_keywordFilterField.updateList();
}
final static class CustomKeywordField extends BasicEditField
{
CustomKeywordField()
{
super(USE_ALL_WIDTH|NON_FOCUSABLE|NO_LEARNING|NO_NEWLINE);
setLabel("Search: ");
}
protected boolean keyChar(char ch, int status, int time)
{
switch(ch)
{
case Characters.ESCAPE:
if(super.getTextLength() > 0)
{
setText("");
return true;
}
}
return super.keyChar(ch, status, time);
}
protected void paint(Graphics graphics)
{
super.paint(graphics);
getFocusRect(new XYRect());
drawFocus(graphics, true);
}
}
}
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
final class KeywordFilterScreen extends MainScreen
{
private KeywordFilter _app;
private KeywordFilterField _keywordFilterField;
public KeywordFilterScreen(KeywordFilter app)
{
_app = app;
_keywordFilterField = _app.getKeywordFilterField();
}
protected boolean keyChar(char key, int status, int time)
{
if (key == Characters.ENTER)
{
displayInfoScreen();
return true;
}
return super.keyChar(key, status, time);
}
public boolean invokeAction(int action)
{
switch(action)
{
case ACTION_INVOKE:
displayInfoScreen();
return true;
}
return super.invokeAction(action);
}
private void displayInfoScreen()
{
Word w = (Word)_keywordFilterField.getSelectedElement();
if(w != null)
{
InfoScreen infoScreen = new InfoScreen(w);
UiApplication ui = UiApplication.getUiApplication();
ui.pushScreen(infoScreen);
ui.popScreen(this);
}
}
private final static class InfoScreen extends MainScreen
{
InfoScreen(Word w)
{
setTitle(w.toString());
BasicEditField popField = new BasicEditField(":",w.getMeaning(),20,Field.NON_FOCUSABLE);
add(popField);
}
}
}
public class Word
{
private String _word;
private String _meaning;
public Word(String word, String meaning)
{
_word = word;
_meaning = meaning;
}
String getMeaning()
{
return _meaning;
}
public String toString()
{
return _word;
}
}
import net.rim.device.api.ui.component .*;
import net.rim.device.api.collection.util.*;
import net.rim.device.api.util.*;
import java.util.*;
public class WordList extends SortedReadableList implements KeywordProvider
{
public WordList(Vector words)
{
super(new WordListComparator());
loadFrom(words.elements());
}
void addElement(Object element)
{
doAdd(element);
}
public String[] getKeywords( Object element )
{
if(element instanceof Word )
{
return StringUtilities.stringToWords(element.toString());
}
return null;
}
final static class WordListComparator implements Comparator
{
public int compare(Object o1, Object o2)
{
if (o1 == null || o2 == null)
throw new IllegalArgumentException("Cannot compare null words");
return o1.toString().compareTo(o2.toString());
}
}
}
add st.close(); and d.close() after accessing db in private Vector getDataFromDatabase() method just before closing the try block
I'm trying to create an app displaying few extra rows on the top of a list of items with a "live search" field on the top:
Of course I've tried using a KeywordFilterField with overriden setSize() for that at first. But this didn't work well - because I couldn't control, when and how setSize() was called by the KeywordFilterField and this has given me drawing issues (a row was not drawn when I had a keyword entered and added a new item to the list, etc.)
So I am trying to "go back to the roots" and use a ListField (and a BasicEditField on the top) this time, because there I can control myself, when and how setSize() is called.
Below is my simplified test code - src\mypackage\MyList.java and border.png.
The code works well, except for the filtering issue - which I will describe below.
package mypackage;
import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.util.*;
public class MyList extends UiApplication {
public static void main(String args[]) {
MyList app = new MyList();
app.enterEventDispatcher();
}
public MyList() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
static final int EXTRA_ROWS = 3;
BasicFilteredList myFilter = new BasicFilteredList();
Background myBg = BackgroundFactory.createSolidBackground(0x111111);
StringProvider myProvider = new StringProvider("Search");
BasicEditField myFind = new BasicEditField(USE_ALL_WIDTH) {
protected void paint(Graphics g) {
if (getTextLength() == 0) {
g.setColor(Color.LIGHTGRAY);
g.drawText(myProvider.toString(), 0, 0);
}
g.setColor(Color.BLACK);
super.paint(g);
}
};
MyItemList myItems = new MyItemList();
// add 1 row for the "* Empty *" string
ListField myList = new ListField(EXTRA_ROWS + 1) {
protected boolean navigationClick(int status, int time) {
int index = getSelectedIndex();
if (index >= EXTRA_ROWS && myItems.size() > 0) {
String str = ((MyItem) myItems.getAt(
index - EXTRA_ROWS)).toString();
Status.show("You have clicked - " + str);
}
return true;
}
};
Border myBorder = BorderFactory.createBitmapBorder(
new XYEdges(12, 12, 12, 12),
Bitmap.getBitmapResource("border.png"));
public MyScreen() {
getMainManager().setBackground(myBg);
myFind.setBorder(myBorder);
setTitle(myFind);
myItems.doAdd(new MyItem(1, "Eins"));
myItems.doAdd(new MyItem(2, "Zwei"));
myItems.doAdd(new MyItem(3, "Drei"));
myItems.doAdd(new MyItem(4, "Vier"));
myList.setCallback(new MyListFieldCallback());
add(myList);
myFilter.addDataSet(1, myItems.getElements(), "Test",
BasicFilteredList.COMPARISON_IGNORE_CASE);
}
protected boolean keyChar(char key, int status, int time) {
if (key == Characters.BACKSPACE && myFind.getTextLength() == 0) {
Status.show("Kill kill kill!");
return true;
}
if (myFind.getTextLength() > 0) {
System.err.println("XXX filter for " + myFind.getText());
}
return super.keyChar(key, status, time);
}
private class MyListFieldCallback implements ListFieldCallback {
public void drawListRow(ListField list, Graphics g, int index, int y, int width) {
Font i = getFont().derive(Font.ITALIC);
g.setColor(Color.DIMGRAY);
g.drawLine(0, y-9, width, y-9);
g.setColor(Color.WHITE);
if (index < EXTRA_ROWS) {
g.setFont(i);
g.drawText("Add Item", 0, y, DrawStyle.ELLIPSIS|DrawStyle.HCENTER, width);
return;
}
if (myItems.size() == 0) {
g.setFont(i);
g.drawText(list.getEmptyString(), 0, y, DrawStyle.ELLIPSIS|DrawStyle.HCENTER, width);
return;
}
MyItem item = (MyItem) myItems.getAt(index - EXTRA_ROWS);
g.drawText(item.toString(), 0, y, DrawStyle.ELLIPSIS|DrawStyle.HCENTER, width);
}
public Object get(ListField list, int index) {
return myItems.getAt(index);
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
public int indexOfList(ListField list, String prefix, int start) {
return 0;
}
}
class MyItemList extends SortedReadableList {
public MyItemList() {
super(new MyItem.MyComparator());
}
protected void doAdd(Object obj) {
super.doAdd(obj);
myList.setSize(size() + EXTRA_ROWS);
}
protected boolean doRemove(Object obj) {
// if the list is empty, add 1 row for "* Empty *" string
int size = (size() == 0 ? EXTRA_ROWS + 1 : size() - 1 + EXTRA_ROWS);
myList.setSize(size);
return super.doRemove(obj);
}
protected Object[] getElements() {
return super.getElements();
}
}
}
class MyItem {
int _num;
String _name;
public MyItem(int num, String name) {
_num = num;
_name = name;
}
public String toString() {
return _num + ": " + _name;
}
static class MyComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
MyItem item1 = (MyItem) obj1;
MyItem item2 = (MyItem) obj2;
return item1.toString().compareTo(item2.toString());
}
}
static class MyProvider implements KeywordProvider {
public String[] getKeywords(Object obj) {
MyItem item = (MyItem) obj;
return new String[]{ Integer.toString(item._num), item._name };
}
}
}
My problem is that I do not know how to add filtering.
For example when a user enters "E" into myFind, my code will detect this event in the keyChar() and also I understand, that I will have to call myList.setSize(EXTRA_ROWS + 1) because there will be 1 filtered result.
But how do I filter the items displayed by myList, how to implement this?
Maybe I could use BasicFilteredList and its method
public void addDataSet(int id,
Object[] objects,
String name,
long style)
but I'm not sure how to apply it here?
UPDATE:
I've added a BasicFilteredList to my test code above, but am still not sure how to marry it with a ListField.
You have to do the filtering in code somewhere, and then store the result. Override myFind.update(int) to run the filtering when the filtering text changes. In addition to myItems, you need to store myFilteredItems. After filtering, call setSize() on the listfield. The listfield drawing code also needs to draw from the filtered collection. Then you should be set.
I've ended up with this code which seems to work well for me (except 2 minor issues listed below) and the green answer mark goes to Michael Donohue for his valuable suggestions.
I use 2 Vectors: myData and myFilteredData. And for UI I use a ListField and a BasicEditField:
border.png:
src\mypackage\MyList.java:
package mypackage;
import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.util.*;
public class MyList extends UiApplication {
public static void main(String args[]) {
MyList app = new MyList();
app.enterEventDispatcher();
}
public MyList() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
static final int EXTRA_ROWS = 3;
Background myBg = BackgroundFactory.createSolidBackground(0x111111);
StringProvider myProvider = new StringProvider("Search");
BasicEditField myFind = new BasicEditField(USE_ALL_WIDTH|TextField.NO_NEWLINE) {
protected void paint(Graphics g) {
if (getTextLength() == 0) {
g.setColor(Color.LIGHTGRAY);
g.drawText(myProvider.toString(), 0, 0);
}
g.setColor(Color.BLACK);
super.paint(g);
}
protected void update(int delta) {
filterData(getText());
}
};
Vector myData = new Vector();
final static SimpleSortingVector myFilteredData = new SimpleSortingVector();
// add 1 row for the "* Empty *" string
ListField myList = new ListField(EXTRA_ROWS + 1) {
protected boolean navigationClick(int status, int time) {
int index = getSelectedIndex();
if (index >= EXTRA_ROWS && myFilteredData.size() > 0) {
String str = ((MyItem) myFilteredData.elementAt(index - EXTRA_ROWS)).toString();
Status.show("You have clicked - " + str);
}
return true;
}
};
Border myBorder = BorderFactory.createBitmapBorder(
new XYEdges(12, 12, 12, 12),
Bitmap.getBitmapResource("border.png"));
public MyScreen() {
getMainManager().setBackground(myBg);
myFind.setBorder(myBorder);
setTitle(myFind);
myData.addElement(new MyItem(1, "Eins"));
myData.addElement(new MyItem(2, "Zwei"));
myData.addElement(new MyItem(3, "Drei"));
myData.addElement(new MyItem(4, "Vier"));
myFilteredData.setSortComparator(new MyItem.MyComparator());
filterData("");
myList.setCallback(new MyListFieldCallback());
add(myList);
}
private void filterData(String prefix) {
myFilteredData.removeAllElements();
if (prefix == null || prefix.length() == 0) {
// no prefix specified - copy everything
for (int i = myData.size() - 1; i >= 0; i--) {
MyItem item = (MyItem) myData.elementAt(i);
myFilteredData.addElement(item);
}
} else {
// copy only strings starting with prefix
for (int i = myData.size() - 1; i >= 0; i--) {
MyItem item = (MyItem) myData.elementAt(i);
String name = item.name.toLowerCase();
if (name.startsWith(prefix.toLowerCase())) {
myFilteredData.addElement(item);
}
}
}
myFilteredData.reSort();
// if the list is empty, add 1 row for "* Empty *" string
int size = (myFilteredData.size() == 0 ? EXTRA_ROWS + 1 : myFilteredData.size() + EXTRA_ROWS);
myList.setSize(size);
}
protected boolean keyChar(char key, int status, int time) {
if (key == Characters.BACKSPACE && myFind.getTextLength() == 0) {
int index = myList.getSelectedIndex();
if (index >= EXTRA_ROWS && myFilteredData.size() > 0) {
String str = ((MyItem) myFilteredData.elementAt(index - EXTRA_ROWS)).toString();
Status.show("You're deleting - " + str);
}
return true;
}
return super.keyChar(key, status, time);
}
static class MyListFieldCallback implements ListFieldCallback {
public void drawListRow(ListField list, Graphics g, int index, int y, int width) {
Font i = g.getFont().derive(Font.ITALIC);
g.setColor(Color.DIMGRAY);
g.drawLine(0, y-9, width, y-9);
g.setColor(Color.WHITE);
if (index < EXTRA_ROWS) {
g.setFont(i);
g.drawText("Add Item", 0, y, DrawStyle.ELLIPSIS|DrawStyle.HCENTER, width);
return;
}
if (myFilteredData.size() == 0) {
g.setFont(i);
g.drawText(list.getEmptyString(), 0, y, DrawStyle.ELLIPSIS|DrawStyle.HCENTER, width);
return;
}
MyItem item = (MyItem) myFilteredData.elementAt(index - EXTRA_ROWS);
g.drawText(item.toString(), 0, y, DrawStyle.ELLIPSIS|DrawStyle.HCENTER, width);
}
public Object get(ListField list, int index) {
return myFilteredData.elementAt(index);
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
public int indexOfList(ListField list, String prefix, int start) {
return myFilteredData.indexOf(prefix, start);
}
}
}
class MyItem {
int num;
String name;
public MyItem(int num, String name) {
this.num = num;
this.name = name;
}
public String toString() {
return num + ": " + name;
}
static class MyComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
MyItem item1 = (MyItem) obj1;
MyItem item2 = (MyItem) obj2;
String name1 = item1.toString().toLowerCase();
String name2 = item2.toString().toLowerCase();
return name1.compareTo(name2);
}
}
}
My minor problems are:
Why do I need to subtract 9 in drawListRow() to draw the lines?
Why does the caret disappear in BasicEditField after using it?
How can I display scroll like marquee text in Blackberry using J2ME? That moves from left to right or vertically?
Any help will be very much appreciated.
its really easy to display.
check the code below .
import java.util.Timer;
import java.util.TimerTask;
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.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class MarqueeApp extends UiApplication {
public MarqueeApp() {
pushScreen(new MarqueeScreen());
}
public static void main(String[] args) {
(new MarqueeApp()).enterEventDispatcher();
}
}
class MarqueeScreen extends MainScreen {
public MarqueeScreen() {
setTitle(new LabelField("hi"));
MarqueeLabel testLabel1 = new MarqueeLabel("This is a marquee",
Field.FOCUSABLE);
add(testLabel1);
MarqueeLabel testLabel2 = new MarqueeLabel("This is a long long " +
"long long long long long long long long long long long " +
"long long marquee", Field.FOCUSABLE);
add(testLabel2);
}
}
class MarqueeLabel extends LabelField {
int currentChar = 0;
String currentText = null;
Font ourFont;
private Timer _scrollTimer;
private TimerTask _scrollTimerTask;
public MarqueeLabel(String text, long style) {
super(text, style);
}
public void paint(Graphics graphics) {
currentText = this.getText();
if (currentChar < currentText.length()) {
currentText = currentText.substring(currentChar);
}
graphics.drawText(currentText, 0, 0, DrawStyle.ELLIPSIS, 200);
super.paint(graphics);
}
public void layout(int width, int height) {
ourFont = this.getFont();
setExtent(500, ourFont.getHeight());
}
protected void onDisplay() {
startScroll();
}
protected void onUnfocus() {
startScroll();
}
private void startScroll() {
// Start scrolling
final String fullText = this.getText();
if (_scrollTimer == null) {
_scrollTimer = new Timer();
_scrollTimerTask = new TimerTask() {
public void run() {
currentChar = currentChar + 4;
if (currentChar > fullText.length()) {
currentChar = 0;
}
invalidate();
}
};
_scrollTimer.scheduleAtFixedRate(_scrollTimerTask, 0, 300);
}
}
protected void onFocus(int direction) {
if (_scrollTimer != null) {
_scrollTimerTask.cancel();
_scrollTimer.cancel();
_scrollTimer = null;
_scrollTimerTask = null;
}
}
protected boolean navigationMovement(int dx, int dy,
int status, int time) {
currentText = this.getText();
int oldCurrentChar = currentChar;
if (Math.abs(dx) > Math.abs(dy)) {
// horizontal scroll
if (dx > 0) {
currentChar = Math.min(currentText.length() - 1,
currentChar + 1);
} else if (dx < 0) {
currentChar = Math.max(0, currentChar - 1);
}
if (oldCurrentChar != currentChar) {
this.invalidate();
}
return true;
} else {
return super.navigationMovement(dx, dy, status, time);
}
}
}