How to use Indexes access operator (get & set) for Arrays? - android

I'm trying to use the Indexed access operator as it explained in the following link:
Indexed access operator
It is written there that it works exactly as set & get but code isn't compiled when trying for example to compile the following:
var vv : Array<Int> = Array(6 ,{ 5*it })
vv[1, 4] =5
that is exactly like the pattern in the link:
a[i, j] = b a.set(i, j, b)
After reading comments I understand that a[i,j] means setting a value to two dimensional array, but still the following code doesn't work:
val rows = 3
val cols = 4
var arr = Array(rows) { IntArray(cols) }
arr[2,3] = 5

Related

Create list/array of Textviews to change .text via index

So I was hoping to make a list/array of textviews so that I can iterate a loop and set the .text value of the TextViews as I go. Otherwise I would have to set the values in the code statically which would be a whole lot messier and potentially not even feasible for my needs.
So in the code below the idea would be to iterate the loop and when the correct value is confirmed that [index] would then set the corresponding
var refillToken : Double = (0).toDouble()
var tweetStored : BooleanArray = BooleanArray(20)
var tweetActive : BooleanArray = BooleanArray(20)
var userID: MutableList<String> = mutableListOf("")
var textViewToken = 0
while (refillToken > 0) {
var token: Int = 0
while (token < (tweetStored.size)) {
if (tweetStored[token] == true) {
tweetActive[token] = true
textView[textViewToken].text = userID[token]
textViewToken++
refillToken--
token++
if (refillToken < 0) {
break
}
}
}
}
}
I know my loop is probably messy by sane people standards but it makes sense to me and (hopefully) isn't the issue at play. Have found a few articles or ideas searching for the past two hours but they're either 10 years old (and I think deprecated), for java or don't work for whatever reason.
You need to get a value and then add it to the textview and change this value after every action on the page.
Use variable assignment for this task

Is there a better way than using 3 different arrays to display 3 types of data

