简体   繁体   中英

How to change color of textView in android listView on comparing JSON data?

I am fetching JSON Data to listView successfully, but i want to change the color of the textView in that listView by comparing JSON data. For ex-

if in JSON {"status":"approved"} - green color else if {"status" : "disapproved"} - red color

I'm able to change it using getView in SimpleAdapter but not able to compare strings there. when i print the status, its is only showing "approved". i can't filter "disapproved" in else if condition.

 SimpleAdapter adapter = new SimpleAdapter(getActivity(), mylist, R.layout.list_leave_approval,
                        new String[]{
                                TAG_STATUS
                                },
                        new int[]{

                                R.id.status}) {
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        //return super.getView(position, convertView, parent);

                        View v = convertView;
                        if (v == null) {
                            //  View v = convertView;
                            LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            v = vi.inflate(R.layout.list_leave_approval, null);

                            TextView test = (TextView) v.findViewById(R.id.status);


                            //  String str = jsonObject.getString("status");

                            Log.d("Day", "get view STATUS: " + strStatus);
                            // jsonObject = jsonArray.getJSONObject(i);

                            if (strStatus.equals(" Approved ")) {
                                test.setBackgroundColor(getResources().getColor(R.color.green));
                                // txtStatus.setText("DEC");

                            } else if (strStatus.equals(" Disapproved ")) {
                                test.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));

                            }
                        }

                        return super.

                                getView(position, v, parent);

                    }
                };

                list.setAdapter(adapter);

You can do like this.

Here string will be your parsed json string.

  JSONObject jObj = new JSONObject(String);
    if (jObj.getString("status").equalsIgnoreCase("approved")) {
         textView1.setTextColor(Color.GREEN)
    } else {
          textView1.setTextColor(Color.RED)
    }

I advise you to read about MVC design pattern

You should keep your JSON in a data object (model in MVC) for example Status object and put all the needed data from json to this object

When filling the listView adjust the view according to the statuses:

if(items[index].getStatus.equals("approved"))
        textView.setTextColor(ContextCompat.getColor(context, R.color.green));
else if(items[index].getStatus.equals("disapproved"))
        textView.setTextColor(ContextCompat.getColor(context, R.color.red));

You will need to override a more complex type of Adapter in order to do what you want, SimpleAdapter only allows setting the text in a very simple way as a String.

Something like ArrayAdapter might be a better fit, in that case you could use the code Michael A has shown in the getView method of ArrayAdapter to change the display of the text.

You might also look into SpannableString which allows adding color to text, but this would require being able to provide code to determine the status of the item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM