Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.05 KB | None | 0 0
  1. public class TableViewActivity extends AppCompatActivity {
  2.  
  3.     String reportJson = "{\n" +
  4.             "      \"records\": [\n" +
  5.             "         {\n" +
  6.             "            \"tableGroup\": \"table1\",\n" +
  7.             "            \"amount\": \"4500\",\n" +
  8.             "            \"packs\": 10\n" +
  9.             "         },\n" +
  10.             "         {\n" +
  11.             "            \"tableGroup\": \"table2\",\n" +
  12.             "            \"amount\": \"500\",\n" +
  13.             "            \"packs\": 10\n" +
  14.             "         },\n" +
  15.             "         {\n" +
  16.             "            \"tableGroup\": \"table3\",\n" +
  17.             "            \"amount\": \"1500\",\n" +
  18.             "            \"packs\": 13\n" +
  19.             "         },\n" +
  20.             "         {\n" +
  21.             "            \"tableGroup\": \"table4\",\n" +
  22.             "            \"amount\": \"500\",\n" +
  23.             "            \"packs\": 12\n" +
  24.             "         },\n" +
  25.             "         {\n" +
  26.             "            \"tableGroup\": \"table5\",\n" +
  27.             "            \"amount\": \"2500\",\n" +
  28.             "            \"packs\": 10\n" +
  29.             "         },\n" +
  30.             "         {\n" +
  31.             "            \"tableGroup\": \"table6\",\n" +
  32.             "            \"amount\": \"500\",\n" +
  33.             "            \"packs\": 10\n" +
  34.             "         },\n" +
  35.             "         {\n" +
  36.             "            \"tableGroup\": \"table7\",\n" +
  37.             "            \"amount\": \"7500\",\n" +
  38.             "            \"packs\": 11\n" +
  39.             "         },\n" +
  40.             "         {\n" +
  41.             "            \"tableGroup\": \"table8\",\n" +
  42.             "            \"amount\": \"1500\",\n" +
  43.             "            \"packs\": 10\n" +
  44.             "         },\n" +
  45.             "         {\n" +
  46.             "            \"tableGroup\": \"table9\",\n" +
  47.             "            \"amount\": \"3500\",\n" +
  48.             "            \"packs\": 12\n" +
  49.             "         },\n" +
  50.             "         {\n" +
  51.             "            \"tableGroup\": \"table10\",\n" +
  52.             "            \"amount\": \"1500\",\n" +
  53.             "            \"packs\": 15\n" +
  54.             "         }\n" +
  55.             "      ]\n" +
  56.             "   }\n" +
  57.             "}";
  58.  
  59. //    String tableGroup;
  60. //    String amount;
  61. //    String packs;
  62.  
  63.     @Override
  64.     protected void onCreate(Bundle savedInstanceState) {
  65.         super.onCreate(savedInstanceState);
  66.         setContentView(R.layout.activity_table_view);
  67.  
  68.         try {
  69.             JSONObject reader = new JSONObject(reportJson);
  70.             JSONArray records = reader.getJSONArray("records");
  71.  
  72.             ArrayList<JSONObject> values = new ArrayList<>();
  73.  
  74.             for (int i = 0; i < records.length(); i++) {
  75.  
  76.                 JSONObject record = records.getJSONObject(i);
  77.  
  78.                 values.add(record);
  79.  
  80.                 ListAdapter listAdapter = new ListAdapter(getApplicationContext(), R.layout.row_layout, values);
  81.                 ListView listView = new ListView(this);
  82.                 listView.setMinimumHeight(200);
  83.                 listView.setMinimumWidth(200);
  84.                 listView.setAdapter(listAdapter);
  85.  
  86.             }
  87.  
  88.  
  89.         } catch (JSONException e) {
  90.             e.printStackTrace();
  91.         }
  92.  
  93.     }
  94.  
  95.     public class ListAdapter extends ArrayAdapter<JSONObject> {
  96.         // Your sent context
  97.         private Context context;
  98.         // Your custom values for the spinner (User)
  99.         private ArrayList<JSONObject> values;
  100.  
  101.         public ListAdapter(Context context, int textViewResourceId,
  102.                            ArrayList<JSONObject> values) {
  103.             super(context, textViewResourceId, values);
  104.             this.context = context;
  105.             this.values = values;
  106.         }
  107.  
  108.         public int getCount() {
  109.             return values.size();
  110.         }
  111.  
  112.         public long getItemId(int position) {
  113.             return position;
  114.         }
  115.  
  116.         @Override
  117.         public View getView(int position, View convertView, ViewGroup parent) {
  118.             View view = convertView;
  119.  
  120.             if (view == null) {
  121.                 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  122.                 view = inflater.inflate(R.layout.row_layout, parent, false);
  123.             }
  124.  
  125.             TextView tableGroupText = (TextView) view.findViewById(R.id.textview_table);
  126.             TextView amountText = (TextView) view.findViewById(R.id.textview_amount);
  127.             TextView packsText = (TextView) view.findViewById(R.id.textview_packs);
  128.  
  129.             try {
  130.                 tableGroupText.setText(values.get(position).getString("tableGroup"));
  131.                 amountText.setText(values.get(position).getString("amount"));
  132.                 packsText.setText(values.get(position).getString("packs"));
  133.  
  134.             } catch (Exception e) {
  135.                 e.printStackTrace();
  136.             }
  137.             return convertView;
  138.         }
  139.  
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement