Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.24 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.                
  81.             }
  82.  
  83.             ListAdapter listAdapter = new ListAdapter(getApplicationContext(), R.layout.row_layout, values);
  84.             ListView listView = new ListView(this);
  85.             listView.setMinimumHeight(200);
  86.             listView.setMinimumWidth(200);
  87.             listView.setAdapter(listAdapter);
  88.  
  89.             LinearLayout myLayout = findViewById(R.layout.activity_table_view);
  90.             myLayout.addView(listView); //yo line chai khoi! listview lai view ma add nai nagari kasari dekhauxa?
  91.  
  92.         } catch (JSONException e) {
  93.             e.printStackTrace();
  94.         }
  95.  
  96.     }
  97.  
  98.     public class ListAdapter extends ArrayAdapter<JSONObject> {
  99.         // Your sent context
  100.         private Context context;
  101.         // Your custom values for the spinner (User)
  102.         private ArrayList<JSONObject> values;
  103.  
  104.         public ListAdapter(Context context, int textViewResourceId,
  105.                            ArrayList<JSONObject> values) {
  106.             super(context, textViewResourceId, values);
  107.             this.context = context;
  108.             this.values = values;
  109.         }
  110.  
  111.         public int getCount() {
  112.             return values.size();
  113.         }
  114.  
  115.         public long getItemId(int position) {
  116.             return position;
  117.         }
  118.  
  119.         @Override
  120.         public View getView(int position, View convertView, ViewGroup parent) {
  121.             View view = convertView;
  122.  
  123.             if (view == null) {
  124.                 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  125.                 view = inflater.inflate(R.layout.row_layout, parent, false);
  126.             }
  127.  
  128.             TextView tableGroupText = (TextView) view.findViewById(R.id.textview_table);
  129.             TextView amountText = (TextView) view.findViewById(R.id.textview_amount);
  130.             TextView packsText = (TextView) view.findViewById(R.id.textview_packs);
  131.  
  132.             try {
  133.                 tableGroupText.setText(values.get(position).getString("tableGroup"));
  134.                 amountText.setText(values.get(position).getString("amount"));
  135.                 packsText.setText(values.get(position).getString("packs"));
  136.  
  137.             } catch (Exception e) {
  138.                 e.printStackTrace();
  139.             }
  140.             return convertView;
  141.         }
  142.  
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement