I have been assign to one struts2 project and its one of jsp contains more than 100 radio buttons and they have handled in statically not dynamically. As jsp contains 100 radio buttons so I am able to see the below list of radio buttons catches in actions with their getter and setter
List selectRadioList001
List selectRadioList002
List selectRadioList003
List selectRadioList004
etc
List selectRadioList100
I want to add these radio button in a list dynamically iterating through 1 to 100 something like below but when I try to access the variable like "searchBoxSelectRadioList"+i then it is pretending like a simple string. I want it to be like a List as shown above.
public class SelectRadioListPOJO {
private List<TicketDesignUtil> selectRadioList;
public List<TicketDesignUtil> getSelectRadioList() {
return selectRadioList;
}
public void setSelectRadioList(List<TicketDesignUtil> selectRadioList) {
this.selectRadioList = selectRadioList;
}
}
Action code:
List<SelectRadioListPOJO> selectRadioListPOJOList = new ArrayList<>();
SelectRadioListPOJO selectRadioListPOJO;
for (int i = 1; i <= 100; i++) {
selectRadioListPOJO = new SelectRadioListPOJO();
selectRadioListPOJO.setSelectRadioList("searchBoxSelectRadioList"+i);// ERROR
selectRadioListPOJOList.add(selectRadioListPOJO);
}
It's not clear what you're asking.
You can't pass arbitrary values to methods; setSelectRadioList takes a list of TicketDesignUtil.
If your action doesn't have getters and setters for all of those radio buttons then you should resort to accessing the request parameters directly, for example, via ParameterAware.
You would then access the radio button parameters by name from the injected parameter map.
Notes:
It's not "pretending" to be a simple string, it is a simple string, because... well, because it is.
Your for loop is wrong; I corrected it in your question to avoid others commenting on it. The POJO should be added to the POJOList inside the loop.
Naming is funky; just call it selectRadioListPojos. Better yet, name it something domain-specific: variables should be semantically meaningful, not just a description of the class(es) involved.
These shouldn't be static in the first place, but a map or array.
Related
I created a grid that has four columns. The first column shows a name, the other three columns represent different roles. Each of those three columns is filled with check boxes in order to assign a specific role to a specific name. That's as far as i have come so far.
In each column and in each row there should only be one selected checkbox allowed. So in total i do have exactly one selection per checkbox column. How do i implement this?
Edit: I realize I might have misunderstood the question entirely. If you want to have 3 columns, with each having multiple CheckBoxes where only 1 can be selected per column, then you should use a RadioButtonGroup in each column and bind each to a different Enum field of your griditem class.
Instead of showing how to do three columns with a CheckBox in each, while only one CheckBox can be selected, I will show a different way to achieve the same information about the item.
The reason for this is the solution that what you want to achieve is not easily doable, since each CheckBox is defined in a scope that does not know the other Checkboxes of the same item. Therefore you would need to implement your only-one-selected rule inside the itemclass' setters, which is not optimal. I mean, it is possible, but I'd rather change the structure to something more apt. Usually you don't want to put this kind of business logic into your bean classes.
How would I solve the problem at hand?
Create a new Enum, which will replace all 3 boolean fields in your item class. Now in your grid you will only need one column for a ComboBox to select the enum.
I chose an Enum because this matches your needs here perfectly. With an Enum, you may have several options, but you can select only one (or none).
To better show what I mean, let's use an example Class for the Grid items, Foo. Your version has 3 booleans which your three grid-CheckBoxes are bound to. Let's call them isA, isB, isC.
// your version of the griditem class
public class Foo {
private boolean isA, isB, isC = false;
// constructor, getters, setters
}
// how the columns are added in the grid (approximately) (without editor):
Grid<Foo> grid = new Grid<>();
grid.addComponentColumn((item) -> {
CheckBox checkBox = new CheckBox();
checkBox.setValue(item.isA());
checkBox.addValueChangeListener(event -> item.setA(event.getValue()); // inside setA() method you need to set isB and isC to false if the new value is true. No good!
return checkBox;
});
grid.addComponentColumn((item) -> {
CheckBox checkBox = new CheckBox();
checkBox.setValue(item.isB());
checkBox.addValueChangeListener(event -> item.setB(event.getValue()); // inside setB() method you need to set isB and isC to false if the new value is true. No good!
return checkBox;
});
grid.addComponentColumn((item) -> {
CheckBox checkBox = new CheckBox();
checkBox.setValue(item.isC());
checkBox.addValueChangeListener(event -> item.setC(event.getValue()); // inside setC() method you need to set isB and isA to false if the new value is true. No good!
return checkBox;
});
And here is how it would look after my changes
public class Foo {
private AbcEnum abcEnum = null;
// constructor, getters, setters
}
public Enum AbcEnum {
A,
B,
C;
}
// how the columns are added (without editor):
Grid<Foo> grid = new Grid<>();
grid.addComponentColumn((item) -> {
ComboBox<AbcEnum> comboBox = new ComboBox<>();
comboBox.setValue(item.getAbcEnum());
comboBox.addValueChangeListener(event -> item.setAbcEnum(item.getValue()));
return comboBox;
});
I wrote "without editor" in the comments about adding the column, because this code will add the ComboBox/CheckBox as clickable and functional components for each grid item, without needing to open the editor for an item to change the values. If you are indeed using an editor, you can add these functional inputs as editorComponents (and bind them to the editor-binder instead of using setValue and addValueChangeListener), and show only the current value in the normal columns (not editable - therefore no inputs like CheckBox or ComboBox are required)
I'm dynamically generating a form based on data received from an RPC call into a FormFieldData object which has details about the field to be rendered such as, Field Name, expected length and type of input, if the field is a required field or not and valid input Regex in some cases etc.
I'd like to be able to perform validation on the field depending on above attributes.
Here's an example:
private void renderTextField(FormFieldData field){
FormGroup formGroup = new FormGroup();
FormLabel formLabel = new FormLabel();
if(field.isRequired()){
formLabel.setText(field.getName()+"*");
}else{
formLabel.setText(field.getName());
}
formGroup.add(formLabel);
TextBox textBox = new TextBox();
textBox.addChangeHandler(new ChangeHandler(){
#Overrride
public void onChange(ChangeEvent event){
//TODO - find a way to get the text entered in TextBox
// and perform validation on it
//and set the TextBox Style to "Validation-error"
}
});
formGroup.add(textBox);
form.add(formGroup);
}
There're similar methods to render dropdowns, Numeric fields, radio button fields etc. which would need similar validation.
The problem is I can't access the text from the TextBox inside the onChange method without declaring it final, which I can't do because I might be rendering multiple text fields. I don't know much about ChangeEvent and if there's a way to get the text from the that.
I'd really appreciate any pointers to a way to do this in real time as the data is entered into the form, other than having to iterate through the fields and their corresponding FormFieldData object when the form is submitted.
First off, you can make the variable final, no problem.
If you don't want to do that for whatever reason, you can get the TextBox from the event like this:
textBox.addValueChangeHandler(new ValueChangeHandler(){
#Overrride
public void onValueChange(ChangeEvent event){
TextBox box = (TextBox) event.getSource();
// Do whatever you need to here
}
});
You are probably also looking for ValueChangeHandler instead of ChangeHandler.
i need to fill an Array with different data types
InvoiceItem[] invoiceItems;
int test = 3;
int i = 0;
This needs to be in the Array:
InvoiceItem invoiceItem = new InvoiceItem();
invoiceItem.setItemType("TestItem");
invoiceItem.setArticleNo("TestItemID");
invoiceItem.setDescription("TestDescription");
invoiceItem.setQty(1);
invoiceItem.setPrice(new BigDecimal(20.00));
invoiceItem.setVat(new BigDecimal(5.0));
There is the possibility that there is more than one InvoiceItem (test=3), so it needs to be in a loop.
It has to be an Array, i need to pass it to another class which only accepts an Arrays.
How can i achieve this?
Edit: I will try to make my question more clear:
I need to know how to put these
invoiceItem.setItemType("TestItem");
invoiceItem.setArticleNo("TestItemID");
invoiceItem.setDescription("TestDescription");
invoiceItem.setQty(1);
invoiceItem.setPrice(new BigDecimal(20.00));
invoiceItem.setVat(new BigDecimal(5.0));
in an Array:
int countofInvoiceItem = 3; // there are 3 InvoiceItem
InvoiceItem[] invoiceItems = new InvoiceItem[countofInvoiceItem];
Where there can be more than one InvoiceItem.
Method looks like this:
public final ResponseCreateInvoice CreateInvoice
(Invoice Invoice, InvoiceItem[] InvoiceItems, Address DeliveryAddress, Address InvoiceAddress, String UserID, String Password)
(This is given and i can not change)
and returns
ResponseCreateInvoice inv = wsClient.createInvoice(invoice, invoiceItems, deliveryAddress, invoiceAddress, userID, password);
i am sort of new to Java (or arrays), so this may be an easy question, but i don't really get it. Also does it matter that there are Strings and Int, BigDecimal etc mixed together in an Array?
You just need to declare your array as an array of type T where T is a superclass of all the classes of the objects you want to fill it with. In the worst case, it would be Object but it's bad design 9 times out of 10.
I would recommend you to make a class that holds everything you need as follows:
public class YourClass{
int id;
double value;
String description;
//and so on
//create getters and setters
}
And you can use this class to pass array of objects to another class.
Put your objects of the class in the Array
For example
YourClass[] objects = new YourClass[SIZE];//define number of objects you need
And you can pass each and every objects separately or as a whole to another class.
And in your receiving class, you can have a constructor as:
public YourRecievingClass(YourClass[] object){
//and recieve here as you need; ask further if you need help here too
}
I think this is the best way to adopt though your question is not 100% clear
Based on your edit, your original question is off base. You do not want to create an array of different types but instead only want to create an array of one type and one type only, that being an array of InvoiceItems. You are confusing object properties with array items, and they are not one and the same. This code here:
invoiceItem.setItemType("TestItem");
invoiceItem.setArticleNo("TestItemID");
invoiceItem.setDescription("TestDescription");
invoiceItem.setQty(1);
invoiceItem.setPrice(new BigDecimal(20.00));
invoiceItem.setVat(new BigDecimal(5.0));
is where you are changing the properties of a single InvoiceItem.
It seems that your InvoiceItem class has String fields for item type, for article number, for description, an int field for quantity, a BigDecimal field for price and a BigDecimal field for VAT. And so your array would look simply like:
InvoiceItem[] invoiceItems = new InvoiceItem[ITEM_COUNT]; // where ITEM_COUNT is 3
You could use a for loop to then create your items:
for (int i = 0; i < invoiceItems.length; i++) {
invoiceItems[i] = new InvoiceItem();
}
And you could perhaps use the same for loop to fill in the properties of each InvoiceItem in the array:
for (int i = 0; i < invoiceItems.length; i++) {
invoiceItems[i] = new InvoiceItem();
invoiceItems[i].setItemType(???);
invoiceItems[i].setArticleNo(???);
invoiceItems[i].setDescription(???);
invoiceItems[i].setQty(???);
invoiceItems[i].setPrice(???);
invoiceItems[i].setVat(???);
}
But the unanswered question is, ... where do you get the data for each property of each InvoiceItem in the array? Is this information contained in a file? Is it inputted by the user? That is something you still need to tell us.
With which types of data? In general, you could use:
Object[] myArray;
All classes are subclasses of Object.
I need to have 2 inputs in my form, one for date and one for time. In my model it is just one property of type java.util.Date. What is the best practice to handle generating the html and binding the input fields to the date property in the model using Play framework 2?
Note, if I use field constructors, I can't lay out the form the way I need to. I want a label on the first line, the 2 inputs on the second line, and validation errors on the third line. Should I just use raw html instead? If I do, will I still have access to validation errors and constraints?
It'd be certainly easier to bind if you were using two separate fields in your model. One idea would be to create an intermediate class which binds to the form submission.
// Controller
public static class FormSubmission {
public Date date;
public Date time;
}
public static Result submitForm() {
Form<FormSubmission> filledForm = form(FormSubmission.class).bindFromRequest();
if (filledForm.hasErrors()) {
return badRequest();
} else {
ModelClass model = new ModelClass(); // fetch first if you update
// Copy all values from form submission to the model
model.dateAndTime = combineDateAndTime(filledForm.get().date, filledForm.get().time);
}
return ok();
}
// View
#(form: Form[FormSubmission])
...
(I know this doesn't help, but tasks like this are extremely trivial in Scala.)
In my android application I want to be able to add a string value to a static arraylist I have declared on my main android activity. The logic goes like this:
1) When you click a button and an activity starts. On the oncreate method I want to save the name of the class that is the current activity to a string value. For example:
String className = "com.lunchlist.demo";
After this value is assigned I want to immediately add this string value to a Static ArrayList I have declared in my main android activity (meaning first android activity that starts) After adding the value
I did something like this:
static String List<String> members = new ArrayList<String>();
This is declared in my main activity. Now when I click a button to start another activity I use this to add the string classname for that current activity to my arraylist in my oncreate method:
String className = "com.lunchlist.demo"
members.add(className);
My question is now, would this add the string value to my arraylist and save it for later use? For example If I click three different buttons this will add three different className values to the arraylist. Would this then store a string value that would hold three different string values for my members arraylist? How would I check each item in my arraylist to see if the values are being added when a new activity is started?
I'm asking this because I will need to retrieve this and store these values using shared preferences and later retrieve them and starting an intent using the string value which is the class to start the activity. I got the activity to start with a string value of a class name I'm just having trouble storing them.
Answering to all of your questions:
would this add the string value to my arraylist and save it for later
use?
Yes. Your code seems perfect to do it with no problems.
For example If I click three different buttons this will add three
different className values to the arraylist. Would this then store a
string value that would hold three different string values for my
members arraylist?
If you tell to your button's onClickListener to add a string to the members ArrayList then it will be done and no matter if you already had previously added that member to the ArrayList because array lists don't care if there is duplicated data or not.
How would I check each item in my arraylist to see if the values are
being added when a new activity is started?
You have to iterate your array list with a for or a for-each cicle and then print that member name as a log entry.
For-each cicle
for (String member : members){
Log.i("Member name: ", member);
}
Simple For cicle
int listSize = members.size();
for (int i = 0; i<listSize; i++){
Log.i("Member name: ", members.get(i));
}
If you try to print/ log a value which index is out of range, i.e., i < 0 || i >= listSize then a IndexOutOfBoundsException will be thrown and crash your app.
Iterate using For-Each introduced in Java from Java 1.5 :
for (String s : members){
Log.d("My array list content: ", s);
}
See this link for further details:
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
Try this:)
We can implement by these two way
foreach
for loop
String type arraylist
ArrayList<String> limits = new ArrayList<String>(); // String arrayList
Using foreach
for (String str_Agil : limits) // using foreach
{
Log.e("Agil_Limits - " , str_Agil);
}
Using forloop
for(int agil=0; agil<=limits.size(); agil++) // using for loop
{
Log.e("Agil_Limits - " , limits.get(agil).toString());
}