How to store checkbox options to use in other classes? - java

Im using a method in my FXMLController to check if radio buttons are selected and storing them in a bollean array. I then will be calling this method in another class to get the boolean values to see which are selected to perform perfrom different actions using if statements accordingly.
public boolean[] GetSelectedOption() {
boolean[] OptionsValue = new boolean[2];
OptionsValue[0] = StripHtmlOption.isSelected();
OptionsValue[1] = StripHtmlOption.isSelected();
return OptionsValue;
}
Is this the right way of storing checkbox/radiobutton options? Is there a better way of doing this? As I can imagine this isn't very object orientated? Im a java beginner.
Here is the code where im trying to call the options/boolean values in another class. This code doesnt work though.
public String StripHtml(String html, String DomainName) {
FXMLController FetchOptions = new FXMLController();
boolean[] OptionsValue = FetchOptions.GetSelectedOption();
OptionsValue[0] = StripHtmlvalue;
OptionsValue[1] = = StripLinkValue;
if (Boolean.TRUE.equals(StripHtmlvalue)) {
System.out.println("Some code will go here");
} else {
}
return Stringyettobemade;
}

OptionsValue[0] = StripHtmlvalue;
OptionsValue[1] = = StripLinkValue;
At first please use lower case variable names.
At seccond why do you overwrite the values?
I think what you want is to GET the values so the right code will be:
StripHtmlvalue = optionValue[0];
and what should this do : = =?

This should work
boolean[] optionsValue ={stripHtmlvalue.isSelected(),stripLinkValue.isSelected()};

Related

Add a missing record into the arraylist if it is not in it

Pardon me as I'm quite a beginner in coding. I have tried researching for ways to add some missing record into the lists but still can't seem to fit it correctly into my code.
I have two ArrayLists with different resultsets. Say, the first one is derived in other method and stored in abcList. This list is then used in my current fixChartStats method as a param.
In my code, I will check for the corresponding record in abcList with the second list I derive from the hql query in fixChartStats method.
If the record corresponds, then I'll do the necessary action as shown below to update the ApprovedCount number etc, else i set it to 0.
How do I go about adding the records that are missing in second list I got into the first arraylist (i.e. abcList)? Can anyone here shed some light? Do let me know if my questions are unclear. Thanks in advance, guys!
private void fixChartStats(List<TAbcModel> abcList, Map<String, Object> param, List<IssueModel> issueList, List<DestModel> destList) throws Exception {
//initialize the hql query
//translate all fields from Object[] into individual variable
firstRow = true;
for (TAbcModel abc : abcList) {
if (abc.getId().getAbcYear() = abcYear &&
abc.getId().getAbcMonthId() = abcMonthId &&
abc.getId().getAbcApplAccnId().getAccnId().equalsIgnoreCase(abcApplAccnId.getAccnId()) {
if (firstRow) {
abc.setApprovedCount(abcApprovedCount);
abc.setCancelledCount(abcCancelledCount);
firstRow = false;
} else {
abc.setApprovedCount(0);
abc.setCancelledCount(0);
}
}else{
// How to do the necessary here
// Below is what I've tried
abcList.add(abc);
}
}
}
When I debug, I noticed that it was added into the list. But soon after it was added, ConcurrentModificationException was thrown.
Create a local list and add missing records to it then add all elements from the local list to the abcList
List<TAbcModel> temp = new ArrayList<>();
in your loop:
} else {
temp.add(abc);
}
after loop
abcList.addAll(temp);

Android: Identical lines of code not working in seperate methods

