-2

How to turn this JSON:

{"GetReportResult":
  [
   {"bulan":"4","total":"2448","type":"CHEESE1K"}, 
   {"bulan":"4","total":"572476","type":"ESL"},
   {"bulan":"4","total":"46008","type":"ESL500ML"},
   {"bulan":"5","total":"5703","type":"CHEESE1K"},
   {"bulan":"5","total":"648663","type":"ESL"},
   {"bulan":"5","total":"51958","type":"WHP"},
   {"bulan":"6","total":"6190","type":"CHEESE1K"},
   {"bulan":"6","total":"443335","type":"ESL"},
   {"bulan":"6","total":"30550","type":"ESL500ML"},
  ]
}

Into table like this on Android:

---------------------------------------------
|   type   | total1  |   total2   |  total3 |
---------------------------------------------
| CHEESE1K | 6190    |   5703     |  2448   |
| ESL      | 443335  |   648663   |  572476 |
| WHP      | 30550   |   51958    |  46008  |
---------------------------------------------

What is the best way to do that? Thank you :)

here is the code that I've made so far

blankon91
  • 521
  • 3
  • 15
  • 39

2 Answers2

1

In your code, you are passing mylist to ListAdapter, but you are not adding map to mylist anywhere.

Chandra
  • 1,317
  • 11
  • 14
1

easy way could be

  • parse JSON string using GSON

like this:

    String json = "{\"GetReportResult\":" + 
"  [" + 
"   {\"bulan\":\"4\",\"total\":\"2448\",\"type\":\"CHEESE1K\"}, " + 
"   {\"bulan\":\"4\",\"total\":\"572476\",\"type\":\"ESL\"}," + 
"   {\"bulan\":\"4\",\"total\":\"46008\",\"type\":\"ESL500ML\"}," + 
"   {\"bulan\":\"5\",\"total\":\"5703\",\"type\":\"CHEESE1K\"}," + 
"   {\"bulan\":\"5\",\"total\":\"648663\",\"type\":\"ESL\"}," + 
"   {\"bulan\":\"5\",\"total\":\"51958\",\"type\":\"WHP\"}," + 
"   {\"bulan\":\"6\",\"total\":\"6190\",\"type\":\"CHEESE1K\"}," + 
"   {\"bulan\":\"6\",\"total\":\"443335\",\"type\":\"ESL\"}," + 
"   {\"bulan\":\"6\",\"total\":\"30550\",\"type\":\"ESL500ML\"}," + 
"  ]" + 
"}"; 
ReportResults reports = new Gson().fromJson(json, ReportResults.class);
List<ReportResult> results = reports.getGetReportResult();
for(ReportResult result:results)System.out.println(result);

The ReportResults class:

package com.yourcomp.test;
import java.util.List;

public class ReportResults {

    private List<ReportResult> GetReportResult;

    /**
     * Gets the getReportResult.
     * 
     * @return <tt> the getReportResult.</tt>
     */
    public List<ReportResult> getGetReportResult() {
        return GetReportResult;
    }

    /**
     * Sets the getReportResult.
     *
     * @param getReportResult <tt> the getReportResult to set.</tt>
     */
    public void setGetReportResult(List<ReportResult> getReportResult) {
        GetReportResult = getReportResult;
    }


}

The ReportResult class:

package com.yourcomp.test;

public class ReportResult {

    private String bulan; 
    private String total;
    private String type;
    /**
     * Gets the bulan.
     * 
     * @return <tt> the bulan.</tt>
     */
    public String getBulan() {
        return bulan;
    }
    /**
     * Sets the bulan.
     *
     * @param bulan <tt> the bulan to set.</tt>
     */
    public void setBulan(String bulan) {
        this.bulan = bulan;
    }
    /**
     * Gets the total.
     * 
     * @return <tt> the total.</tt>
     */
    public String getTotal() {
        return total;
    }
    /**
     * Sets the total.
     *
     * @param total <tt> the total to set.</tt>
     */
    public void setTotal(String total) {
        this.total = total;
    }
    /**
     * Gets the type.
     * 
     * @return <tt> the type.</tt>
     */
    public String getType() {
        return type;
    }
    /**
     * Sets the type.
     *
     * @param type <tt> the type to set.</tt>
     */
    public void setType(String type) {
        this.type = type;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "ReportResult [bulan=" + bulan + ", total=" + total + ", type="
                + type + "]";
    }


}

Then use the parsed result to show in TableLayout.

sunil
  • 6,444
  • 1
  • 32
  • 44
  • 1
    another thing i noted was: you would need 'WHP' instead of 'ESL500ML' rt ? – sunil Jun 28 '12 at 08:19
  • 1
    i have tried the UI portion also. sometimes it may help you to improve the UI. http://www.pastie.org/4164695 – sunil Jun 28 '12 at 09:11
  • no, I have not try that before. Ok, I'll try to use it :) thank you very much – blankon91 Jun 28 '12 at 09:23
  • wait, I get this error on my log `Could not find class 'com.google.gson.Gson', referenced from method report.weeklyflash.WeeklyFlashIdActivity.onCreate` why I get that error? – blankon91 Jun 28 '12 at 09:53
  • you have to download the gson binary jar from here http://google-gson.googlecode.com/files/google-gson-2.2.1-release.zip, add it to the libs folder and then add this to the project's build path – sunil Jun 28 '12 at 11:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13191/discussion-between-blankon91-and-sunil) – blankon91 Jun 29 '12 at 00:24
  • I've add Gson on my libary and I can compile it, but when I run it, there is error message in my log that says `Could not find class 'com.google.gson.Gson', referenced from method report.weeklyflash.WeeklyFlashIdActivity.onCreate` – blankon91 Jun 29 '12 at 00:32