'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
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 1:[1]
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;
Solution 2:[2]
All you need to do is update the item in myList and then call deviceListAdapter.notifyDataSetChanged() to refresh the list views.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Diego |
| Solution 2 | Dan Harms |

