Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically change background color listView

I need to change background color of list view dynamically based on string value (JSON). I want to change the background color if the text in the TAG_SEVERITY field is "critical". Below is how I tried, but With code it changes the severity field to blue regardless of the text. I t would be etter if the row color is changed instead of field background color. I'm quite new to this.

    SimpleAdapter adapter = new SimpleAdapter(this, mCommentList,
            R.layout.single_post, new String[] {TAG_SEVERITY, TAG_TITLE }, new int[] { R.id.severity, R.id.rTitle});adapter.setViewBinder(new SimpleAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View view, Object data,
                String textRepresentation) {
            int v=view.getId();
            if(v==R.id.severity && String.valueOf(data.toString()).contentEquals("Critical")){
                ((View) view.getParent()).setBackgroundColor(Color.BLUE);
            }
            TextView TV=(TextView) view;
            TV.setText(data.toString());
            return true;
        }
    });

    setListAdapter(adapter);
like image 708
user2602159 Avatar asked Jun 05 '26 07:06

user2602159


1 Answers

I think that by setting the background for the view.getParent(), you are setting the background for the entire ListView. Have you tried using just view.setBackground()?

The other alternative is to create your own adapter class, and setting the view's background color in the getView() method based on the severity.

like image 145
Jace J McPherson Avatar answered Jun 07 '26 23:06

Jace J McPherson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!