Key Value in combobox lwuit? - java-me

I want to store and retrieve a key value associated with the comboBox. I have used only
getSelectedIndex() and getSelectedItem(). This will not help my purpose as I have to get a unique key value assocaiated with the item.
Example Scenario :
India - 10, China - 15, Russia - 18. Here if 'India' is the comboBox item then '10' is its key. Likewise, 15 for china and 18 for Russia.
When India is selected I need to get the value as 10, if china 15, if Russia 18.
How can I achieve this in Lwuit 1.5. could you guys guide me to do this.

I think that you must map the values with the items in the ComboBox.
You can do this in some different ways.
You can do it with a Hashtable for example. You will need to make the proper castings to get the value in the data type what you want.
ComboBox combo;
//Here create the hash
Hashtable h = new Hashtable();
h.put("India", "10");
h.put("China", "15");
h.put("Russia", "18");
//create the combo
Vector v = new Vector();
v.addElement("India");
v.addElement("China");
v.addElement("Russia");
combo = new ComboBox(v);
combo.addActionListerner(new ActionListener ae){
public void actionPerformed(ActionEvent ae){
String selected = (String) combo.getSelectedItem();
//get the value
String value = (String) h.get(selected);
}
});

Related

How to get count of a generic list in C# and no need to consider items with null or empty values in the count?

I need to get the count of a generic list in C#.No need to consider empty and null items in the count of list.
Below is my code
Public class Student
{
public string Name {get;set;}
public string Age {get;set;}
}
List<student> listStudent = new List<student>();
Student studentOne=new Student();
studentOne.Name="abc";
studentOne.Age="20";
listStudent.Add(studentOne);
Student studentTwo=new Student();
studentOne.Name="def";
studentOne.Age="22";
listStudent.Add(studentTwo);
Student studentThree=new Student();
studentOne.Name=" ";
studentOne.Age=null;
listStudent.Add(studentThree);
I have written below code to get the count
listStudent.count
It returns 3.it is correct as it contains 3 rows in the list.But I need to get the count of elements or items only having values. here in my code values in last item is null or empty.so I need to get count as 2.
Is there any built in method in c# to do the same. is there any way to check without using loops ?
LINQ can help here:
listStudent.Where(
s => !string.IsNullOrWhiteSpace(s.Name) && s.Age != null
).Count();
There is no method in framework that you are looking for. You need to create your own extended method to make this.

Xpages "filter by category name" with two fields values (Restricting a View to Multiple Categories)

