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
Contributor
EricTheRed03
Posts: 32
Registered: ‎08-13-2012
My Device: 9800
My Carrier: Rogers
Accepted Solution

ObjectChoiceField

[ Edited ]

Hi,

 

I have Employees outlined like so:

 

class Employee
{
int _Eid;
int _Ename;
public Employee(){}
}

 Now lets say I want to create a list of Employees with IDs and populate them in a "ObjectChoiceField" dropdown.. what is the best way to do so? 

 

When the user selects "Rob" from the Dropdown I should be able to determine Rob's "_Eid". 

 

Can I do this?

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: ObjectChoiceField

Update Employee so that its tpString() method displays the Emplyee name you want to see.

 

Create an array of these (call this the employeeList array) and add the array as the choices in an ObjectChoiceField.

 

use the getSelectedIndex() on the ObjectChoiceField and the Employee you want is that index in your employeeList array.

 

Have a cuppa....

Please use plain text.
Contributor
EricTheRed03
Posts: 32
Registered: ‎08-13-2012
My Device: 9800
My Carrier: Rogers

Re: ObjectChoiceField

[ Edited ]

Thank you. I will try this. I notice you said array but after I read my employee id/ employee name (through web api) I have to store them in a dynamic list. So I was thinking a BigVector. Let me know what you think of BigVector vs the array you suggested... My Employee constructor below takes (id, employee name)

BigVector employees = new BigVector();
        Employee employee ;
         for (int i = 0; i< 5; i++)
         {
        	 employee  = new Employee(i +100, "Bob" + i+100);
        	 employees.addElement(employee);
         }
         //find employee id "103" via binary search
         

 I'm not sure how I use the Comparator used for the search. Any insight into this problem would be helpful.

 

Thanks.

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: ObjectChoiceField

If the list is going to change, then a BigVector is a good idea. 

 

But you do have to extract them into an array because the ObjectChoiceField requires an array of choices, not a BigVector of choices. 

 

What I typically do in this sot of situation is intially put the data into a Vector while processing the download, then copy the Vector into an array  once we have the list pretty much fixed. 

 

People say things are dynamic, but they don't change every second, you have to do something, like process some UI stuff, and so converting the collection from array to Vector (or BigVector) and back is not a graet problem.  Also look at the Arrays class - there are loads of utilities in there like Binary Search and Sort that help too.

 

The Compariator is actually nothing to be frigthened of.  In your case, you will create a new class, that extends Object and implements the required method.  You might actually have a number of different compariators.  Here is a quick sample to get you started.

 

class EmployeeComparator implements Comparator
{

 

public int compare(Object o1, Object o2) {

if ( o1 instanceof Employee && o2 instanceof Employee ) {

Employee e1 = (Employee) o1;

Employee e2 = (Employee) o2;

int compareValue = ei._Ename.compareTo( e2._Ename);

if (compareValue == 0 ) {

// name is same, is the id?

compareValue = e1._Eid - e2._Eid;

}

return compareValue;

return 0; // Not Employees!

}

 

}

 

Please use plain text.
Contributor
EricTheRed03
Posts: 32
Registered: ‎08-13-2012
My Device: 9800
My Carrier: Rogers

Re: ObjectChoiceField

Any idea why this may not work

int compareValue = e1._Ename.compareTo( e2._Ename);
Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: ObjectChoiceField

I thought _Ename was a String, but when I look at your code at the start it is not.  So that code is wrong.  Have a go at figuring out what it should be yourself, if you get no-where, post the new source for Employee. 

Please use plain text.
New Developer
saurabh0208
Posts: 10
Registered: ‎08-24-2012
My Device: 9820
My Carrier: Airtel

Re: ObjectChoiceField

1. Override thee Tostring method of class employee.

2. add array of employee to object choice field.

3. get the selected index of object choice field and get the employee with the same index from employee array.

Please use plain text.