How to convert a string variable to a string array - android

I am making a request from my app to a server which is returning an array. I want to put that array into a listView but the the whole array is treated as a single listItem.
As the response is recieved in a string variable i want to convert it to a string array?
any help is appretiated.. Thanks

Just replace arr with your String Array variable name:
StringBuilder builder = new StringBuilder();
for(String s : arr) {
builder.append(s);
}
String str = builder.toString();

Firstly string response data split by space[Available special character] or assign string array.
String yourdata = "A B C D E F G H";
String[] yourtDataArr = yourdata.split(" ");
ArrayList<String> list = new ArrayList<String>();
for(i =0;i<yourtDataArr.length;i++){
list.add(yourtDataArr[i]);
}
Then use your list on ListView. You can see following example.
http://www.vogella.com/tutorials/AndroidListView/article.html
https://www.tutorialspoint.com/android/android_list_view.htm
http://windrealm.org/tutorials/android/android-listview.php

Related

How can i put a list to array

How can i put a list of strings, type string
in
String countryList[] = {a};
a is the variable that have the list
so, it will be
String countryList[] = {"heyyesGabon","heyyesGibraltar","heyyesGuinea"}; //and the others
this list comes from console.log in javascript
it always save the last item
From List to String array
String [] countryArray = countryList.toArray(new String[0]);
From String array to List
List<String> list = Arrays.asList(array);
You can use in-build function list.toArray(stringArray); it will return String[].
String countryList[] = new String[list.size()];
countryList = list.toArray(countryList);
Hope this will help you.

Handle comma separated string

Creating an app in which i want to get string from json but i have one key and multiple value so i don't know how to handle this.
"colours": "#fff600,#000000,#ffffff,#00000,#ff9900,#333333"
And want to use this color in different class:
final ValueAnimator colorAnimation = ValueAnimator.ofObject(new android.animation.ArgbEvaluator(), Color.RED, Color.BLUE,Color.WHITE,Color.YELLOW,Color.CYAN,Color.MAGENTA,Color.GREEN,Color.GRAY);
colorAnimation.setDuration(1400);
Put the value of colours in string variable and then split the string in following way and add it to an arraylist :
String[] arr = str.split(",");
ArrayList<String> arr1 = new ArrayList<String>();
for (int i=0; i<arr.length; i++){
arr1.add(arr[i]);
}
Get value of color and use string tokenizer with ',' delima like this:
StringTokenizer stringTokenizer = new StringTokenizer(colorValueString, ",");
Also it has stringTokenizer.nextToken to get the next color in string
You can get the value of colours as a string and then split the string into parts like this:
String colours = json.getString("colours");
Log.d(TAG, colours);
String items[] = colours.split(",");
for (String item : items) {
Log.d(TAG, item);
}
If you own the json
You should use JSONArray:
"colours": ["#fff600","#000000","#ffffff","#00000","#ff9900","#333333"]
And read it like
JSONObject json = ...;
JSONArray colorsArray = json.getJSONArray("colours");
for(int i = 0; i < colorsArray.length(); i++) {
String colorString = colorsArray.getString(i);
int color = Color.parseColor(colorString);
// you should probably also catch IllegalArgumentException for wrong input
}
If you don't own the json
You can read it as string and split around commas:
JSONObject json = ...;
String colorsString = json.getString("colours");
String[] colorStrings = colorsString.split(",");
for(String string : colorStrings) {
int color = Color.parseColor(string);
// you should probably also catch IllegalArgumentException for wrong input
}

Android- How to get JSON values from string

I have three strings. Following the value of String a;:
{"a":"value","b":"value"}
And the value of String b;:
[{"a":"value","b":"value"},{"c":"value","d":"value"}]
And the value of String c;:
[[{"a":"value","b":"value"},{"c":"value","d":"value"}],[{"e":"value2","f":"value"},{"g":"value2","h":"value"}]]
How to get the value of a of String a;, the value of a and c of String b; and a, d, f and h of String c; through JSON without use libraries beyond those already available?
Use JSONObject. For json String a:
JSONObject aObj = new JSONObject(a);
String a = jObj.getString("a");
String b = jObj.getString("b");
Json string b:
JSONArray bArr = new JSONArray(b);
JSONObject first = bArr.getJSONObject(0);
Json string c:
JSONArray cArr = new JSONArray(b);
JSONArray innerArr = cArr.getJSONArray(0);
JSONObject first = innerArr.getJSONObject(0);
Try using the GSON parser. See http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html or many other resources

JSON parsing into ListView Android

This is the JsonObject i get form parsing my url: {"status":0,"items":"1333333454510|-7611868|-7222457"
now i need to get the seperate numbers seperetly as long.
i also get an error if i try to convert it to string: org.json.JSONException: Value 1333333454510|-7611868|-7222457 at items of type java.lang.String cannot be converted to JSONObject
How? Thanks
here is some code
JSONObject obj = JSONParser.getJSONFromUrl(URL + "search", parameters);
String firstItem = obj.getJSONObject("items").toString();
Split your items using \\|.
Example :
String items= "1333333454510|-7611868|-7222457" ; //here you got items
String[] _items=items.split("\\|");
Now iterate loop and convert it into long using Long.ParseLong().
EDIT :
As Ashwin said you need to use json.getString("items"); to get items.
You can convert it into long using following code.
ArrayList<Long> longs=new ArrayList<Long>();
for(int i=0;i<_items.length;i++){
longs.add(Long.parseLong(_items[i]));
}
Use Split function to separate.
parse json to get items in items String
use jsonObject.getString instead of jsonObject.getJSONObject to avoid error.
String items= "1333333454510|-7611868|-7222457" ;
String[] itemsArray =items.split("\\|");
String response= "{"status":0,"items":"1333333454510|-7611868|-7222457"}" ; // ie, response contains the string.
JsonObject json= new JsonObject(response);
String item=json.getString("items");
String [] items =item.split("|");
Will get 3 numbers in the items array.
JsonObject json= new JsonObject(response);
String item=json.getString("items");
String [] items =item.split("|");
your problem is here
JSONObject obj = JSONParser.getJSONFromUrl(URL + "search", parameters);
String firstItem = obj.getJSONObject("items").toString();
so change it to
String firstItem = obj.getString("items").toString();
and then do split operations as suggested by others.
SO force always with you:)

Get Text from Json and add it to String[]

I want to fetch a text (some urls) from Json and add them straightly to a String[] in android. The text is like
"http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-1.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-2.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-3.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-4.png"
I want to put this text into a String[]. I know I can use this method:
products = json.getJSONArray(TAG_PRODUCTS);
JSONObject c = products.getJSONObject(0);
String name = c.getString(TAG_NAME);
But it gives me String not String[].
By looking your String
"http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-1.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-2.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-3.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-4.png"
It seems that this can be converted into String[].
use like
String[] result = above_string_variable.split(",");
In your case
use
String[] result = c.getString(TAG_NAME).split(",");
instead of
String name = c.getString(TAG_NAME);
products = json.getJSONArray(TAG_PRODUCTS);
String[] result = new String[products.length()];
for(int i=0;i<products.length();i++) {
JSONObject c = products.getJSONObject(i);
result[i] = c.getString(TAG_NAME);
}

Categories

Resources