give each place in array randomaly button-android - android

I need some idea of how randomaly give each button[i] on of the values R.id.buttonj_mg.
(one to one function...).
I don't know how to do it since R.id.button1_mg is not a string, so I can't do somethink like R.id.button+j+_mg when j chossen randomaly..
This is the situation now:
button[1]= (Button)findViewById(R.id.button1_mg);
button[2]= (Button)findViewById(R.id.button2_mg);
button[3]= (Button)findViewById(R.id.button3_mg);
button[4]= (Button)findViewById(R.id.button4_mg);
button[5]= (Button)findViewById(R.id.button5_mg);
button[6]= (Button)findViewById(R.id.button6_mg);
button[7]= (Button)findViewById(R.id.button7_mg);
button[8]= (Button)findViewById(R.id.button8_mg);
button[9]= (Button)findViewById(R.id.button9_mg);
button[10]= (Button)findViewById(R.id.button10_mg);
button[11]= (Button)findViewById(R.id.button11_mg);
button[12]= (Button)findViewById(R.id.button12_mg);
button[13]= (Button)findViewById(R.id.button13_mg);
button[14]= (Button)findViewById(R.id.button14_mg);
button[15]= (Button)findViewById(R.id.button15_mg);
button[16]= (Button)findViewById(R.id.button16_mg);

You could use a collection to store your ints as Integers and then use the Java Collection class shuffle() method on those objects. Then you could remove them one by one from the Collection in each one of your buttons.
List<Integer> resources = new ArrayList<Integer>();
...
resources.add(R.id.button1);
...
Collections.shuffle(resources);

One solution is to create the buttons and their ids in the code instead of taking them from resources, look at https://stackoverflow.com/a/11615356/987358. Then you can store them easily in a collection as another answer suggests.
Another solution is the Java reflection API which allows to retrieve the values of the ids using strings of the id names.

Related

How to append an array into an array field in firestore?

