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 .
Related
I'am using StickyGridHeaders for GridView with sections.
this library is using R.array.countries to populate GridView with provided data and extracting first letter from passed string array to create section heading.
I m trying to pass an ArrayList of objects that will contain ArrayList for sections text named as "sectionArrayList" and "contentArrayListPrivate" and "contentArrayListPublic".
Now i want to play a for loop that will play till < sectionArrayList.size and will check is there any sections then show section text and then show all contents associated with this section by contentArrayListPrivate.size and similar for others but unable to achieve my target.
Can someone please point me towards right direction how to achieve this task ?
want to achive something like:
I downloaded it and checked to solve your problem. so here is solution.
First of all that project is replaced to SuperSlim it is showing on your given link itself, so get superslim.
Then follow the example in that project to achieve it. In that example there is a file named CountryNamesAdapter.java open it, in the 37th line there i found this
final String[] countryNames = context.getResources().getStringArray(R.array.country_names);
which means the data is loaded from Array resource
replace it with
your data - convert your arraylist to string array and assign it like this
final String[] countryNames = myData.
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).
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.
How can I search through an String-Array? I've got an dictionary app and the words are saved in a String-Array and it would be user-friendlier, if you could search for the word you want to look up, instead of looking its way to the word. Can somebody help?
Thanks.
You can try using an ArrayList instead. Then you can see if the word is in your 'dictionary' by using the contains method, ex:
ArrayList <String> myDictionary = new ArrayList<String>();
myDictionary.add(new String("foo"));
myDictionary.add(new String("bar"));
...
// To check if the word exists in your dictionary.
if (myDictionary.contains(new String("word_to_look_up")))
{
}
Not entirely sure what you are looking for- do you want to see if the word is in the array, or what is the goal? If you want the user to get to the word faster, changing the string-array will not help the UI jump to the right place.
If you want it to be like Google suggest you could take your array and make a tree data object, which each node representing a letter in a word. Then if the user types a, you go into that node and offer possible words.
You may use the Arrays.binarySearch() method to search an element from the sorted array.
I have a working android app using TextView, some formatting (line breaks, rows of dashes) and Linkify to generate a primitive "ListView-like" display with clickable URLs in each "row". I'd like to move up to a real ListView, but I'm just not finding the sample/explanation that I need to take that next step.
I have successfully reproduced the HelloListView sample, starting with the hardcoded string array, and moving to a string array defined in my res/values/strings.xml. I've taken one small step toward my goal by adding my HttpClient code to retrieve a set of data from a service, parse the results into a String Array and feed that into setListAdapter() such that my text and links show up as text-only in ListView items.
I want to move to the next step which is to make each "row" in my ListView launch the browser to the URL contained in the data, either by
(A) clicking anywhere in the row, or
(B) clicking a hyperlink displayed within the row data
For option (A), it appears that I need to have my onItemClick() method issue an intent that launches the browser. That's straightforward, but I don't get how to associate the URL with the item (currently its just one part of the string content for each "row" of text). How do I separate my URL from the rest of the text, such that I can launch a browser to the corresponding URL? Do I need to replace my String Array with an array of custom objects?
For option (B), can I use Linkify? It seems that my string array elements get converted to individual TextViews (inferring from the way the Toast text is generated in the HelloListView sample). Do I have access to that TextView to run Linkify against? Do I need to replace my String Array with a TextView Array and run Linkify myself? Am I completely off base?
Thanks to anyone who can help explain back to me what I'm trying to do, in a way that helps to find my way around the SDK, samples and other helps!
How do I separate my URL from the rest of the text, such that I can launch a browser to the corresponding URL?
Use a regular expression (java.util.regex) to find the URL.
For option (B), can I use Linkify?
Yes.
Do I have access to that TextView to run Linkify against?
Yes. Override getView() in your ArrayAdapter. Chain to the superclass and get your TextView from the result of super.getView().
Even better would be to use Linkify on your strings before putting them in the array in the first place.
Do I need to replace my String Array with a TextView Array and run Linkify myself?
No, and that is really not a good idea. Here is a free excerpt from one of my books that goes into more detail on tailoring the individual rows of a ListView, in case this helps.