I want to change the color of text by applying a condition. I'm getting the text from Json. Code is working fine without that "check for the color change" part present at the end of the code. Otherwise, It isn't returning anything. app crashes. What's the problem??
Here is the code :
ArrayList<HashMap<String, String>> list = new ArrayList<>();
try {
JSONObject jobj = new JSONObject(jsonResposnce);
JSONArray jarray = jobj.getJSONArray("items");
for (int i = 0; i < jarray.length(); i++) {
JSONObject jo = jarray.getJSONObject(i);
String itemName = jo.getString("itemName");
String Power = jo.getString("Power");
String RH = jo.getString("RH");
String DAILYDATE = jo.getString("DAILYDATE");
String TPH = jo.getString("TPH");
String Production = jo.getString("Production");
HashMap<String, String> item = new HashMap<>();
item.put("itemName", itemName);
item.put("Power", Power);
item.put("RH", RH);
item.put("TPH", TPH);
item.put("DAILYDATE", DAILYDATE);
item.put("Production", Production);
list.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
listView.setAdapter(new ArrayAdapter<HashMap<String, String>>(this, R.layout.listdaily_row,
R.id.yx_item_name, list) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = super.getView(position, convertView, parent);
final HashMap<String, String> item = getItem(position);
TextView itemName = rowView.findViewById(R.id.yx_item_name);
itemName.setText(item.get("itemName"));
TextView Power = rowView.findViewById(R.id.yx_power);
Power.setText(item.get("Power"));
TextView RH = rowView.findViewById(R.id.yx_rh);
TextView TPH = rowView.findViewById(R.id.yx_tph);
TPH.setText(item.get("TPH"));
TextView DAILYDATE = rowView.findViewById(R.id.yx_date);
DAILYDATE.setText(item.get("DAILYDATE"));
TextView Production = rowView.findViewById(R.id.yx_prod);
Production.setText(item.get("Production"));
// check for the color change
String check = item.get("RH");
if (check != null && Integer.parseInt(check) >= 10) {
RH.setTextColor(Color.GREEN);
} else {
RH.setTextColor(Color.RED);
}
RH.setText(item.get("RH"));
return rowView;
}
});