Android text is in reversed order - android

As a result of the two for-loops below I should get the word an Album name, but I get this name in reverse order. For example if I expect "LINK", I get "KNIL"... Where am I going wrong?
Here is the code:
jsonobj = new JSONObject(param);
JSONObject datajson = jsonobj.getJSONObject("data");
JSONArray news = datajson.getJSONArray(TAG_NEWS);
JSONArray actual = datajson.getJSONArray("actual");
for(int i = 0; i < news.length(); i++){
JSONObject c = news.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String title = c.getString(TAG_TITLE);
String album = c.getString(TAG_ALBUM);
TextView tv = (TextView) findViewById(R.id.imgtest);
//tv.setText(album);
if(!(album == "null")){
String var[] = album.split("|");
for(int a=0;a<var.length;a++){
String t = var[a].intern();
String al = ((TextView) findViewById(R.id.imgtest)).getText().toString();
String b = t+al;
tv.setText(b);
}
}else{
break;
}
}

This line is adding the newest letter t to the beginning of your existing String al:
String b = t+al;
However rather than simply switching this line around (b = al + t), I recommend a faster method:
String album = c.getString(TAG_ALBUM).replaceAll("|", "");
TextView tv = (TextView) findViewById(R.id.imgtest);
tv.setText(album);
I looks like you only wanted to remove the "|" character from your album String, so just use String.replaceAll().
If you need album split into an array, then when you rebuild the String don't waste your time calling findViewById() on a view that you already have. In fact you already have the String, so there is no need to use getText() either:
String var[] = album.split("|");
StringBuilder builder = new StringBuilder();
for(int a=0;a<var.length;a++)
builder.append(a);
// use builder.toString() when you want the cleaned album name.

To add the latest letter t to the end of the current string al:
b = al + t

Related

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
}

how to show json array as html in textview

i want to show json in html tag below mycode im getting json and show in two diffrent textview but i want to show in single lines and show all getJSONObject(i).getString("name")); name in bold color and all getJSONObject(i).getString( "sub_ingredients")); in simple text do not want to use two textview i want toshow insingle text view what wil ido? i want to show as html tag
"dish_ingredient":
[
{"name":"Salt","sub_ingredients":""},
{"name":"Sesame Seeds","sub_ingredients":""},
{"name":"Calcium Sulfate","sub_ingredients":""},
{"name":"Brown Sugar","sub_ingredients":""},
{"name":"Salt","sub_ingredients":""},
{"name":"Hamburger Bun","sub_ingredients":""},
{"name":"Cheese-cultured pasteurized milk","sub_ingredients":""},
{"name":"Hamburger Bun","sub_ingredients":"Wheat, Niacin, Eggs"}]}
final LinearLayout table3 = (LinearLayout) findViewById(R.id.table3);
JSONArray school5 = json2.getJSONArray("dish_ingredient");
for (int i = 0; i < school5.length(); i++) {
row4 = getLayoutInflater().inflate(R.layout.row2, null);
((TextView) row4.findViewById(R.id.name)).setText(school5
.getJSONObject(i).getString("name"));
((TextView) row4.findViewById(R.id.subingredients))
.setText(school5.getJSONObject(i).getString( "sub_ingredients"));
table3.addView(row4);
}
If I understand correctly you want to display the JSON as formatted above in one TextView but with certain highlighting. Check out this post. If you want to keep the JSON in its original format I would just get the initial JSONArray, call toString(), and then replace the words "name" and "sub-ingredient" with text fomatted the way you want.
If I misunderstood and you want to extract the values for each JSONObject you just need to loop through the JSONArray
//format these to look like whatever you want
SpanableString nameTitle = "Name: "
SpanableString subIngredientTitle = " Sub-ingredient: ";
String concatProduct = "";
int arraySize = jsonArray.length();
for(int i = 0; i < arraySize; i++){
JSONObject thing = jsonArray.getJSONObject(i);
String name = thing.getString("name");
String subIngredient = thing.getString("sub-ingredient");
if(i == 0){
concatProduct = nameTitle + name + subIngredientTitle + subIngredient;
} else {
concatProduct += " " + nameTitle + name + subIngredientTitle + subIngredient;
}
}
textView.setText(concatProduct);
Something like this would let you process the JSONArray and build one string that you could set to one TextView. The formatting is up to you.

Extract array elements and create a String from them

I have got an array through Vector addittion like
[1!,2!,3!,4!]
I have to convert it into a string like
{1!2!3!4!}
..can you please tell me the name of few methods by which i can make it? Thanks all..
String getElement = null;
for(int j = 0;j<5;j++){
getElement = dynamicViewtagNames.elementAt(j);
}
I can get elements of this array like this..then I have to convert it into a string.
I'm not sure if I understand your question correctly, but if you just want to turn this:
[1!,2!,3!,4!]
into
{1!2!3!4!}
you can for example make use of the String.replace() or String.replaceAll() method.
String str = "[1!,2!,3!,4!]";
str = str.replace("[", "{");
str = str.replace("]", "}");
str = str.replace(",", "");
If [1!,2!,3!,4!] is a Vector containing the Strings you showed us above, you could do it like this using a StringBuffer:
// letz assume this vector has the following content: [1!,2!,3!,4!]
Vector<String> dynamicViewtagNames = new Vector<String>();
StringBuffer b = new StringBuffer();
b.append("{");
for(int i = 0; i < dynamicViewtagNames.size(); i++) {
b.append(dynamicViewtagNames.get(i))
}
b.append("}");
String mystring = b.toString();
Simple Solution
String names = names.replaceAll(",","");
names = names.replaceAll("[", "{");
names = names.replaceAll("]", "}");
Use this code,
StringBuilder str = new StringBuilder();
for (int i = 0; i<dynamicViewtagNames.length;i++){
str.append(dynamicViewtagNames[i])
}
str.toString();
or you can use:
Arrays.toString(dynamicViewtagNames);
Thanks

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);
}

how to make JSObject as array?

I have alot of link to get data from web hence I wanted to use loop to retrieve each URL's data but I had trouble in making JSObject as array.
JSONObject[] jsObjectallnewstype;
JSONArray[] jsonArrayallnewstype = null;
for(int i = 0; i < categories.length(); i++)
{
JSONObject c = categories.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TAG_TITLE);
String url = c.getString(TAG_URL);
jsObjectallnewstype[i] = JSONFunction.getnewstype(title, url); //java.lang.NullPointerException
jsonArrayallnewstype[i] = jsobjectallnewstype[i].getJSONArray(TAG_NEWLIST);
}
This line jsObjectallnewstype[i] get null error although the log shows JSONFunction.getnewstype successfully retrieve the data.
And i am also worry tat second line jsonArrayallnewstype[i] could cause same error as well.
So JSObject cant be put as array? If so what are the alternative??
To fix your current code, you need to initialize your array. This is why you are getting a NPE:
JSONObject[] jsObjectallnewstype = new JSONObject[categories.length()];
JSONArray[] jsonArrayallnewstype = new JSONArray[categories.length()];
for(int i = 0; i < categories.length(); i++)
{
JSONObject c = categories.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TAG_TITLE);
String url = c.getString(TAG_URL);
jsObjectallnewstype[i] = JSONFunction.getnewstype(title, url); //java.lang.NullPointerException
jsonArrayallnewstype[i] = jsobjectallnewstype[i].getJSONArray(TAG_NEWLIST);
}

Categories

Resources