How can I add object to the page dynamically? - android

I try to add my objects to the Page1 dynamically. I know how I can create them but the problem is adding them to the page.
Here is my code sample:
var createButton = new SMF.UI.Image();
createButton.image = "answer.png";
createButton.top = 5;
createButton.width = 38;
createButton.height = 38;
createButton.left = 5;
createButton.imageFillType = SMF.UI.ImageFillType.aspectFit;
createButton.onShow = function(e) {
alert("object created");
};

It is very simple. Like other programming language just use add method. You have to give a parameter which is your obect. Take a look below.
Pages.Page1.add(createButton);

Related

to create variables, loops in kotlin?

I create app for Android on Kotlin,I need to create 55 variables to work with them in code,how to do it using a for loop?
Variables should be look like this:
val EditText0: EditText = findViewById(R. id.et0)
val EditText1: EditText = findViewById(R. id.et1)
and so on
You should use getIdentifier()
for(int i=0; i<some_value; i++) {
for(int j=0; j<some_other_value; j++) {
String buttonID = "btn" + i + "-" + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = ((Button) findViewById(resID));
buttons[i][j].setOnClickListener(this);
}
}
This is Java but same logic for kotlin.
When you have many of a similar kind of variable like this, you really should have a single List variable for all of them.
If they have consistent names like this, you can generate the IDs using resources.getIdentifier. It's easier using a List constructor than a for loop.
// In an Activity:
val myEditTexts: List<EditText> = List(55) { index ->
val id = resources.getIdentifier("et$index", "id", packageName)
findViewById<EditText>(id)
}
// In a Fragment's onViewCreated function:
val myEditTexts: List<EditText> = with(requireContext()) {
List(55) { index ->
val id = resources.getIdentifier("et$index", "id", packageName)
view.findViewById<EditText>(id)
}
}
If you don't want to keep all of them in a single list, then you should use View Binding to let the properties be generated for you.

how to List double values from dynamically added EditTexts android

I want to create a list of the double values. screen shot of my layout
When the (+) add button is pressed a minimum of three edit boxes are added.
i want to get the values of these edit boxes and be able to cross multiply them.
the values will be mostly +/- and with decimals.
How can i identify the edit boxes, then from the input Values, i set them in a List where i can be able to cross Multiply them.
I am trying this but i don't understand where am going wrong.
List<EditText> allNs = new ArrayList<EditText>();
List<EditText> allEs = new ArrayList<EditText>();
String[] northings = new String[allNs.size()];
String[] eastings = new String[allEs.size()];
double inputNorths, inputEasts = 0;
for(int i=0; i<allNs.size(); i++){
northings[i] = allNs.get(i).getText().toString();
inputNorths = Double.parseDouble(northings[i]);
northValues [1] = inputNorths;
}
resultN.add(northValues);
for(int e=0; e<allEs.size(); e++){
eastings[e] = allEs.get(e).getText().toString();
inputEasts = Double.parseDouble(eastings[e]);
eastValues[2] = inputEasts;
}
resultsE.add(eastValues);
calcArea();
To be honest I don't understand what are you doing here:
inputNorths = Double.parseDouble(northings[i]);
....
inputEasts = Double.parseDouble(eastings[e]);
Every loop you overwrite these variables. Maybe you forgot about adding?
inputNorths += Double.parseDouble(northings[i]);
northValues [1] += inputNorths;

Tasker variable editing add commas

I need to edit the string value in variable.
So,
00343755932
should be converted to:
0,0,3,4,3,7,5,5,9,3,2
because I must define each number as an variable array for readable one by one.
if I'm right you are trying to create an array from string. Use following code
String val = "00343755932";
int[] numberArray = new int[val.length()];
Matcher match = Pattern.compile("[0-9]").matcher(val);
int i = 0;
while(match.find()) {
System.out.println(match.group());
numberArray[i] = Integer.parseInt(match.group());
i++;
}

Edit text of several buttons using a for loop

