Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TableViewActivity extends AppCompatActivity {
- String reportJson = "{\n" +
- " \"records\": [\n" +
- " {\n" +
- " \"tableGroup\": \"table1\",\n" +
- " \"amount\": \"4500\",\n" +
- " \"packs\": 10\n" +
- " },\n" +
- " {\n" +
- " \"tableGroup\": \"table2\",\n" +
- " \"amount\": \"500\",\n" +
- " \"packs\": 10\n" +
- " },\n" +
- " {\n" +
- " \"tableGroup\": \"table3\",\n" +
- " \"amount\": \"1500\",\n" +
- " \"packs\": 13\n" +
- " },\n" +
- " {\n" +
- " \"tableGroup\": \"table4\",\n" +
- " \"amount\": \"500\",\n" +
- " \"packs\": 12\n" +
- " },\n" +
- " {\n" +
- " \"tableGroup\": \"table5\",\n" +
- " \"amount\": \"2500\",\n" +
- " \"packs\": 10\n" +
- " },\n" +
- " {\n" +
- " \"tableGroup\": \"table6\",\n" +
- " \"amount\": \"500\",\n" +
- " \"packs\": 10\n" +
- " },\n" +
- " {\n" +
- " \"tableGroup\": \"table7\",\n" +
- " \"amount\": \"7500\",\n" +
- " \"packs\": 11\n" +
- " },\n" +
- " {\n" +
- " \"tableGroup\": \"table8\",\n" +
- " \"amount\": \"1500\",\n" +
- " \"packs\": 10\n" +
- " },\n" +
- " {\n" +
- " \"tableGroup\": \"table9\",\n" +
- " \"amount\": \"3500\",\n" +
- " \"packs\": 12\n" +
- " },\n" +
- " {\n" +
- " \"tableGroup\": \"table10\",\n" +
- " \"amount\": \"1500\",\n" +
- " \"packs\": 15\n" +
- " }\n" +
- " ]\n" +
- " }\n" +
- "}";
- // String tableGroup;
- // String amount;
- // String packs;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_table_view);
- try {
- JSONObject reader = new JSONObject(reportJson);
- JSONArray records = reader.getJSONArray("records");
- ArrayList<JSONObject> values = new ArrayList<>();
- for (int i = 0; i < records.length(); i++) {
- JSONObject record = records.getJSONObject(i);
- values.add(record);
- }
- ListAdapter listAdapter = new ListAdapter(getApplicationContext(), R.layout.row_layout, values);
- ListView listView = new ListView(this);
- listView.setMinimumHeight(200);
- listView.setMinimumWidth(200);
- listView.setAdapter(listAdapter);
- LinearLayout myLayout = findViewById(R.layout.activity_table_view);
- myLayout.addView(listView); //yo line chai khoi! listview lai view ma add nai nagari kasari dekhauxa?
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- public class ListAdapter extends ArrayAdapter<JSONObject> {
- // Your sent context
- private Context context;
- // Your custom values for the spinner (User)
- private ArrayList<JSONObject> values;
- public ListAdapter(Context context, int textViewResourceId,
- ArrayList<JSONObject> values) {
- super(context, textViewResourceId, values);
- this.context = context;
- this.values = values;
- }
- public int getCount() {
- return values.size();
- }
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View view = convertView;
- if (view == null) {
- LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- view = inflater.inflate(R.layout.row_layout, parent, false);
- }
- TextView tableGroupText = (TextView) view.findViewById(R.id.textview_table);
- TextView amountText = (TextView) view.findViewById(R.id.textview_amount);
- TextView packsText = (TextView) view.findViewById(R.id.textview_packs);
- try {
- tableGroupText.setText(values.get(position).getString("tableGroup"));
- amountText.setText(values.get(position).getString("amount"));
- packsText.setText(values.get(position).getString("packs"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return convertView;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement