I retrieve from database my swimming performance. I would like to change background color of one field according to his value. For example if i swimm 4 laps I want color background. I try this code that set background correctly but text disappears.
String[] columns = new String[] { "swimm_pos", "swimm_date","swimm_lap", "swimm_stroke", "swimm_time", "swimm_media", "swimm_efficiency", "swimm_note" };
int[] to = new int[] { R.id.row_counter, R.id.swimm_date, R.id.swimm_lap, R.id.swimm_stroke, R.id.swimm_time, R.id.swimm_medialap, R.id.swimm_efficiency, R.id.swimm_note};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.contacto_list_item,
cursor,
columns,
to);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.swimm_lap)
{
int color = cursor.getInt(columnIndex);
String s = String.valueOf(color);
if (s.equals("4")) {
TextView tv = (TextView)view;
tv.setBackgroundColor(0xFF558866);}
return true;
}
return false;}
});
And is also possible, when lap is equals to 4 set background color of another field, for example in my code: R.id.swimm_pos? thank you.
Returning true from ViewBinder implies that you are also binding the data to the View.
But in your case you are not setting the text of R.id.swimm_lap.
So add setText before return statement
tv.setText(s);
return true;
Edit: For the second question suppose you want to change background of R.id.row_counter depending upon swim lap then add
else if (view.getId() == R.id.row_counter){
int color = cursor.getString(cursor.getColumnIndex("swimm_lap"));
if (s.equals("4")) {
view.setBackgroundColor(0xFF558866);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With