Search code examples
androidandroid-listview

Update ListView background color when bluetooth get a new read


I'm trying to update each element every time an read comes from the Bluetooth, I found this

deviceListAdapter = new ArrayAdapter<String>(view.getContext(),
            android.R.layout.simple_list_item_1, mylist) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = super.getView(position, convertView, parent);

               if(getItem(position).startsWith("something"))
                {
                    // do something change color
                    row.setBackgroundColor (Color.GREEN); // some color
                }
                else
                {
                    // default state
                    row.setBackgroundColor (Color.RED); // default coloe
                }               
            return row;
        }
    };

It does updates the color, but here is the thing I have a list like this one

enter image description here

The list is loaded through a call to a server with Volley.

The things is that when the Bluetooth receive data, check if he id exists in the ListView and update the color to green.

I this even possible? Any thoughts on this can I do this?


Solution

  • So I found what It was missing in the conditional to look if the id has being read.

    I had to add testArray.contains() with that part it works as expected.

    if(testArray.contains(getItem(position)))
        {
          row.setBackgroundColor (Color.GREEN); // some color
        }
        else
        {
          row.setBackgroundColor (Color.RED); // default coloe
        }
                       
        return row;