I want to design a view that display only projects of selected customer and services, so I created a view that customer and service columns are categorized and a view panel for this view. If I put
document1.getDocument().getItemValueString("Customer")
it works with a restrict single category. How I can do this with tow categorized columns.
Thanks in advance
Could you implement a repeat within a repeat for this?
The first repeat would capture your first category. Then inside you have a panel and a repeat and the second repeat would further filter the datasource by the second category
You cheat. Have one column that concatenates customer and project. Grab that column in your selection and split it in code.
Then use the selected value for the one column. Lets presume your original columns are customer and project. So your new column would have the formula customer+"~"+project. You then read this values to populate a SSJS object or a variable, so you can retrieve the customers (first dropdown) and the projects (second dropdown). In a dropdown you can use the format Display|Value, so a good approach is to have the value in the format customer~project.
As said you can do that in Java or JavaScript. Since I like Java's collection framework a lot, here's the Java version:
import java.util.Set;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
import lotus.domino.Database;
import lotus.domino.NotesException;
import lotus.domino.View;
import lotus.domino.ViewEntry;
import lotus.domino.ViewEntryCollection;
public class SplitCategoryBean {
private static final String SEPARATOR = "~";
private final Map<String,Set<String>> allCategories = new TreeMap<String, Set<String>>();
public void populate(Database db, String viewName) throws NotesException {
View v = db.getView(viewName);
ViewEntryCollection vec = v.getAllEntries();
ViewEntry ve = vec.getFirstEntry();
while (ve != null) {
ViewEntry nextVE = vec.getNextEntry(ve);
this.addEntry(ve.getColumnValues().get(0).toString());
ve.recycle();
ve = nextVE;
}
vec.recycle();
v.recycle();
}
private void addEntry(String combinedCategory) {
String[] splitCategory = combinedCategory.split(SEPARATOR);
String key = splitCategory[0];
String value = splitCategory[1];
Set<String> thisCategory = (this.allCategories.containsKey(key)) ? this.allCategories.get(key): new TreeSet<String>();
thisCategory.add(value+"|"+combinedCategory);
this.allCategories.put(key, thisCategory);
}
public Set<String> getFirstCategory() {
return this.allCategories.keySet();
}
public Set<String> getSecondCategoryReadyForDropDown(String key) {
return this.allCategories.get(key);
}
}
You would configure that as a managed bean (viewScope) and in the queryOpen you call the populate method. Then you can easily bind your first selection to #{beanName.firstCategory} for the selection and e.g. #{viewScope.curCustomer} for the value. The second drop-down you use the rendered="#{viewScope.curCustomer}" so it only shows when the customer is selected. And you bind the selections to #{javascript:beanName.getSecondCategoryReadyForDropDown(viewScope.curCustomer);
Put refreshs on the change events and render the view only if you have a project selected.
Does that work for you?
Thanks guys
I created a view that the first column is categorized and its value is Customer+Service then put
document1.getDocument().getItemValueString("Customer") + document1.getDocument().getItemValueString(“Service")
in "filter by category name" of view panel it works now.

Need to have HashMap add(sum) multiple values within a key

I am attempting to have a single button process multiple inputs in the same input boxes. I have 16 input boxes, each with its own id# (YfProduct) which I am using as the key for my hashmap. For the input value, I have Weight. A user will enter in whatever double weight they want, in however many input boxes they wish, and click a button (an a4j:commandButton) which activates the method below.
private HashMap<Integer, Double> storeWeight = new HashMap<Integer, Double>();
public void storeWeight(Yieldfl yieldfl){
for (YieldItem row : yielditem) {
storeWeight.put(row.getYfProduct(), row.getWeight());
System.out.print(storeWeigt)}
}
Right now this code will set the appropriate values with the right key, and replace those values with new input entered and another button click. However what I am trying to do is have the bean save the previous values, and sum up the next values entered with the previous entry(s) that have the same key. So at the end of the user's input, the HashMap will contain 16 keys with the sum of the individual values added up for each key. I havn't been able to come up with a way to do this without some serious hardcoding. Help much appreciated.
So this actually turned out to be a simple fix, I'll put the solution on here just incase somebody runs into a similar problem. It's my first time working with a HashMap so I had a colleague help me out. I needed the value to call the key.
storeWeight.put(row.getYfProduct(), storeWeight.get(row.getYfProduct() + (row.getWeight()));
and to avoid null-pointers:
public void storeWeight(Yieldfl yieldfl){
for (YieldItem row : yielditem) {
Double oldValue = storeWeight.get(row.getYfProduct());
if (oldValue == null)
oldValue = 0.0;
storeWeight.put(row.getYfProduct(), oldValue + (row.getWeight()));
row.setWeight(0.0);
System.out.print(storeWeight);}
}

SharePoint 2010 - how to make multi-column a unique value

i'm implementing a sand box solution where is should have more than one column a unique key, i have to use the item adding event receiver but how to get the current adding item field values to know if this item is occurred within the list.
thanks
Create UniqueID column and make it unique.
Create an event receiver as follows:
public override void ItemAdding(SPItemEventProperties properties)
{
string Name = properties.AfterProperties["Name"].ToString();
string Title = properties.AfterProperties["Title"].ToString();
StringBuilder StringBuilder = new StringBuilder(Name);
StringBuilder.Append("-");
StringBuilder.Append(Title);
properties.AfterProperties["UniqueID0"] = StringBuilder.ToString();
base.ItemAdding(properties);
}

Is it possible to use custom JSF converter for UISelectOne, depending on value of Bean property or UIInputText value?

I need to get help with a problem.
I have an application, which displays data from database.
In database I am using one column for identifying an object with changes in time (version of object).
I mean that rows have different IDs, but same OBJECT_ID.
They are also differentiated by validity DATE columns (VALID_FROM and VALID_TO), so i can find the proper version of object in selected time.
When page is shown to user, he sets DATE for displaying data.
There is form on page, with Select Boxes (their items are object from database).
I need to show only items, that satisfy the validity condition.
Everything is OK before form handling.
I am using custom converter, that converts String value (ID of row in database table) to Object of my model, but I need two values for row to find. (ID and DATE from user form).
Converter method getAsObject gets only one VALUE parameter (ID from Select Box).
How to get second parametr to work?
Can i find it from Bean or from JSF input object?
If there is some way that includes using <f:attribute/>, is there way to set <f:attribute/> by Java code in Bean? (Form is generated by bean method).
I am sorry for my English, i try my best :o)
P.S.: Date parameter is important in sqlMap because of JOIN used. I search by ID, but other tables are joined by OBJECT_ID (version) => and i need to get one row only
Thanks for answers and have a nice day
Worsik
Edit:
Select Boxes are generated by this code:
uiInput = new HtmlSelectOneMenu();
UISelectItems items = new UISelectItems();
items.setValueExpression("value", createValueExpression(
"#{myBean.referencedList." + item.getName() + "}",
List.class));
uiInput.getChildren().add(items);
Converter method looks like this:
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
if (value.equals("")) {
return null;
}
Class entityType = component.getValueExpression("value").getType(
FacesContext.getCurrentInstance().getELContext());
// TODO: need to get date value from form
Date day = new Date();
return myService(context).findEntityById(entityType, Long.valueOf(value), day);
}
entityType is Class of model for object from database
myService calls Dao object and sqlMap is called
select * from object_table as t1
JOIN other_table as t2 ON t1.object_fk = t2.object_id // (or version_id)
where t1.id = #id:INTEGER#
and t2.valid_from <= #day:DATE#
and t2.valid_to >= #day:DATE#
I figured out how to do this by <f:attribute/> by code in converter:
Date day = (Date) component.getAttributes().get("dateForSearch");
and in bean method after generating SelectBox with code:
uiInput.getAttributes().put("dateForSearch", getSelectedDate());
and in pages without dynamic generating of forms i used:
<SelectOneMenu>
...
<f:attribute name="dateForSearch" value="#{myBean.selectedDate}" />
</SelectOneMenu>
I hope that this will not be only self answered question and will help somebody else

Resources