title: How to change color of different field in json data
body: <p>I am using ...</p>
  
  <blockquote>
    <p>GUARDIAN_REQUEST_URL = <a href="http://content.guardianapis.com/search?from-date=2015-01-01&amp;order-by=newest&amp;show-fields=thumbnail&amp;q=android&amp;api-key=test" rel="nofollow noreferrer">http://content.guardianapis.com/search?from-date=2015-01-01&amp;order-by=newest&amp;show-fields=thumbnail&amp;q=android&amp;api-key=test</a></p>
  </blockquote>
  
  <p>...  to parse JSON data. My app runs perfectly, but I want to change the color of different <code>sectionName</code> for e.g. for Technology = pink, for Society = blue.  Below is the screenshot of the app. Can anyone tell me how to change color of different sections?</p>
  
  <p><a href="https://i.stack.imgur.com/h6PkD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/h6PkD.png" alt="Screen Shot"></a></p>
  
  <p><strong>NewsAdapter.java:</strong></p>
  
  <pre><code>public class NewsAdapter extends ArrayAdapter&lt;News&gt; {
  Context mContext;
  
  public NewsAdapter(Context context, ArrayList&lt;News&gt; news) {
      super(context, 0, news);
      mContext = context;
  }
  
  // "view holder" that holds references to each subview
  private class ViewHolder {
  
      private final ImageView newsImage;
      private final TextView sectionName;
      private final TextView webTitle;
  
      public ViewHolder(ImageView newsImage, TextView sectionName, TextView webTitle) {
          this.newsImage = newsImage;
          this.sectionName = sectionName;
          this.webTitle = webTitle;
      }
  }
  
  @NonNull
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      News currentNews = getItem(position);
  
      // Check if there is an existing view (called convertView) that we can reuse,
      // otherwise, if convertView is null, then inflate a new list item layout.
      if (convertView == null) {
          final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
          convertView = layoutInflater.inflate(R.layout.news_list_item, null);
  
          final ImageView newsImage = (ImageView) convertView.findViewById(R.id.image_view);
          final TextView sectionName = (TextView) convertView.findViewById(R.id.section_name);
          final TextView webTitle = (TextView) convertView.findViewById(R.id.web_title);
  
          final ViewHolder viewHolder = new ViewHolder(newsImage, sectionName, webTitle);
          convertView.setTag(viewHolder);
      }
  
      final ViewHolder viewHolder = (ViewHolder) convertView.getTag();
      Picasso.with(mContext)
              .load(currentNews.getThumbnail())
              .resize(100, 120)
              .into(viewHolder.newsImage);
      viewHolder.sectionName.setText(currentNews.getSectionName());
      viewHolder.webTitle.setText(currentNews.getWebTitle());
  
      return convertView;
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>QueryUtils.java:</strong></p>
  
  <pre><code>public class QueryUtils {
  
  public QueryUtils() {
  
  }
  
  /**
   * Query the GUARDIAN dataset and return a list of {@link News} objects.
   */
  public static List&lt;News&gt; fetchNewsData(String requestUrl) {
  
      // Create URL object
      URL url = createUrl(requestUrl);
  
      // Perform HTTP request to the URL and receive a JSON response back
      String jsonResponse = null;
  
      try {
          jsonResponse = makeHttpRequest(url);
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      // Extract relevant fields from the JSON response and create a list of {@link News}s
      List&lt;News&gt; newses = extractFeatureFromjson(jsonResponse);
  
      return newses;
  }
  
  /**
   * Returns new URL object from the given string URL.
   */
  private static URL createUrl(String stringUrl) {
      URL url = null;
  
      try {
          url = new URL(stringUrl);
      } catch (MalformedURLException e) {
          e.printStackTrace();
      }
      return url;
  }
  
  /**
   * Make an HTTP request to the given URL and return a String as the response.
   */
  private static String makeHttpRequest(URL url) throws IOException {
      String jsonResponse = "";
  
      // If the URL is null, then return early.
      if (url == null) {
          return jsonResponse;
      }
  
      HttpURLConnection urlConnection = null;
      InputStream inputStream = null;
  
      try {
          urlConnection = (HttpURLConnection) url.openConnection();
          urlConnection.setReadTimeout(1000);
          urlConnection.setConnectTimeout(1500);
          urlConnection.setRequestMethod("GET");
          urlConnection.connect();
  
          // If the request was successful (response code 200),
          // then read the input stream and parse the response.
          if (urlConnection.getResponseCode() == 200) {
              inputStream = urlConnection.getInputStream();
              jsonResponse = readFromStream(inputStream);
          }
      } catch (IOException e){
  
      } finally {
          if (urlConnection != null) {
              urlConnection.disconnect();
          }
          if (inputStream != null) {
              // Closing the input stream could throw an IOException, which is why
              // the makeHttpRequest(URL url) method signature specifies than an IOException
              // could be thrown.
              inputStream.close();
          }
      }
      return jsonResponse;
  }
  
  /**
   * Convert the {@link InputStream} into a String which contains the
   * whole JSON response from the server.
   */
  private static String readFromStream(InputStream inputStream) throws IOException {
      StringBuilder output = new StringBuilder();
      if (inputStream != null) {
          InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
          BufferedReader reader = new BufferedReader(inputStreamReader);
          String line = reader.readLine();
          while (line != null) {
              output.append(line);
              line = reader.readLine();
          }
      }
      return output.toString();
  }
  
  /**
   * Return a list of {@link News} objects that has been built up from
   * parsing the given JSON response.
   */
  private static List&lt;News&gt; extractFeatureFromjson(String newsJson) {
      // If the JSON string is empty or null, then return early.
      if (TextUtils.isEmpty(newsJson)) {
          return null;
      }
  
      // Create an empty ArrayList that we can start adding news to
      List&lt;News&gt; newses = new ArrayList&lt;&gt;();
  
      try {
  
          JSONObject baseJsonResponse = new JSONObject(newsJson);
  
          JSONObject response = baseJsonResponse.getJSONObject("response");
  
          JSONArray newsArray = response.getJSONArray("results");
  
          for (int i = 0; i &lt; newsArray.length(); i++) {
  
              JSONObject currentNews = newsArray.getJSONObject(i);
  
              String sectionName = currentNews.getString("sectionName");
              Log.e("SECTION_NAME", sectionName);
  
              String webTitle = currentNews.getString("webTitle");
              Log.e("WEB_TITLE", webTitle);
  
              String webUrl = currentNews.getString("webUrl");
              Log.e("WEB_URL", webUrl);
  
              JSONObject fields = currentNews.getJSONObject("fields");
  
              if (fields.has("thumbnail")) {
  
                  String thumbnail = fields.getString("thumbnail");
                  Log.e("THUMBNAIL", thumbnail);
  
                  News news = new News(thumbnail, sectionName, webTitle, webUrl);
  
                  newses.add(news);
              }
          }
  
      } catch (JSONException e) {
          e.printStackTrace();
      }
  
      return newses;
  }
  }
  </code></pre>
  
  <p><strong>news_list_item.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:tools="http://schemas.android.com/tools"
  android:orientation="vertical"&gt;
  
  &lt;android.support.v7.widget.CardView
      android:id="@+id/cv"
      android:layout_width="170dp"
      android:layout_height="wrap_content"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:padding="16dp"&gt;
  
          &lt;ImageView
              android:id="@+id/image_view"
              android:layout_width="140dp"
              android:layout_height="200dp"
              android:layout_alignParentLeft="true"
              android:layout_alignParentTop="true"/&gt;
  
          &lt;TextView
              android:id="@+id/section_name"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAllCaps="true"
              android:background="@color/colorAccent"
              android:padding="5dp"
              android:textSize="9sp"
              android:textColor="@android:color/white"
              tools:text="technolgy"/&gt;
  
          &lt;TextView
              android:id="@+id/web_title"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="@android:color/black"
              android:layout_marginTop="@dimen/activity_horizontal_margin"
              tools:text="Amazon and Google fight crucial battle over voice recognition" /&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/android.support.v7.widget.CardView&gt;
  </code></pre>
  
  <p></p>