Android的使用SimpleCursorAdapter设置颜色不只是字符串 [英] Android, using SimpleCursorAdapter to set colour not just strings

查看:135
本文介绍了Android的使用SimpleCursorAdapter设置颜色不只是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的游标适配器,在我的应用程序中设置一个列表如下:

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由两个TextViews(genreBox和colourBox)。而不是TextView的内容设置为颜色的价值,我想设置它的背景色,以该值。

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?

推荐答案

查看<一个href="http://developer.android.com/reference/android/widget/SimpleCursorAdapter.ViewBinder.html">SimpleCursorAdapter.ViewBinder.

<一个href="http://developer.android.com/reference/android/widget/SimpleCursorAdapter.ViewBinder.html#setViewValue%28android.view.View,%20android.database.Cursor,%20int%29">setViewValue基本上是你的机会,做任何你想要的数据在你的光标,包括设置的观点的背景颜色。

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

例如,是这样的:

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 ),那么code不改变一大堆。你会覆盖 getView bindView

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.

这篇关于Android的使用SimpleCursorAdapter设置颜色不只是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