I'm trying to populate an array with files in my raw folder. I then pick a random slot in that array and use it to play that file.
But when I do this, every slot seems to contain whatever was the final video in the array. I'm not sure what the cause of this is...
var videos = Array(5){R.raw.c0; R.raw.c1; R.raw.c2; R.raw.c3; R.raw.c4};
videoView.setVideoPath("android.resource://" + getPackageName() + "/" + videos[Random.nextInt(0, 4)])
videoView.requestFocus()
videoView.start()
You're initializing the array incorrectly.
The {...} block after the Array(5) is actually a lambda that takes an integer and returns the content for that index in the array. The semicolons (rather than commas) mean that each of R.raw.c0, R.raw.c1, etc. is just a statement that doesn't do anything. Since R.raw.c4 is the last statement in that block, it sets all five indexes to that value.
You probably meant:
val videos = intArrayOf(R.raw.c0, R.raw.c1, R.raw.c2, R.raw.c3, R.raw.c4)
Note the replacement of semicolons with commas. I also switched the var to a val, as you don't appear to be changing it.
You should be doing one of the following to correctly create your array.
var videos = intArrayOf(R.raw.c0, R.raw.c1, R.raw.c2, R.raw.c3, R.raw.c4)
or
var videos = arrayOf(R.raw.c0, R.raw.c1, R.raw.c2, R.raw.c3, R.raw.c4)
Please try it, this should solve your problem.
Related
I want to keep a list with the ID of the users who liked the specific object. To achieve that I created an array where I'm trying to keep that list.
I also want to display how many users like that object.
In mapper:
likes = getLikedListSize(it["userLikedOfferList"].toString())
then
private fun getLikedListSize(userList: String): String {
return userList.length.toString()
}
The problem is that function returns random numbers. For example in the array are two items, function return "8" etc.
What is a better approach to store list and get the size of it in Firestore?
When you are using the following method call:
getLikedListSize(it["userLikedOfferList"].toString())
It means that you are trying to pass to the getLikedListSize() method, the String representation of the array object. This representation is nothing else but the address of the object in the memory. This address it's a String that consists of 8 characters. That's the reason why, when you call .length you return the length of that String and not the actual length of the array. To solve this, simply pass the array, without converting it to a String:
getLikedListSize(it["userLikedOfferList"])
And change the method like this:
private fun getLikedListSize(userList: Array<String>): Int {
return userList.length
}
Now, when calling this method, you'll always get the number of elements that exist in the userLikedOfferList array.
I am new in android development and when I read array data from firestore using following code
val variable = arrayOf(document.get("restaurant"))
and then loop over the variable using code
varibale.forEach {
Log.d("someTag", ${it.toString()} + " is your data")
}
I get the result with square brackets at log as following
[somedata, somedata2] is your data
my problem is that forEach loop runs only once and I am not able to get the result (without square brackets) as following
somedata is your data
somedata2 is your data
I have 2 elements in my restaurant array in firestore
I will be very thankfull to any one who will help me.
You are actually wrapping an array/list into another array when using arrayOf, that's why you see those brackets. Instead, try casting your document.get("restaurant") and then looping directly through it.
arrayOf doesn't parse an array. It creates a new array using the elements you pass to it. That's not what you want. You should instead cast document.get("restaurant") to the type that you expect to get from Firestore.
If a field is an array of strings, then the SDK will give you a List<*>, and you will need to make sure each item in the list is a String, if that's what you stored in the array.
val variable = document.get("restaurant") as List<*>
// Iterate variable here, make sure to check or convert items to strings
Ultimately I am trying to store an Int Array in Shared Preferences but I know Kotlin doesn't support that. So I am converting my Int Array to a String Array using the method here:
How can I store an integer array in SharedPreferences?
My issue is that I am struggling to put in a default value for the getStringSet method:
private fun loadIntScoreArray() {
val prefs = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE)
//TODO: Load the String array
var default = emptyList<String>()
avgScoreArrayString = prefs.getStringSet(AVG_SCORE_ARRAY, default)
}
However default is not an acceptable object in the prefs.getStringSet(AVG_SCORE_ARRAY, default) line. The error is confusing because it seems contradictory:
Required: MutableList
Found: (Mutable)Set!
Required: (Mutable)Set!
Found: MutableList
There is few things you need to know. Since API 11 you can only store plain objects or sets to shared preferences. You can convert your list to set, but it can be lossy conversion in your list contain duplicates.
If you want to use sets you should call it like this:
//to get
prefs.getStringSet(AVG_SCORE_ARRAY, emptySet<String>()))
//to set
prefs.edit().putStringSet(key, AVG_SCORE_ARRAY)
The other way is to join array to a single string using join operation. Here is a doc for you https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/join-to.html
To be honest both of these ways are not the perfect solutions. If it is production application and I recommend using persistance library like Room, Realm etc.
Hope it helps.
Edit.
If you are hundred percent sure you are going to have 5 ints stored (and it is not gonna change in a near or distant future), using database could be overkill. I recommend using joining to single string and storing it as single string or just store 5 independent int values. There is no point in complicating simple things.
I am using ksoap2 in order to extract an array of strings from a wsdl based webservice(for an android app). How do I process the returned array? I need those 3-4 lines of code which will let me save and use that returned array in my class. Thanks.
String r = NameArray.columncount("userid", limitstart, loadNumber,loggername);
String temp = r.replaceAll(";\\s", ",").replaceAll("string=", " ")
.replace("anyType{", "").replace(",}", "");
String[] fulname = temp.split(",\\s+");
'NameArray.columncount' is my function which gets the array from the wsdl(don't get confused in that)
step 1-
Here I am getting the array values returned from the wsdl in to a string called 'r'.In this case I am getting an array of numbers
Returned array string r looks like this
r ="anyType{string=10054; string=10055; string=10056; string=10035; string=10052; string=10036; string=10037; string=10038; }"
step 2-
Then creating a String variable called temp where I am removing all the unwanted characters using the replaceAll function.
after removing unwanted characters temp looks like this
temp="10054, 10055, 10056, 10035, 10052, 10036, 10037, 10038"
step3-
Finally created a string array called 'fulname' and split the modified string with ',\s'
Array fulname after split looks like this
fulname = [ 10054, 10055, 10056, 10035, 10052, 10036, 10037, 10038]
This will work fine because all the wsdl array return the same type of string with same unwanted characters
Hope you understood
Good Luck
If you are still on this problem, you can check out this article which explain the whole procedure to parse arrays returned in KSOAP:
http://seesharpgears.blogspot.fr/2010/10/web-service-that-returns-array-of.html
Hope this answer to your question ;)
I have a Android Air project written in AS3, when the project starts I am loading the music and loading the XML file to be parsed. In the first frame I call my classes to parse the XML and set the nodes at strings, and to play the audio, these both work fine. Also in my first frame I am declaring some textfields to input the data from the XML file so that when the user enters frame 3 the user is able to see this data from the XML file. This also works fine. The problem I am having is going between frame 4 and back to frame 3, the data in the textfields disapears? I trace the strings from the XML class that are holding the data and these values appear everytime, but going from frame 3 to frame 4 and back to frame 3 wipes out the textfield display? Can anyone point me in the right direction?
thanks
Scientific
Ok here is some code from frame one where I am declaring the textfields
var name1TextField:TextField = new TextField();
var name2TextField:TextField = new TextField();
var format:TextFormat = new TextFormat();
format.font = "_sans";
format.color = 0xF8FBF8;
format.size = 36;
//set the names format to the textfields
name1TextField.defaultTextFormat = format;
name2TextField.defaultTextFormat = format;
highScore1.addChild(name1TextField);
highScore2.addChild(name2TextField);
Here is the code calling the XML parsing class and setting the text to the nodes
var network:networkScores = new networkScores();
addChild(network);
var timer4:Timer = new Timer(600);
timer4.addEventListener(TimerEvent.TIMER, scoresDis);
timer4.start();
function scoresDis(e:TimerEvent):void
{
name1TextField.text = network.name1;
name2TextField.text = network.name2;
//trace(name1TextField.text);
//trace(name2TextField.text);
name1TextField.width = 230;
name2TextField.width = 230;
timer4.stop();
}
Previously above I had stated that I am calling the audio class and the xml class from the first frame, declaring the text fields and moving on from there. Now I have decided that since this XML file is a list of scores and coded be always changing, I thought it would be good to load and parse this file everytime I enter frame 3. The same thing is still happening, I have my scores display and then when I go to frame 4 and back to frame three, the scores do not display, but when I trace the data from the class, it displays properly.
Thanks
Scientific
I've seen this issue happen before with improperly embedded fonts or something, I can't recall exactly. A quick way to test this would be to trace out the .text property of the fields too. This way, when the text isn't visible, if the .text property of the field is still populated then you know you've got a rendering problem.
Do this quick test and post the results, I'll update my answer with more information depending on the outcome.