Question
I have a collection named Users with a field named friendEmails which is an array that contains Strings.
I have a document with friendEmails = {joe#gmail.com, dan#gmail.com}, and I want to append newEmails = {mat#gmail.com, sharon#gmail.com} to it.
Problem
The only options I know for this are:
Reading the friendEmails array first, then adding the union of it and newEmails to the document.
Iterating over the elements of newEmails (let's and each iteration doing:
myCurrentDocumentReference.update(FieldValue.arrayUnion, singleStringElement);
(Since FieldValue.arrayUnion only lets me pass comma-separated elements, and all I have is an array of elements).
These two options aren't good since the first requires an unnecessary read operation (unnecessary since FireStore seems to "know" how to append items to arrays), and the second requires many write operations.
The solution I'm looking for
I'd expect Firestore to give me the option to append an entire array, and not just single elements.
Something like this:
ArrayList<String> newEmails = Arrays.asList("mat#gmail.com", "sharon#gmail.com");
void appendNewArray() {
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.collection("Users").document("userID").update("friendEmails", FieldValue.arrayUnion(newEmails));
}
Am I missing something? Wouldn't this be a sensible operation to expect?
How else could I go about performing this action, without all the unnecessary read/write operations?
Thanks!
You can add multiple items to an array field like this using FieldValue.arrayUnion() as the value of a field update:
docRef.update("friendEmails", FieldValue.arrayUnion(email1, email2));
If you need to convert an ArrayList to an array for use with varargs arguments:
docRef.update("friendEmails", FieldValue.arrayUnion(
newEmails.toArray(new String[newEmails.size()])
));

Dynamic Variable Names for Android App

I have an activity with 4 TextView elements with ids of Mon1, Mon2, Mon3, Mon4.
Is it possible to create a loop in the MainActivity.java code where I can perform, for example, a setText action on each of the 4 ids without having to list them out one-by-one.
ie. Mon*X*.setText=""; (where X is a value from 1 to 4).
I guess to take this one step further, if the ids were actually Mon1, Mon2, Mon3, Mon4, Tue1, Tue2, Tue3, Tue4, Wed1 .........Sun1, Sun2,Sun3, Sun4. Could a loop be created to not only change the number 1..4 but also use an array for the Mon, Tue, Wed etc.
The end result being some sort of loop that can do setText on ALL the ids that I need rather than 28 individual setText commands.
You could do something like:
TextView Mon1; //and do whatever with it
TextView Mon2; //And so on
TextView[] tv = {Mon1, Mon2, Mon3, /*etc*/}
int i = 0;
void doSomething(){while(i<=/*number of TextViews*/){tv[i].setText("BLAH");i++;}}
I hope this helped :D
Is it possible to create a loop in the MainActivity.java code where I
can perform, for example, a setText action on each of the 4 ids
without having to list them out one-by-one.
Yup. Use an array.
To take it another step further, use another array. It's what they're made for.
(By array, I mean an ArrayList, HashMap, dictionary, array, or any other data structure like that).

Android SparseArray<SparseArray<Object>> initialization

I have to implement the classification of somehting like Hashmap with two keys and a value, let's say Hashmap<K1, K2, V>, where the two keys are integers and the value is a generic MyObject defined by me.
I read this, this, and this post, and I also know that guava project offers the table interface, but I don't want to use external libraries (if not strictly necessary) to keep my project smaller as possible.
So I decided to use SparseArrays: I thought that this was the better choice because my keys are int and are not necessarily starting from zero and increasing.
I do this initializing:
SparseArray<SparseArray<MyObject>> myObjectSparseArray = new SparseArray<SparseArray<MyObject>>();
Now let's go to the point. Can I do this kind of operation:
MyObject myObject = new MyObject();
myObjectSparseArray.get(3).put(2,myObject);
or should I do something like:
MyObject myObject = new MyObject();
myObjectSparseArray.put(3, new SparseArray<MyObject>());
myObjectSparseArray.get(3).put(2,myObject)
In other words: Do I initialize both SparseArrays with this single line?
SparseArray<SparseArray<MyObject>> myObjectSparseArray = new SparseArray<SparseArray<MyObject>>()
Do you think there are better implementations for my case?
If you have two keys and one value, I would just use a normal HashMap or SparseArray and will combine those two keys into one value.
Let's say you have one key which is String and one which is Long so your map key will be:
(strKey + longKey).hashCode(). You can use it as Integer and save it into SparseArray or as String and use HashMap.
Having tow nested SparseArray is against the purpose of having SparseArray in the first place since you will allocate a new SparseArray for each unique first key in your key pairs.
A good solution for that would be by using hashmap and use a key object that takes two int values (Point for example) or an object that you define that simply holds two keys.

Playing raw file based on String

I have a simple program where I generate random numbers, and use these to return an element from an array. Based on this array element, I want to play a sound file. For example: The random element that was returned was "Am". I now want to play the file "am.ogg". But you cant just throw a string in for the resourceId. Any ideas?
This seems to be duplicate of this thread (which by the way is also duplicate). Except for using this method you can also use reflection on the R class (second also not good option). The best option is the one in which the list of ids you will be interested in can be determined in the code. Basically you define map mapping every string to the corresponding R.id variable (int). However, I am not quite sure this will be your case.

Android: Can I save more than one SharedPreference entry to a file?

This is the problem:
I have a form for inputting names, then saves it, then displays it in another activity. But once I try to enter another name, the previous name is overwritten by the new name.
Is there a way to set these names up to list themselves one after another without overwriting each other in SharedPreferences?
You can as long as they have distinct names. Ig you need multiple values for same name, you can store JSON array or use some form of prefix / suffix solution to provide unique names
Either do it like Konstantin Pribluda suggested, or you might think of using the SQLite, if you have a lot of names you want to store (e.g. if creating a history of typed in names). That way you can store unlimited values for the same key and retrieve them as a list/cursor.
But of course that's overkill if you only have 2-3 names…
you can also save a Set of Strings in the SharedPreferences.

Categories

Resources