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.
Related
I want to load JSON data into the List view with subtitle and images (Left side on the row).
Here below I have posted my sample JSON response code :
for (int i = 0; i < contacts.length(); i++) {
JSONObject obj = contacts.getJSONObject(i);
Iterator keys = obj.keys();
while(keys.hasNext()) {
// loop to get the dynamic key
String currentDynamicKey = (String)keys.next();
// store key in an arraylist which is A,B,...
// get the value of the dynamic key
JSONArray currentDynamicValue = obj.getJSONArray(currentDynamicKey);
Log.d("Response: ", "> " + currentDynamicKey);
int jsonrraySize = currentDynamicValue.length();
if(jsonrraySize > 0) {
for (int ii = 0; ii < jsonrraySize; ii++) {
JSONObject nameObj = currentDynamicValue.getJSONObject(ii);
String name = nameObj.getString("name");
System.out.print("Name = " + name);
//Log.d("Response: ", "> " + name);
//store name in an arraylist
}
}
}
}
I want to Show Tittle : currentDynamicKey values and Sub Title : Name string values.
If you already have your data and you don't necessarily need to integrate the server requests with your adapter, then you really just need an adapter which supports a JSONArray. There's a really nice open source one within the Advanced-Adapters library. Then it's just a matter of passing the JSONArray to the adapter and setting the adapter to a ListView.
I am having trouble in displaying the values in text view. It displays only the last value in text view. I want to iterate each and every value in new line.
Here is my code.
PreviewCatNameTxt = new TextView[obdatin.catCount];
PreviewNameTxt = new TextView[10];
PreviewQtyTxt = new TextView[10];
PreviewAmtTxt = new TextView[10];
System.out.println("setview3");
for(int i=0;i<obdatin.catCount;i++){
System.out.println("num oof prca"+obdatin.catList[i].getNumCategory());
if(obdatin.catList[i].isTotQtyNonZero(0)){
PreviewCatNameTxt[i] = (TextView) findViewById(R.id.textViewh);
PreviewCatNameTxt[i].setText(obdatin.catList[i].getCategoryName(0));
System.out.println("prca"+obdatin.catList[i].getCategoryName(0));
}
int cnt =obdatin.catList[i].proList.getNumProduct();
System.out.println("setview3");
for(int j = 0; j<cnt; j++) {
if(obdatin.catList[i].proList.isQtyNonZero(j)){
PreviewNameTxt[j] = (TextView)findViewById(R.id.textView1);
PreviewNameTxt[j].setText(obdatin.catList[i].proList.getProductName(j));
PreviewQtyTxt[j] = (TextView)findViewById(R.id.textView2);
PreviewQtyTxt[j].setText(obdatin.catList[i].proList.getProductQty(j));
PreviewAmtTxt[j] = (TextView)findViewById(R.id.textView3);
String amt = obdatin.catList[i].proList.getProductAmt(j);
PreviewAmtTxt[j].setText(amt);
System.out.println("prcaN"+obdatin.catList[i].proList.getProductName(j));
System.out.println("prcaQ"+obdatin.catList[i].proList.getProductQty(j));
System.out.println("prcaA"+obdatin.catList[i].proList.getProductAmt(j));
}
}
Edited Code as per your requirement
PreviewCatNameTxt = (TextView) findViewById(R.id.textViewh);
for(int i=0;i<obdatin.catCount;i++){
System.out.println("num oof prca"+obdatin.catList[i].getNumCategory());
if(obdatin.catList[i].isTotQtyNonZero(0)){
PreviewCatNameTxt.append(" "+obdatin.catList[i].getCategoryName(0));
System.out.println("prca"+obdatin.catList[i].getCategoryName(0));
}
int cnt =obdatin.catList[i].proList.getNumProduct();
System.out.println("setview3");
for(int j = 0; j<cnt; j++) {
if(obdatin.catList[i].proList.isQtyNonZero(j)){
PreviewCatNameTxt.append("\n"+obdatin.catList[i].proList.getProductName(j));
PreviewCatNameTxt.append("\n"obdatin.catList[i].proList.getProductQty(j));
String amt = obdatin.catList[i].proList.getProductAmt(j);
PreviewCatNameTxt.append("\n"+amt+"\n\n");
System.out.println("prcaN"+obdatin.catList[i].proList.getProductName(j));
System.out.println("prcaQ"+obdatin.catList[i].proList.getProductQty(j));
System.out.println("prcaA"+obdatin.catList[i].proList.getProductAmt(j));
}
Please remove TextView array and all those things. Please reply your feedback.
I am not entirely sure what you are trying to do.Why do you use System.out.println?
If I got it right you want a larger text to be put in TextView separated by newlines after each loop of the for. One way to do it is to have a String which you fill with every iteration, also adding a newline after each iteration and after the for setting the text.
So, you could have something like this:
String catList="";
for(int i=0;i<obdatin.catCount;i++){
if(obdatin.catList[i].isTotQtyNonZero(0)){
catList = catList + obdatin.catList[i].getCategoryName(0) + "\n";
}
int cnt =obdatin.catList[i].proList.getNumProduct();
PreviewCatNameTxt[i] = (TextView) findViewById(R.id.textViewh);
PreviewCatNameTxt[i].setText(catList);
Also, if you want to log something you should od to use LogCat. You can read about Reading and Writing in LogCat here and here.
Good luck!
PreviewNameTxt[j] = (TextView)findViewById(R.id.textView1);
PreviewQtyTxt[j] = (TextView)findViewById(R.id.textView2);
PreviewAmtTxt[j] = (TextView)findViewById(R.id.textView3);
String cat="",obtaincat="",amt="";
for(int j = 0; j<cnt; j++) {
if(obdatin.catList[i].proList.isQtyNonZero(j)){
cat = cat + obdatin.catList[i].getCategoryName(j) + "\n";
obtaincat=btaincat+obdatin.catList[i].proList.getProductQty(j)+"\n";
amt =amt+ obdatin.catList[i].proList.getProductAmt(j)+"\n";
}
}
PreviewNameTxt[j].setText(cat);
PreviewQtyTxt[j].setText(obtaincat);
PreviewAmtTxt[j].setText(amt);
Change to Logcat :
Log.w("","prcaN"+obdatin.catList[i].proList.getProductName(j));
Log.w("","prcaQ"+obdatin.catList[i].proList.getProductQty(j));
Log.w("","prcaA"+obdatin.catList[i].proList.getProductAmt(j));
I am trying to colour text for my android app depending on a value entered into the SQLite database. I have set up 3 textviews and have different text colours for all of these.
The code looks like this
String arr[] = data.split("..\n\n");
for(int i = 0; i < arr.length; i++)
{
System.out.println("arr["+i+"] = " + arr[i].trim());
if(arr[i].contains("High Severity"))
{
// String highArr = arr[i];
textView.setVisibility(View.VISIBLE);
textView.setText(highArr+"\n");
textView.setTextColor(Color.RED);
}
else if(arr[i].contains("Low Severity"))
{
textView3.setVisibility(View.VISIBLE);
textView3.setText(arr[i]+"\n");
textView3.setTextColor(Color.GREEN);
}
else if(arr[i].contains("Medium Severity"))
{
textView2.setVisibility(View.VISIBLE);
textView2.setText(arr[i]+"\n");
textView2.setTextColor(Color.rgb(255, 136, 0));
}
}
I have parsed the string which has all the values for my database table, but when I try my for loop it only prints out the latest entered values.
It looks like it could be that your arr[] is only got one entry in it. You should use the debugger to see how many times your loop actually runs and what the actual lenght of the array is.
EDIT:
I just realized that you only have three textviews. Of course the textview is displaying the last one because each time your calling the settext method its reseting the text. There are a few ways to do what you are trying but Im not sure how your app is set up. You should try dynamically creating the textview, setting the text to it, then adding that textview to a linearlayout or relativelayout that you have set up. Or you could do this inside of a listview using a custom implementation of a baseadapter or any of the other adapters depending on your needs.
I would change my code like this:
String arr[] = data.split("..\n\n");
String Hseverity = "", Mseverity = "", Lseverity = "";
for(int i = 0; i < arr.length; i++)
{
System.out.println("arr["+i+"] = " + arr[i].trim());
if(arr[i].contains("High Severity"))
{
// String highArr = arr[i];
textView.setVisibility(View.VISIBLE);
Hseverity += arr[i] +"\n";
textView.setText(Hseverity);//do this to save each High Severity entry
//same for the other severity levels
textView.setTextColor(Color.RED);
}
else if(arr[i].contains("Low Severity"))
{
textView3.setVisibility(View.VISIBLE);
Lseverity += arr[i]+"\n";
textView3.setText(Lseverity);
textView3.setTextColor(Color.GREEN);
}
else if(arr[i].contains("Medium Severity"))
{
textView2.setVisibility(View.VISIBLE);
Mseverity += arr[i] + "\n";
textView2.setText(Mseverity);
textView2.setTextColor(Color.rgb(255, 136, 0));
}
}
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
Hi im creating a list that has headers of dates and then content underneath. i have a JSON feed that contains fixtures inside each is an array containing the data for each fixture one string i need is matchdate to create my headers however if i was to just run through it it will create multiple instances of the same match day so id have 3 headers with the same date for example. how can i extract that information and the create another array that says if this date already exists go through the next one and so on. i know it's pretty specific question but if someone could at least point me in the right direction. thanks in advance.
heres my feed
fixturesArray = [{"awayteam":"Team 1","comp":"LGE","location":null,"attendance":null,"awayscore":null,"division":"Testing 1","homescore":null,"fixture_note":null,"kickoff":"15:30:00","awayteam_id":"64930","matchdate":"2012-07-07","awaypens":null,"homepens":null,"division_id":"5059","hometeam":"Team 3","hometeam_id":"64932"},{"awayteam":"Team 2","comp":"LGE","location":null,"attendance":null,"awayscore":null,"division":"Testing 1","homescore":null,"fixture_note":null,"kickoff":"15:00:00","awayteam_id":"64931","matchdate":"2012-07-07","awaypens":null,"homepens":null,"division_id":"5059","hometeam":"Team 4","hometeam_id":"64933"},{"awayteam":"Team 4","comp":"LGE","location":null,"attendance":null,"awayscore":null,"division":"Testing 1","homescore":null,"fixture_note":null,"kickoff":"15:00:00","awayteam_id":"64933","matchdate":"2012-07-14","awaypens":null,"homepens":null,"division_id":"5059","hometeam":"Team 1","hometeam_id":"64930"}]
heres what i have tried so far
Log.v("MyFix", "fixturesArray = " + fixturesArray);
if(fixturesArray.length() < 1){
TextView emptytext = (TextView) fixturesView.findViewById(R.id.TextView02);
emptytext.setText("No Upcoming Fixtures Available");
}else{
try{
JSONArray datesArray = null;
fixturesInfo = null;
String matchDateTemp = "";
for(int t = 0; t < fixturesArray.length(); t++){
JSONObject matchDateDict = fixturesArray.getJSONObject(t);
String matchDate = matchDateDict.getString("matchdate");
JSONArray matchdates = matchdates.put(matchDate);
Log.v("MyFix", "matchdate = " + matchDate);
tempArray.put(t, fixturesArray);
fixturesInfo.put(matchDate, tempArray);
}
Log.v("MyFix", "fixturesInfo = " + fixturesInfo);
Log.v("MyFix", "tempArray = " + tempArray);
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Just get the piece of data then iterate through the new arraylist checking for a duplicate before adding.
int matchCount=0; // set count of matches to 0
String matchDate = matchDateDict.getString("matchdate"); // get the data to compare
for (int i = 0; i < uniqueDateArrayList.size(); i++){ // set loop to iterate through unique date arraylist
if (matchDate.equals(uniqueDateArray[i])){ // compare the date to the date stored at position i
matchCount++; // if it matches increase the match count
}
}
if (matchCount == 0) {
uniqueDateArrayList.add(matchDate) // if there is no matching date, add it to the unique date arraylist
}
That should give you an array list containing all of the unique dates in your data.