If I happen to use a few lines of code in the same method as the construction of the called variables, it works, but if I would create another method that would just do the same operation, copy-pasted and then just call the other method inside it, it wouldn't work.
private void initElements(){
TextView ageValue[] =
{
(TextView)this.findViewById(R.id.age1Value),
(TextView)this.findViewById(R.id.age2Value),
(TextView)this.findViewById(R.id.age3Value)
};
TextView ageText[] =
{
(TextView)this.findViewById(R.id.age1Text),
(TextView)this.findViewById(R.id.age2Text),
(TextView)this.findViewById(R.id.age3Text)
};
for(int i = 0; i<=2;i++){
intValues[i] = a.getHours((i));
stringValues[i] = String.valueOf(intValues[i]);
ageValue[i].setText(stringValues[i]);
}
//updateValues();
}
private void updateValues(){
for(int i = 0; i<=2;i++){
intValues[i] = a.getHours((i));
stringValues[i] = String.valueOf(intValues[i]);
ageValue[i].setText(stringValues[i]);
}
}
If I would uncomment the updateValues() function, the program wouldn't run, even though the same code would be executed in the function before. Moreover, debugging would lead after this statement:
ageValue[i].setText(stringValues[i]);
to this error
I've tried rebuilding and cleaning the program and reinstalling it. I tried it on a virtual device and my real smartphone. Same error over and over. (Moreover, it sometimes says that "Source code does not match the bytecode" when I hover on the error in the picture, but sometimes it also doesn't." Also tried the "this." keyword everywhere, but no success.
I am just baffled how this could happen. Maybe the method does not get some sort of right memory reference because it is not directly next to the declaration but I mean the two statements before the "error statement" work perfectly fine, despite them having the same nature. It would be great if you could like enlighten me why this does not work, thanks!
TextView ageValue[] = new TextView[3];
TextView ageText[] = new TextView[3];
int intValues[] = new int[3];
String stringValues[] = new String[3];
CalculateDates a = new CalculateDates();
This is the declaration of the used variables. (That's in the beginning of the class)
You need to use the global ageValue[] and ageText[] for that class.
private void initElements(){
ageValue[0] =(TextView)this.findViewById(R.id.age1Value);
ageValue[1] =(TextView)this.findViewById(R.id.age2Value);
ageValue[2] =(TextView)this.findViewById(R.id.age3Value);
ageText[0] = (TextView)this.findViewById(R.id.age1Text);
ageText[1] = (TextView)this.findViewById(R.id.age2Text);
ageText[2] = (TextView)this.findViewById(R.id.age3Text);
for(int i = 0; i<=2;i++){
intValues[i] = a.getHours((i));
stringValues[i] = String.valueOf(intValues[i]);
ageValue[i].setText(stringValues[i]);
}
Do not declare the TextViews inside the method also.
You were creating different instances of those textViews in initElements() method and the global TextViews were still null

Processing: manipulate variables by string names

I am importing a .txt file to use to run parts of my program. A certain part needs to manipulate boolean variables. However, I am using loadStrings() to interpret the text into Strings, with the individual lines called lines[]. I have a variable's name as a String and must reference the variable itself. Unfortunately, I have been unable to figure out how to do this.
boolean choice1 = false;
// lines[counter+2] is "choice1"
if (lines[counter+2] = false) {
println("statement is false");
counter += 4;
}
Obviously, the above statement doesn't work, nor does:
if (boolean(lines[counter+2]) = false) {
as it errors.
Also,
if (boolean(lines[counter+2]) == false) {
is not a possible solution as any word other than "true" when used in boolean() is false, so the above gives a false positive (pardon the pun).
If there is a solution to the same problem in java coding, I would be happy to see that as well.
P.S. If you have a solution, is there a way to not just reference the variable by its name, but also to change it? For example:
boolean(lines[counter+2]) = false;
or
lines[counter+2] = false;
obviously, the above are incorrect, but that idea.
You can't do that.
You could use a HashMap of String values to Boolean values though:
HashMap<String, Boolean> variableMap = new HashMap<String, Boolean>();
variableMap.put("myVariable", true); //set the value
boolean check = variableMap.get("myVariable"); //get the value
println(check);
variableMap.put("myVariable", false); //change the value
boolean checkAgain = variableMap.get("myVariable"); //get the new value
println(checkAgain);
More info can be found in the Processing reference.

Java Iterate through a collection of classes and using their functions

i wanted to know how i can iterate through a collection of classes and be able to use the classes functions on every pass.
Im new to java and im much more familier with c++.
This is the collection i wish to iterate..
private ArrayList<Album> albumCollection;
and activate the following function
get title
This is my current code...
//Lists all stored titles
private void ListAllTitles(){
int size = albumCollection.size();
for(int i=0; i < size; i++){
System.out.println(albumCollection(i).getTitle());
}
}
In order to access an element from a List, you need to use the get method:
System.out.println(albumCollection.get(i).getTitle());
Also note, you could use the for each loop to achieve this:
for (Album album : albumCollection) {
System.out.println(album.getTitle());
}
This for each construct is simpler:
for (Album album : albumCollection)
{
System.out.println(album.getTitle());
}

How to get a string from strings.xml by adding int to the name

private int info = 1;
public void nextStep(View view)
{
TextView textInfo = (TextView) findViewById(R.id.textInfo);
textInfo.setText(R.string.info1);
info++;
}
When one button is clicked, method nextStep is called. And every time a button clicked, I want to show different info, first time it's info1 string, next time it's info2 string and etc. from strings.xml. I would like to do something like that:
private int info = 1;
public void nextStep(View view)
{
TextView textInfo = (TextView) findViewById(R.id.textInfo);
textInfo.setText(R.string.info + info);
info++;
}
Of course, it's not possible. What should I do? I really don't want to write a big if/else or switch statement. Thanks.
Of course, it's not possible.
Actually, it is. You can use getIdentifier to do this:
private int info = 1;
public void nextStep(View view)
{
TextView textInfo = (TextView) findViewById(R.id.textInfo);
int myStrId = textInfo.getContext().getResources().getIdentifier("info"+info, "string", textInfo.getContext().getPackageName());
textInfo.setText(myStrId);
info++;
}
getIdentifer() is a way to fetch resource IDs that are stored in R if you don't know the exact name. While it isn't the most efficient method in the world, it suffices in situations where referencing R (such as your situation) is not possible.
The method returns the same ID that R would; that is, getIdentifier("info1", "id", ...); is the same as R.id.info1, since R is just a compiled version of it. This method also works in the event that you are unsure if an ID exists (such as from an external library) but need to reference it anyway.
Use an array of String or better a List<String> such as an ArrayList<String> and fill it with the Strings from the XML. Then you can use the get(int index) method to get the ith String in the list.
If you are using JAXB to unmarshall your XML, you can have it set up to create your List for you without much fuss.

Categories

Resources