Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - SimpleCursorAdapter.ViewBinder - Set background color

Tags:

android

sqlite

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.

like image 338
lucignolo Avatar asked Oct 22 '22 22:10

lucignolo


1 Answers

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);
 }
}
like image 136
nandeesh Avatar answered Oct 27 '22 11:10

nandeesh