How to get a string from a string-array? - android

In my app I have a ListView which is populated with URLs from a string-array. When the user selects some item in this ListView, the app should open a WebView and load the selected URL.
The string-array is like this:
<string-array name="app_urls">
<item>http://www.google.com</item>
<item>http://www.android.com/</item>
<item>http://www.youtube.com</item>
</string-array>
I am trying to get the string value of the url this way:
Resources res = getResources();
String[] url = res.getStringArray(R.array.app_urls);
webView = new WebView(view.getContext());
webView.loadUrl("http://" + url);
How can I correctly get the url value?

I dont know much about android but the last line you probably want something like
webView.loadurl(url[index of the url you want to use]);
because A) you already have the http://'s in your array definition so it would be redundant your way,
and B) as the above poster said you have to reference your array items by index. Think of an array like a train, where each 'index' is a car holding some data. Arrays are 0-indexed so the first slot is [0], 2nd is [1] and so on.
So if you wanted to get google out of your frst array youd call bookmark_urls[0], and if you wanted offspot youd say bookmark_urls[2].
I'm not sure why you define your string array then declare another string array and (as near as i can tell) make it equal to the first though. Cant you just reference 'bookmark_urls[]' throughout?

you have to access the element in the array.. for instance url[0] if the first element and so on.... you can't call an array in that way....

Related

Spinner from json raw file

I have a raw json file like this
{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}
{"_id":1270260,"name":"State of Haryāna","country":"IN","coord":{"lon":76,"lat":29}}
{"_id":708546,"name":"Holubynka","country":"UA","coord":{"lon":33.900002,"lat":44.599998}}
{"_id":1283710,"name":"Bāgmatī Zone","country":"NP","coord":{"lon":85.416664,"lat":28}}
{"_id":529334,"name":"Mar’ina Roshcha","country":"RU","coord":{"lon":37.611111,"lat":55.796391}}
{"_id":1269750,"name":"Republic of India","country":"IN","coord":{"lon":77,"lat":20}}
{"_id":1283240,"name":"Kathmandu","country":"NP","coord":{"lon":85.316666,"lat":27.716667}}
{"_id":703363,"name":"Laspi","country":"UA","coord":{"lon":33.733334,"lat":44.416668}}
It is NOT an JSON array - it is a huge list of JSON objects. Here Populate the spinner from JSON In Android, here Convert json array to list of json(raw) strings here android how to convert json array to string array or even here https://developer.android.com/guide/topics/ui/controls/spinner.html it is assumed that I have a JSON array or static data. I'd like to have two spinners in which one is for country and second one is for city. Let's assume I have list of countries so it will be a static data. I'd like the second spinner to dynamically load from raw json file all cities that corresponds to selected city in first spinner. I think that I can handle spinners thing but how can I load data from raw json file into spinner? Do I need to modify this raw file so that it will be a json array and then do something like this:
JSONArray jsonArray = new JSONArray(arraytitle);
List<String> list = new ArrayList<String());
for(int i = 0 ; i < jsonArray.length();i++){
list.add(jsonArray.getJSONObject(i));
}
EDIT
I found this answer https://stackoverflow.com/a/26780581/4671908 - if I will succeed I will post an answer
So you have two questions:
How to update the second Spinner dynamically when the first Spinner is updated
Three thing you need to do:
Implement AdapterView.OnItemSelectedListener
Update the backed data set inside the adapter of the second Spinner. In your case it would be a list of city. (To do this you need to create your own adapter extended from ArrayAdapter)
Call adapter.notifyDataSetChanged()
How to use the JSON
There are several solutions there. But it would be hard for others to provide the one that would suit your case the best without knowing any detail of the requirement.
Just remember one thing when you face design problem:
Do the simplest thing first.

How to pass a 'json' parameter with an url in android?

I have check-boxes on one page and when user selects a particular check-box, it passes the id associated with it to the next page, on this page I want to send those ids in the form of url to a wcf service.
My url is
http://rankup.co.uk/service1.svc/getTopic/
After adding the parameters, I want this json to pass with the url like
http://rankup.co.uk/service1.svc/getTopic/20,32
where "20,32" are the ids selected by the user (it may vary accordingly)
Can anybody tell me how to do this?
Just pass the parameters with the Intent from the calling activity like this
intent.putExtra("parameters", "20,32");
Then retrieve this parameter from the second activity :
String parameters = getIntent().getStringExtra("parameters", 0.0);
After that make the url like this :
String url = "http://rankup.co.uk/service1.svc/getTopic/ "+parameters;
Second part : to get and parse the json you can see this tutorial.
Happy Coding.
You can create an array list or can create an string variable. On clicking on checkbox, you can add all ids in a comma separated list or array and then can send using intent to the next screen.

How do I create and read from arrays in Android?

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 .

How to get a string from string.xml in android when constructing name at run time?

In my android app, I have a number of buttons in a grid (basically a 2-D array of components). When long pressing these components I need to display a string to the user, with each array location having different strings.
For an actual app where I am using this, please see :
RIFT Assistant : https://market.android.com/details?id=com.gopalshalu.rift.assistant
In the app, start up a soul tree .
Question
Is there a way to dynamically formulate the name of string, and get the strings value.
Something like…
Int row = 0;
String target_string_name = “”;
for (int col=0;i<1;i++)
{
target_string_name = “teststring_” + row + “_” + col; // we have dynamically created the name
How do we get the actual string value here, using string name in target_string_name variable?
}
How do we get the actual string value here, using string name in target_string_name variable?
Example
String to be displayed when pressing grid location (0,0) - Hello, test string at 0,0
String to be displayed when pressing grid location (0,1) - World!.. test string at 0,1
I have a string.xml file, with the following naming convension:
<string name=’teststring_row_column’>string contents</string>
So, for the above example, the string.xml will look like:
<string name=”teststring_0_0”>Hello, test string at 0,0</string>
<string name=”teststring_0_1”>World!... test string at 0,1</string>
Thanks in advance for your time and responses.
If I understand you correctly, I believe you're looking for getIdentifier().
Here's a demonstration of its use.
I think you might consider making use of setTag() also if you want to associate a particular piece of data with a view. I also started a Rift Calculator (never finished) and for display the spell tooltips (I assume that's what you're doing?) I just associated the tooltip data with the tag for that view.

Conceptual help with Android ListView

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.

Categories

Resources