Android,使用SimpleCursorAdapter设置颜色而不仅仅是字符串

发布于 2024-10-31 00:53:43 字数 391 浏览 5 评论 0原文

我在应用程序的列表上设置了一个简单的光标适配器,如下所示:

private static final String fields[] = {"GenreLabel", "Colour", BaseColumns._ID};


datasource = new SimpleCursorAdapter(this, R.layout.row, data, fields, new int[]{R.id.genreBox, R.id.colourBox});

R.layout.row 由两个 TextView(genreBox 和 colorBox)组成。我不想将 TextView 的内容设置为 "Colour" 的值,而是想将其背景颜色设置为该值。

我需要做什么才能实现这一目标?

I have a simple cursor adapter set on a list in my application as follows:

private static final String fields[] = {"GenreLabel", "Colour", BaseColumns._ID};


datasource = new SimpleCursorAdapter(this, R.layout.row, data, fields, new int[]{R.id.genreBox, R.id.colourBox});

R.layout.row consists of two TextViews (genreBox and colourBox). Rather than setting the content of the TextView to the value of "Colour" , I would like to set its background colour to that value.

What would I need to do to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自在安然 2024-11-07 00:53:43

查看 SimpleCursorAdapter.ViewBinder

setViewValue< /a> 基本上是您对光标中的数据执行任何操作的机会,包括设置视图的背景颜色。

例如,类似:

SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        String name = cursor.getColumnName(columnIndex);
        if ("Colour".equals(name)) {
            int color = cursor.getInt(columnIndex);
            view.setBackgroundColor(color);
            return true;
        }
        return false;
    }
}
datasource.setViewBinder(binder);

更新 - 如果您使用自定义适配器(扩展CursorAdaptor),那么代码不会发生太大变化。您将覆盖 getViewbindView

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView != null) {
        return convertView;
    }
    /* context is the outer activity or a context saved in the constructor */
    return LayoutInflater.from(context).inflate(R.id.my_row);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    int color = cursor.getInt(cursor.getColumnIndex("Colour"));
    view.setBackgroundColor(color);
    String label = cursor.getString(cursor.getColumnIndex("GenreLabel"));
    TextView text = (TextView) findViewById(R.id.genre_label);
    text.setText(label);
}

您需要更多手动操作,但或多或​​少是相同的想法。请注意,在所有这些示例中,您可以通过缓存列索引而不是通过字符串查找它们来节省性能。

Check out SimpleCursorAdapter.ViewBinder.

setViewValue is basically your chance to do whatever you wish with the data in your Cursor, including setting the background color of your views.

For example, something like:

SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        String name = cursor.getColumnName(columnIndex);
        if ("Colour".equals(name)) {
            int color = cursor.getInt(columnIndex);
            view.setBackgroundColor(color);
            return true;
        }
        return false;
    }
}
datasource.setViewBinder(binder);

Update - if you're using a custom adapter (extending CursorAdaptor) then the code doesn't change a whole lot. You'd be overriding getView and bindView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView != null) {
        return convertView;
    }
    /* context is the outer activity or a context saved in the constructor */
    return LayoutInflater.from(context).inflate(R.id.my_row);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    int color = cursor.getInt(cursor.getColumnIndex("Colour"));
    view.setBackgroundColor(color);
    String label = cursor.getString(cursor.getColumnIndex("GenreLabel"));
    TextView text = (TextView) findViewById(R.id.genre_label);
    text.setText(label);
}

You're doing a bit more manually, but it's more or less the same idea. Note that in all of these examples you might save on performance by caching the column indices instead of looking them up via strings.

话少情深 2024-11-07 00:53:43

您正在寻找的东西需要一个自定义光标适配器。您可以对 SimpleCursorAdapter 进行子类化。这基本上允许访问创建的视图(尽管您将自己创建它)。

有关完整示例,请参阅有关自定义 CursorAdapter 的博客文章。特别是,我认为您需要重写 bindView

What you're looking for requires a custom cursor adapter. You can subclass SimpleCursorAdapter. This basically give access to the view as its created (although you'll be creating it yourself).

See this blog post on custom CursorAdapters for a complete example. Particularly, I think you'll need to override bindView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文