I have 16 buttons, whose names are "button1", "button2", and so on. Is there a way I can iterate through them using a for loop, by somehow appending the number value upon each iteration? Something like this:
for(int i = 1; i<17; i++ ){
Button b = (Button)findViewById(R.id.buttoni);
I know I can simply initialize each button in my onCreate() method, but I was just curious if I could do it in a way similar to my example code.
Thank you.
You can use getIdentifier :
for(int i = 1; i<17; i++ ){
int buttonId = getResources().getIdentifier("button"+i, "id", getPackageName());
Button b = (Button)findViewById(buttonId);
//Your stuff with the button
}
You can create an array of Button's and use getIdentifier method that allows you to get an identifier by its name.
final int number = 17;
final Button[] buttons = new Button[number];
final Resources resources = getResources();
for (int i = 0; i < number; i++) {
final String name = "btn" + (i + 1);
final int id = resources.getIdentifier(name, "id", getPackageName());
buttons[i] = (Button) findViewById(id);
}
In case someone is interested how to achive the same result using Java only
The solution above uses Android specific methods (such as getResources, getIdentifier) and can not be used in usual Java, but we can use a reflection and write a method that works like a getIdentifier:
public static int getIdByName(final String name) {
try {
final Field field = R.id.class.getDeclaredField(name);
field.setAccessible(true);
return field.getInt(null);
} catch (Exception ignore) {
return -1;
}
}
And then:
final Button[] buttons = new Button[17];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = (Button) findViewById(getIdByName("btn" + (i + 1)));
}
NOTE:
Instead of optimizing this kind of code you should rethink your layout. If you have 17 buttons on the screen, a ListView is probably the better solution. You can access the items via index and handle onClick events just like with the buttons.

AndEngine - can't iterate through array items inside handler calls

I am having a problem working inside a handler call. The problem is something as follows:
Sprite sprite = new Sprite[spriteArrayLength];
IUpdateHandler mm[i] = new ........// you know what
for(int i = 0; i < spriteArrayLength; i++){
//many other actions
mm[i] = new IUpdateHandler() {
//do somthing with sprite array items
float anyVar = sprite[i].getX();//problem rises here
};
sprite[i].registerUpdateHandler(mm[i]);
}
Every time it shows an error saying i reaches out of array bound. That means the handler call executes after for loop ends and so i already crosses its limit. How can I do something like above in a proper way?
Edit:
Sorry for my previous mistake. The First line of the code will be:
Sprite sprite[] = new Sprite[spriteArrayLength];
IUpdateHandler mm[] = new ........// you know what
not this:
Sprite sprite = new Sprite[spriteArrayLength];
IUpdateHandler mm[i] = new ........// you know what
I just thought these line are not very important to mention that's why made the mistake. But still the problem remains same.
Edit-2:
I get "array out of bound" type run time error. Let's say, array size is 6. So, last element is 5. But inside UpdateHandler "i" begins with 6 and throws an error. And I tried making "i" final, even made it global by declaring as a class field. I am trying to write short code examples here because it contains many codes. Better version is as follows:
public int i;//global declaration
//inside some method:
Sprite sprite[] = new Sprite[spriteArrayLength];
IUpdateHandler mm[] = new IUpdateHandler[spriteArrayLength];
for(i = 0; i < spriteArrayLength; i++){
//many other actions
mm[i] = new IUpdateHandler() {
//do somthing with sprite array items
float anyVar = sprite[i].getX();//problem rises here
};
sprite[i].registerUpdateHandler(mm[i]);
}
Based on the code in your Edit-2, my suggestion is:
Sprite sprite[] = new Sprite[spriteArrayLength];
IUpdateHandler mm[] = new IUpdateHandler[spriteArrayLength];
for(i = 0; i < spriteArrayLength; i++){
//many other actions
mm[i] = new IUpdateHandler() {
private final int id = i; /* add this line */
// call Log.i() here is compiling error
#Override
public void onUpdate(float pSecondElapsed) {
//do somthing with sprite array items
/* And access id, instead of i in below code.
Here, I assume this statement is located
within onUpdate() or reset().
*/
float anyVar = sprite[id].getX();
}
#Override
public void reset() {
}
};
sprite[i].registerUpdateHandler(mm[i]);
}
You are using the index variable i in 2 different contexts.
In your code you do this before the loop using i as an array index:
IUpdateHandler mm[i] = new ........// you know what
then inside the loop, you are changing the value of i and referencing mm[i] and sprite[i].
I assume that you haven't correctly initialized the mm array to the correct size (spriteArrayLength)
The problem is that your update handler holds a reference to variable i but isn't evaluated until after the loop finishes. Instead of holding a reference to i, give it a reference to the specific sprite, and for readability make it final to avoid any doubt that it could change:
for(i = 0; i < spriteArrayLength; i++){
final Sprite currentSprite = sprite[i];
//many other actions
mm[i] = new IUpdateHandler() {
//do somthing with sprite array items
float anyVar = currentSprite.getX();
};
currentSprite.registerUpdateHandler(mm[i]);
}

Categories

Resources