package com.example.administrator.busapp;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    BusAdapter adapter;
    private static ArrayList<Bus> arrList;
    private EditText edtInput;
    private Button btnTim;
    private ListView lvBus;

    private String strJSON =
            "{\"Bus\":[\n" +
                    "   {\n" +
                    "      \"bus_id\":1,\n" +
                    "      \"bus_name\":\"Bến Thành - BX Chợ Lớn\"\n" +
                    "   },\n" +
                    "   {\n" +
                    "      \"bus_id\":2,\n" +
                    "      \"bus_name\":\"Bến Thành - BX Miền Tây\"\n" +
                    "   },\n" +
                    "   {\n" +
                    "      \"bus_id\":4,\n" +
                    "      \"bus_name\":\"Bến Thành - Cộng Hòa - An Sương\"\n" +
                    "   },\n" +
                    "   {\n" +
                    "      \"bus_id\":3,\n" +
                    "      \"bus_name\":\"Bến Thành - Thạnh Lộc\"\n" +
                    "   },\n" +
                    "   {\n" +
                    "      \"bus_id\":5,\n" +
                    "      \"bus_name\":\"Bến xe Chợ Lớn - Biên Hòa\"\n" +
                    "   }\n" +
                    "]}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        arrList = new ArrayList<Bus>();
        edtInput = (EditText) findViewById(R.id.editTextSearch);
        btnTim = (Button) findViewById(R.id.btnTim);
        lvBus = (ListView) findViewById(R.id.listViewBus);

        try {
            JSONObject jsonObject = new JSONObject(strJSON);
            JSONArray jsonArray = jsonObject.optJSONArray("Bus");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject items = jsonArray.getJSONObject(i);

                String id = items.optString("bus_id").toString();
                String name = items.optString("bus_name").toString();

                Bus bus = new Bus(id, name);
                arrList.add(bus);

                adapter = new BusAdapter(MainActivity.this, R.layout.layout_item_listview, arrList);
                lvBus.setAdapter(adapter);
            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }

    }



}

@@ -829,1009 +829,8 @@
 s;%0A%0A
-    private String strJSON =%0A            %22%7B%5C%22Bus%5C%22:%5B%5Cn%22 +%0A                    %22   %7B%5Cn%22 +%0A                    %22      %5C%22bus_id%5C%22:1,%5Cn%22 +%0A                    %22      %5C%22bus_name%5C%22:%5C%22B%E1%BA%BFn Th%C3%A0nh - BX Ch%E1%BB%A3 L%E1%BB%9Bn%5C%22%5Cn%22 +%0A                    %22   %7D,%5Cn%22 +%0A                    %22   %7B%5Cn%22 +%0A                    %22      %5C%22bus_id%5C%22:2,%5Cn%22 +%0A                    %22      %5C%22bus_name%5C%22:%5C%22B%E1%BA%BFn Th%C3%A0nh - BX Mi%E1%BB%81n T%C3%A2y%5C%22%5Cn%22 +%0A                    %22   %7D,%5Cn%22 +%0A                    %22   %7B%5Cn%22 +%0A                    %22      %5C%22bus_id%5C%22:4,%5Cn%22 +%0A                    %22      %5C%22bus_name%5C%22:%5C%22B%E1%BA%BFn Th%C3%A0nh - C%E1%BB%99ng H%C3%B2a - An S%C6%B0%C6%A1ng%5C%22%5Cn%22 +%0A                    %22   %7D,%5Cn%22 +%0A                    %22   %7B%5Cn%22 +%0A                    %22      %5C%22bus_id%5C%22:3,%5Cn%22 +%0A                    %22      %5C%22bus_name%5C%22:%5C%22B%E1%BA%BFn Th%C3%A0nh - Th%E1%BA%A1nh L%E1%BB%99c%5C%22%5Cn%22 +%0A                    %22   %7D,%5Cn%22 +%0A                    %22   %7B%5Cn%22 +%0A                    %22      %5C%22bus_id%5C%22:5,%5Cn%22 +%0A                    %22      %5C%22bus_name%5C%22:%5C%22B%E1%BA%BFn xe Ch%E1%BB%A3 L%E1%BB%9Bn - Bi%C3%AAn H%C3%B2a%5C%22%5Cn%22 +%0A                    %22   %7D%5Cn%22 +%0A                    %22%5D%7D%22;%0A%0A
     
@@ -1202,24 +1202,95 @@
 tViewBus);%0A%0A
+        readDataBusJSON();%0A    %7D%0A%0A    private void readDataBusJSON() %7B%0A
         try 
@@ -1342,16 +1342,28 @@
 NObject(
+DataBusJSON.
 strJSON)
@@ -1409,18 +1409,18 @@
 nObject.
-op
+ge
 tJSONArr
@@ -1580,34 +1580,34 @@
 ring id = items.
-op
+ge
 tString(%22bus_id%22
@@ -1660,10 +1660,10 @@
 ems.
-op
+ge
 tStr
@@ -2020,16 +2020,13 @@
   %7D%0A
-%0A
     %7D%0A%0A
-%0A%0A
 %7D%0A

Load JSON on Listview