title: Parsing JSON Data to a list View
body: <p><strong>I am trying to parse the data into the list View in android</strong></p>
  
  <p><strong><code>JSON:: http://54.218.73.244:7000/</code></strong></p>
  
  <p><strong>It has a JSON output ::</strong> <code>[{"restaurantID":1,"restaurantNAME":"CopperChimney"},{"restaurantID":2,"restaurantNAME":"Aroy"},{"restaurantID":3,"restaurantNAME":"MarkBoulevard"}]</code></p>
  
  <ul>
  <li>I am trying to display the <code>CopperChimney, Aroy, MarkBoulevard</code> in a
  list view</li>
  <li>I figured some small parts but can someone help me fill
  <strong>AndroidJSONParsingActivity.java</strong> part of the code which invoolves putting data in a collection and displaying it</li>
  </ul>
  
  <p><strong>Any ideas ?</strong></p>
  
  <hr>
  
  <p><strong>JSONParser</strong></p>
  
  <pre><code>public class JSONParser {
  
      static InputStream is = null;
      static JSONArray jObj = null;
      static String json = "";
  
      // constructor
      public JSONParser() {
  
      }
  
      public JSONArray getJSONFromUrl(String url) {
  
          // Making HTTP request
          try {
              // defaultHttpClient
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(url);
  
              HttpResponse httpResponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();           
  
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              json = sb.toString();
          } catch (Exception e) {
              Log.e("Buffer Error", "Error converting result " + e.toString());
          }
  
          // try parse the string to a JSON object
          try {
              jObj = new JSONArray(json);
          } catch (JSONException e) {
              Log.e("JSON Parser", "Error parsing data " + e.toString());
          }
  
          // return JSON String
          return jObj;
  
      }
  }
  </code></pre>
  
  <p><strong>Item.java</strong></p>
  
  <pre><code>public class Item{
      private String Name;
  
      public Item(String name){
          this.Name = name;
      }
      public String getName(){
          return Name;
      }
  }
  </code></pre>
  
  <p><strong>ListAdapter.java</strong></p>
  
  <pre><code>public class ListAdapter extends ArrayAdapter&lt;Item&gt; {
      private List&lt;Item&gt; items;
      public ListAdapter(Context context, int resource, List&lt;Item&gt; items) {
          super(context, resource, items);
          this.items = items;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View v = convertView;
          TextView tt = null;
  
          if (v == null) {
              LayoutInflater vi;
              vi = LayoutInflater.from(getContext());
              v = vi.inflate(R.layout.itemlistrow, null);
              tt = (TextView) v.findViewById(R.id.RestaurantNameID);
          }
  
          Item p = items.get(position);
          if (p != null) {
              if (tt != null) {
                  tt.setText(""+p.getName());
              }
  
          }
          return v;
      }
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" &gt;
  
      &lt;ListView
          android:id="@+id/listViewID"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" 
          android:layout_gravity="center"&gt;
      &lt;/ListView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>itemlistrow.xml</strong></p>
  
  <pre><code>&lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:layout_height="wrap_content" android:orientation="vertical"
               android:layout_width="fill_parent"&gt;
  
      &lt;TableRow android:layout_width="fill_parent"
                android:id="@+id/TableRow01"
                android:layout_height="wrap_content"&gt;
  
          &lt;TextView
                  android:id="@+id/RestaurantNameID"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:text="name" android:textStyle="bold"
                  android:gravity="left"
                  android:layout_weight="1"
                  android:typeface="monospace"
                  android:height="40sp"/&gt;
      &lt;/TableRow&gt;
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p><strong>AndroidJSONParsingActivity.java</strong></p>
  
  <pre><code>public class AndroidJSONParsingActivity extends Activity {
  
  
      private static String url = "http://54.218.73.244:7000/";
  
          //
          //
          //
          //
                /------ CODE -Trying to ADD ------ /
          //
          //
  }
  </code></pre>
  
  <p><strong>CODE I TRIED FOR AndroidJSONParsingActivity.java</strong></p>
  
  <pre><code>public class AndroidJSONParsingActivity extends Activity {
  
      // url to make request
      private static String url = "http://54.218.73.244:7000/";
      List&lt;Item&gt; yourData = new ArrayList&lt;Item&gt;();
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          // Creating JSON Parser instance
          JSONParser jParser = new JSONParser();
  
          // getting JSON string from URL
          JSONArray json = jParser.getJSONFromUrl(url);
  
          try {
              for (int i = 0; i &lt; json.length(); i++) {
                  JSONObject c = json.getJSONObject(i);
  
                  // Storing each json item in variable
                  String NAME=c.getString("restaurantNAME");
  
                  yourData.add(new Item(NAME));
              }
          } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          ListView yourListView = (ListView) findViewById(R.id.listViewID);
          ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, yourData);
          yourListView.setAdapter(customAdapter);
          yourListView.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
                  // When clicked, show a toast with the TextView text
                  if(position == 0)
                  {
                      //code specific to first list item    
                      Intent myIntent = new Intent(AndroidJSONParsingActivity.this,Employee1.class);
                      startActivity(myIntent);
                  }else if(position == 1)
                  {
                      Intent myIntent = new Intent(AndroidJSONParsingActivity.this,Employee2.class);
                      startActivity(myIntent);                    
                  }
  
              }
          });
      }
   }
  </code></pre>