我想通过应用条件来更改文本的颜色。我从杰森那里得到了文字。代码正常运行,没有,该代码末尾没有“检查颜色变化”部分。否则,它什么也不会返回。应用程序崩溃。有什么问题??
这是代码:
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;
}
});
答案 0 :(得分:0)
问题是空字符串。因此,我修改了检查部分以处理空值问题,如下所示,现在它可以正常工作了。
//查看价格
-legacy_icecast 1