I'm making a soundboard app from an example I found, and each sound has an id, the text shown, and a picture.
The sample uses 3 arrays defined in arrays.xml to do that, an array for the text, an array for the id of the sound (#raw/example) and an array for the picture (#mipmap/ic_example).
An object is created with the value of the 3 arrays :
val res = context.applicationContext.resources
val labels = res.obtainTypedArray(R.array.labels)
val ids = res.obtainTypedArray(R.array.ids)
val pictureIds = res.obtainTypedArray(R.array.pictureIds)
val sounds = ArrayList<Sound>()
for (i in 0 until labels.length()) {
sounds.add(
Sound(
labels.getString(i),
ids.getResourceId(i, -1),
pictureIds.getResourceId(i, -1)
)
)
}
I started to fill the arrays but it started to be difficult to link the values
Is there a way to regoup those 3 arrays into one ?

How to append 2 strings in Kotlin?

I am trying to concatenate 2 String but not sure how to go about it.
this is my code:
val word = R.string.word
and i'm trying to append it with "$currentPage/5" inside the setText("$currentPage/5")
i tried to make it in this way setText("$word $currentPage/5")
and this way setText("${R.string.value} $currentPage/5")
and it did not work , it only shows me numbers not the text
try to use this:
val word = getString(R.string.word)
text_view.text = "$word $currentPage/5"
If you want to edit your value (e.g. current page) wrap it with {}
E.g.
val word = getString(R.string.word)
text_view.text = "$word ${currentPage/5}"
Remember to use proper kotlin syntax
In Kotlin, the concatenation of string can be done by **interpolation/templates**.
val a = "Its"
val b = "Kotlin!"
val c = "$a $b"
The output will be Its Kotlin!
Or we can alson do concatenate using the **+ / plus() operator**:
val a = "String"
val b = "Concatenate"
val c = a + b
val d =a.plus(b)
print(c)
The output will be: StringConcatenate
print(d)
The output will be: StringConcatenate
Or you can concatenate using the StringBuilder which is a normal way to do that.
To concatenate two string, we could do
val concatenatedWord = "${resources.getString(R.string.value)}:
${number/3}."
If R.string.value was "The result" and number was 15, value of concatenatedWord will be "The result: 5."
Or we could also concatenate using the + operator or using StringBuilder.
But if you do
textView.text = "${resources.getString(R.string.value)}: ${number/3}."
AS will warn "Do not concatenate text displayed with setText." so, in the case of setting concatenated text in textview, consider using
String.format("%s: %d.", resources.getString(R.string.value):
number/3)
As a future resource and answer why the accepted answer works:-
String Templates:-
Strings may contain template expressions, i.e. pieces of code that are evaluated and whose results are concatenated into the string.
How to implement these?
A template expression should start with a dollar sign ($) and consists of either a simple name:
when the expression is a simple variable.
val i = 10
println("i = $i") // prints "i = 10"
or else arbitrary expression in curly braces:
val s = "abc"
println("$s.length is ${s.length}") // prints "abc.length is 3"
Note :- Templates are supported both inside raw strings and inside escaped strings.
val nameOfAnimal = "fish"
val speciesClass = "is an Aquatic Vertebrate"
println(nameOfAnimal.plus(speciesClass))
println(nameOfAnimal+speciesClass)
println("$nameOfAnimal $speciesClass")
Results:
fishis an Aquatic Vertebrate
fishis an Aquatic Vertebrate
fish is an Aquatic Vertebrate

Is Android java getResource.getIdentfier exist in IOS Swift?

My application has so many data. So There are so many textfield. Therefore I want to manage textView in iOS swift in the same way.
area1Layer = new TextView[25];
for(int k = 0; k < layer1; k++){// 층수 SET
area1Layer[k] = (TextView)findViewById(getResources().getIdentifier("layer"+(k+1),"id","kr.soen.areacard"));
area1Layer[k].setText(Integer.toString(k + 1) + "0" +ho1);
}
I assume that you want to identify the respective UITextField/UITextView seperately. This can be done by assigning different tags to the respective textfields/textviews.
let textField1: UITextField = UITextField()
textField1.tag = 1
let textField2: UITextField = UITextField()
textField2.tag = 2
and in the UITextFieldDelegate method,
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField.tag == 1 {
//Type your code here
}
if textField.tag == 2 {
//Type your code here
}
}
Also you could assign outlets to each textfield/textview, and use the outlets to check.
#IBOutlet weak var textField1: UITextField!
#IBOutlet weak var textField2: UITextField!
and in the UITextFieldDelegate method,
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == textField1 {
//Type your code here
}
if textField == textField2 {
//Type your code here
}
}
The same can be done in the case of UITextView as well.
I believe that you want to identify each UITextField or UITextView separately and accordingly manipulate them. You can do it in the following way:
let textView1: UITextView = UITextView()
textView1.tag = 1
let textView2: UITextView = UITextView()
textView2.tag = 2
self.view.addSubview(textView1)
self.view.addSubview(textView2)
For identifying between different view objects, you can use .tag property in iOS by setting .tag in the above way.
To get different UITextView added to the self.view as a subView in swift, you can do the following:
if let textViewObject: AnyObject = self.view.viewWithTag(2) {
// first check is to identify if there is a given view with the tag
if let textView: UITextView = textViewObject as? UITextView {
}
}
The main difference is iOS uses an number based tag while Android uses a text key. You can accomplish the same thing with both. In iOS store all your labels in a collection and modify per tag. The collection type and syntax specifics for setting the tag depend on the way you wright your views.
After setting the tag via interface builder or view.tag = myTag; you can do something like:
for (int x=0; x<strings.count; x++){
UITextView *view = [self.view viewWithTag:x];
view.text = strings[x];
}

Unity3d BootCamp Tutorial UnityScript Array Order Anomaly in ImageEffectsOrder.js

I am new to Unity and grabbed the BootCamp project and ran it within Unity 4.1.5f1 as a Windows Build without any modification
I then tried to build to Android and had a bunch of errors (mostly variables not being declared)
But I have one remaining that I just don't understand...
In the following code in the file ImageEffectsOrder.js the javascript references an order method of the array sorted[] as sorted[i].order
The compiler errors with 'order' is not a member of object.
So I'm a little confused as to why the windows build supports this member but not android.
This makes me wonder what other surprises await when converting from platform to platform.
But for now can anyone point me to a workaround for the order member? And I'm not quite clear on what it is actually returning...it seems the variable i should give you the order.
The order just seems intrinsic from the code, it is never set to any value, so what 'order' is it? I can't seem to find any docs on this 'member' of the Array class.
Here is the code:
var sorted : Array = new Array();
var i : int = 0;
for (var fx : PostEffectsBase in GetComponents(PostEffectsBase))
{
if(fx && fx.enabled)
{
sorted[i++] = fx;
}
}
while (sorted.length)
{
var indexToUse : int = 0;
var orderValue : int = -1;
for(i = 0; i < sorted.length; i++) {
if(sorted[i].order > orderValue) {
orderValue = sorted[i].order;
indexToUse = i;
}
}
...more code...
I solved it. The problem is not with the Array Class as the fx that is being assigned to the sorted[] array is an object of class PostEffectsBase.
So the actual problem is one of casting when we try to use sorted[i].order
I changed the reference from sorted[i].order to (sorted[i] as PostEffectsBase).order and it worked.
I have to remember this. It seems there are a lot of these casts that have to be done between platforms.

Categories

Resources