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).
Related
How to make history score in Array
I try u make score in array like this
this my firestore
And this is my code
String uid = auth.getCurrentUser().getUid();
muridref.document(uid).update("nilai", FieldValue.arrayUnion(skortampil));
When I get the same score the array field doesn't make new Array data,
without see data there or not in array
As mentioned in the documentation Update elements in an array about this behavior:
If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present.
Considering that, it's working as expected, since it's not adding values that are equal. So, this means that you won't be able to add values that are equal using the method arrayUnion() directly.
This other question from the Community - accessible here - indicates that for you to achieve this goal, you will need to read all the values from the array in your client side, update your values in the array outside the database and then, writing/updating it back in the database.
Let me know if the information helped you!
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.
I'm making a fake command-line system for a fun app, and I want to show the input and output in the same TextView, like this:
>something
>something else
>even more stuff
>etcetera.
I already figured out how to store the text from the EditText into a string and add \n and >, but I can't use strings for the whole thing: to avoid clogging up RAM, I'd like to delete lines after, say 50? I figured that would be much easier to do using Lists.
However, this doesn't work:
log.setText((CharSequence) logText);
But what will?
This method :
http://developer.android.com/reference/android/text/TextUtils.html#join(java.lang.CharSequence, java.lang.Iterable)
return a string composed of each element (either cast as a string or the toString value is used) separated by the delimiter in between each element. You can therefore easily concat all your items in one String.
You can also use http://developer.android.com/reference/java/util/AbstractList.html#subList(int, int)
to limit the count of items in said list.
From your question I assume logText is a List of some sort, therefore you can call
log.setText(TextUtils.join("\n>", logText.subList(0, 50));
Maybe you can put all your strings in a list, an each time you add one, recreate a single string from the list which contains all your items, and affect it to your textview.
You could use a ListView without a separator and populate it using an ArrayAdapter.
That way you wouldn't have to worry about memory, and the user could easily scroll through previous commands.
My app is using api 7. I don't know where to start with this challenge.
I have SQLite DB with some numbers stored between 1-99. Now I would like to make number picker for this range which would also remove numbers that are already in DB.
Create a list off the numbers 0-99, then do a query on your database. For every row in the results from the database, check if the list contains is (something like list.contains(number). If it is there, remove it (list.remove(item)) then proceed to the next row
You'll probably have to make your own widget. This will be somewhat involved, and since you are an Android beginner you might better spend your time coming up with a different input method.
In case you do decide to write your own widget, I would recommend extending LinearLayout, then inside of the constructor, doing something like this psuedocode:
setOrientation(VERTICAL);
addView ImageButton arrowUpButton;
addView EditText numberEditText;
addView ImageButton arrowDownButton;
arrowUpButton.setOnClickListener {
int num = myListOfInts.get(currentIndex++);
numberEditText.setText(num);
}
//vice versa for arrowDownButton
you'd also have to create a setter for the myListOfInts.
Good luck!
I've used arrays in other languages (Python, MATLAB/Octave, C, Visual Basic, BASIC), but I haven't figured out how to use them in Android.
For example, how would I modify the Hello World program at World program at http://developer.android.com/resources/tutorials/hello-world.html to replace "Hello, Android" with the name of a TV show denoted by an index number (tvshow[0], tvshow[1], etc.)? Let's name the array tvshow, and the four values in the array are "Route 66", "The Twilight Zone", "Magnum P.I.", and "MASH".
What method(s) can I use to accomplish this?
There is also ArrayList . So all you have to do to add is call the add method and to read you use the get method.
ArrayList <String> myArrayList = new ArrayList<String>();
//this is how you add the elements you want to.
myArrayList.add("Route 66");
myArrayList.add("The Twitlight Zone");
myArrayList.add("Magnum");
myArrayList.add("P.I.");
myArrayList.add("Mash");
and to read the element you want:
String tvShow= myArrayList.get(2);
The advantage over a normal array (i.e. the one that android developer mentioned) is that you dont need to define an array size and you can add new elements in case the tv shows you have aument by just calling the add method.
well , you are talking about strings , so for an array of strings , you can simply use:
String[] tvshow={"Route 66","The Twilight Zone","Magnum", "P.I.", "MASH"} ;
for using it , use:
String currentTvShow=tvshow[3];
for working with strings , read this:
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
strings on java (and on android, since it's based on it) are not mutable (for various reasons) , so they can't be modified. if you wish to modify such a thing , either use StringBuilder or an array of chars .