-3

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;
            }
        });
  • 3
    What is the error that you get? – Cortex Jun 04 '20 at 16:54
  • 3
    paste the exception message too... please – ΦXocę 웃 Пepeúpa ツ Jun 04 '20 at 16:54
  • It shows loading dialog for few seconds and then screen goes blank. Later, shows app stopped working – Pratyush Jun 04 '20 at 16:57
  • No error in building the app. Problem after running the activity – Pratyush Jun 04 '20 at 16:59
  • 1
    Please have a look at [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) to get the crash log that people are asking for. – Markus Kauppinen Jun 04 '20 at 17:00
  • Try printing the value of `check`, most probably it's not a numeric string. It's better to add a vlidation of this string before using it in the if statement. – Cortex Jun 04 '20 at 17:03
  • In locat it shows the error : java.lang.NumberFormatException: For input string: "" Problem lies in the If condition line. I don't know why?? what's wrong here – Pratyush Jun 04 '20 at 17:15
  • If you were given a string "" which number would you make of it? `Integer.parseInt()` can't parse an empty string. – Markus Kauppinen Jun 04 '20 at 17:23
  • Thanks @Markus. There was some null values and decimal values which was causing the error. Somehow I can't remove null and decimal values from my item list(accessing it from gsheet). Is there any other method other than 'Integer.parseInt' to deal with it while having null and decimal values.?? I can use float for decimal values. But what should be used for handling null ?? – Pratyush Jun 04 '20 at 17:40
  • What about a simple `if (check != null && !check.isEmpty() && Integer.parseInt(check) >= 10) {...}`? – Markus Kauppinen Jun 04 '20 at 18:15

1 Answers1

0

The Problem was the null string. So I modified the check part to handle null value issue, as shown below and now it's working.

// check price

            String checkPrice = item.get("RH");

            int check = 0;
            try
            {
                if(checkPrice != null)
                    check = Integer.parseInt(checkPrice);
            }
            catch (NumberFormatException e)
            {
                check = 0;
            }


                if (check>= 10) {
                    RH.setTextColor(Color.GREEN);
                } else {
                    RH.setTextColor(Color.RED);
                }
                RH.setText(item.get("RH"));

            return rowView;
        }
    });