简体   繁体   English

Android - SimpleCursorAdapter.ViewBinder - 设置背景颜色

[英]Android - SimpleCursorAdapter.ViewBinder - Set background color

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. 例如,如果我游泳4圈我想要彩色背景。 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? 并且也有可能,当lap等于另一个字段的4设置背景颜色时,例如在我的代码中:R.id.swimm_pos? thank you. 谢谢。

Returning true from ViewBinder implies that you are also binding the data to the View. 从ViewBinder返回true意味着您还将数据绑定到View。

But in your case you are not setting the text of R.id.swimm_lap. 但在你的情况下,你没有设置R.id.swimm_lap的文本。

So add setText before return statement 所以在return语句之前添加setText

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 编辑:对于第二个问题,假设您想根据游泳圈改变R.id.row_counter的背景然后添加

else if (view.getId() == R.id.row_counter){ 
 int color = cursor.getString(cursor.getColumnIndex("swimm_lap")); 
 if (s.equals("4")) {
     view.setBackgroundColor(0xFF558866);
 }
}

Solved, here the right code: 解决了,这里是正确的代码:

         adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
              if (view.getId() == R.id.row_counter)
                { 
                  int color = cursor.getInt(cursor.getColumnIndex("swimm_lap"));
                  String s = String.valueOf(color);
                  if (s.equals("4")) {
                  TextView tv = (TextView)view;
                  tv.setBackgroundColor(0xFF558866);
                              }
                 return true;
            }
              return false;}
        });
    this.setListAdapter(adapter);
    datasource.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM