5

我从数据库中检索我的游泳成绩。我想根据他的值改变一个字段的背景颜色。例如,如果我游泳 4 圈,我想要彩色背景。我尝试正确设置背景但文本消失的代码。

        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;}

        });

并且也是可能的,当 lap 等于 4 时设置另一个字段的背景颜色,例如在我的代码中:R.id.swimm_pos? 谢谢你。

4

2 回答 2

3

从 ViewBinder 返回 true 意味着您还将数据绑定到视图。

但是在您的情况下,您没有设置 R.id.swimm_lap 的文本。

所以在 return 语句之前添加 setText

tv.setText(s);
return true;

编辑:对于第二个问题,假设您想根据游泳圈数更改 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);
 }
}
于 2012-12-16T09:46:53.630 回答
0

解决了,这里是正确的代码:

         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();
}
于 2012-12-16T16:12:05.540 回答