我可以从 ViewBinder 中的 ListView 行更改 LinearLayout 的背景吗?

发布于 2024-11-05 23:46:50 字数 2957 浏览 0 评论 0原文

我有一个包含一些元素的 ListView,我想根据它们在数据库中的类型更改行的背景。我得到的是一个 SimpleCursorAdapter 实例及其函数 adapter.setViewBinder(...)。但似乎我无法访问一行的 LinearLayout 。这是代码:

final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.day, c, new String[] { "name_con", "start", "end",
                    "type", "prof", "room" }, new int[] { R.id.subjectName,
                    R.id.subjectStart, R.id.subjectEnd, R.id.subjectType,
                    R.id.subjectProf, R.id.subjectRoom });

    adapter.setViewBinder(new ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int column) {
            switch (view.getId()) {
            case R.id.subjectName:
                final String colNameName = cursor.getString(1);
                ((TextView) view).setText(colNameName);

                return true;
            case R.id.subjectStart:
                final int colNameStart = cursor.getInt(2);

                Date dStart = new Date(colNameStart * 1000);
                SimpleDateFormat sdfStart = new SimpleDateFormat(
                        "HH:mm", Locale.getDefault());
                String startString = sdfStart.format(dStart);

                ((TextView) view).setText(startString);

                return true;
            case R.id.subjectEnd:
                final int colNameEnd = cursor.getInt(3);

                Date dEnd = new Date(colNameEnd * 1000);
                SimpleDateFormat sdfEnd = new SimpleDateFormat(
                        "HH:mm", Locale.getDefault());
                String EndString = sdfEnd.format(dEnd);

                ((TextView) view).setText(EndString);

                return true;
            case R.id.subjectType:
                final int colNameType = cursor.getInt(4);

                switch(colNameType){
                case 0:
                    ((TextView) view).setText(R.string.practice);
                    break;
                case 1:
                    ((TextView) view).setText(R.string.course);
                    break;
                case 2:
                    ((TextView) view).setText(R.string.practica);
                    break;
                case 3:
                    ((TextView) view).setText(R.string.seminar);
                }

                return true;
            case R.id.subjectProf:
                final String colNameProf = cursor.getString(5);
                ((TextView) view).setText(colNameProf);

                return true;
            case R.id.subjectRoom:
                final String colNameRoom = cursor.getString(6);
                ((TextView) view).setText(colNameRoom);

                return true;
            case R.id.dayCell:
                ((LinearLayout)view).setBackgroundColor(Color.WHITE);
            default:
                return false;
            }
        }
    });

    this.setListAdapter(adapter);

I have a ListView with some elements and I want to change the background of the row depending on their type in the database. What I got is a SimpleCursorAdapter instance and it's function adapter.setViewBinder(...). But it seems, that I can't access the LinearLayout of a row. Here's the code:

final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.day, c, new String[] { "name_con", "start", "end",
                    "type", "prof", "room" }, new int[] { R.id.subjectName,
                    R.id.subjectStart, R.id.subjectEnd, R.id.subjectType,
                    R.id.subjectProf, R.id.subjectRoom });

    adapter.setViewBinder(new ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int column) {
            switch (view.getId()) {
            case R.id.subjectName:
                final String colNameName = cursor.getString(1);
                ((TextView) view).setText(colNameName);

                return true;
            case R.id.subjectStart:
                final int colNameStart = cursor.getInt(2);

                Date dStart = new Date(colNameStart * 1000);
                SimpleDateFormat sdfStart = new SimpleDateFormat(
                        "HH:mm", Locale.getDefault());
                String startString = sdfStart.format(dStart);

                ((TextView) view).setText(startString);

                return true;
            case R.id.subjectEnd:
                final int colNameEnd = cursor.getInt(3);

                Date dEnd = new Date(colNameEnd * 1000);
                SimpleDateFormat sdfEnd = new SimpleDateFormat(
                        "HH:mm", Locale.getDefault());
                String EndString = sdfEnd.format(dEnd);

                ((TextView) view).setText(EndString);

                return true;
            case R.id.subjectType:
                final int colNameType = cursor.getInt(4);

                switch(colNameType){
                case 0:
                    ((TextView) view).setText(R.string.practice);
                    break;
                case 1:
                    ((TextView) view).setText(R.string.course);
                    break;
                case 2:
                    ((TextView) view).setText(R.string.practica);
                    break;
                case 3:
                    ((TextView) view).setText(R.string.seminar);
                }

                return true;
            case R.id.subjectProf:
                final String colNameProf = cursor.getString(5);
                ((TextView) view).setText(colNameProf);

                return true;
            case R.id.subjectRoom:
                final String colNameRoom = cursor.getString(6);
                ((TextView) view).setText(colNameRoom);

                return true;
            case R.id.dayCell:
                ((LinearLayout)view).setBackgroundColor(Color.WHITE);
            default:
                return false;
            }
        }
    });

    this.setListAdapter(adapter);

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

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

发布评论

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

评论(3

半透明的墙 2024-11-12 23:46:50

您应该能够执行以下操作:

switch (view.getId()) {
            case R.id.subjectName:
                final String colNameName = cursor.getString(1);
                ((TextView) view).setText(colNameName);
                ((LinearLayout)view.getParent()).setBackgroundResource(<resource id>);
                return true;

编辑:为了提高效率,您可能只想在其中一个 ViewBinder 传递中执行此操作,并且您需要确保它是创建的 LinearLayout在 R.layout.day 中,否则最终会出现 ClassCast 异常。

You should be able to do something like:

switch (view.getId()) {
            case R.id.subjectName:
                final String colNameName = cursor.getString(1);
                ((TextView) view).setText(colNameName);
                ((LinearLayout)view.getParent()).setBackgroundResource(<resource id>);
                return true;

EDIT: for efficiency, you'll probably only want to do it in one of the ViewBinder passes, and you'd need to be sure its a LinearLayout that's created in R.layout.day or you'll end up with a ClassCast exception.

感受沵的脚步 2024-11-12 23:46:50

您可以调用 setBackgroundColor(int color)、setBackgroundResource(int) 或 setBackgroundResource(int resid) 来更改视图的背景。

BR,
克里斯托弗

You could call the setBackgroundColor(int color), setBackgroundResource(int) or setBackgroundResource(int resid) to change the background of your view.

BR,
Christoffer

套路撩心 2024-11-12 23:46:50

覆盖 bindView 在你的 CursorAdapter

@Override
public void bindView(View view, Context context, Cursor cursor) {
    super.bindView(view, context, cursor);
    view.setBackgroundColor(color);
}

Override bindView in your CursorAdapter

@Override
public void bindView(View view, Context context, Cursor cursor) {
    super.bindView(view, context, cursor);
    view.setBackgroundColor(color);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文