Q:

How to change color of different field in json data

I am using ...

GUARDIAN_REQUEST_URL = http://content.guardianapis.com/search?from-date=2015-01-01&order-by=newest&show-fields=thumbnail&q=android&api-key=test

...  to parse JSON data. My app runs perfectly, but I want to change the color of different sectionName 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?

NewsAdapter.java:
public class NewsAdapter extends ArrayAdapter<News> {
Context mContext;

public NewsAdapter(Context context, ArrayList<News> 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;
}

}
QueryUtils.java:
public class QueryUtils {

public QueryUtils() {

}

/**
 * Query the GUARDIAN dataset and return a list of {@link News} objects.
 */
public static List<News> 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<News> 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<News> 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<News> newses = new ArrayList<>();

    try {

        JSONObject baseJsonResponse = new JSONObject(newsJson);

        JSONObject response = baseJsonResponse.getJSONObject("response");

        JSONArray newsArray = response.getJSONArray("results");

        for (int i = 0; i < 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;
}
}

news_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<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">

<android.support.v7.widget.CardView
    android:id="@+id/cv"
    android:layout_width="170dp"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="140dp"
            android:layout_height="200dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"/>

        <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"/>

        <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" />

    </LinearLayout>

</android.support.v7.widget.CardView>

A:

You can change color based on your sectioned by the below changge: 
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    News currentNews = getItem(position);
if(currentNews.getSectionName().equals("Technology"))
{
  viewHolder.sectionName.setBackgroundResource(R.color.yourColor);
}

    // 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;
}

Switch case for color change :
 switch (currentNews.getSectionName()) {
         case "Technology":
            viewHolder.sectionName.setBackgroundResource(R.color.yourColor);// change color 
             break;

         default:
             throw new IllegalArgumentException("Invalid Color Set " + currentNews.getSectionName());
     }

