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);
4

3 回答 3

0

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

BR,克里斯托弗

于 2011-05-06T13:33:43.367 回答
0

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

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 异常。

于 2011-05-06T13:36:04.510 回答
0

覆盖你的bindViewCursorAdapter

@Override
public void bindView(View view, Context context, Cursor cursor) {
    super.bindView(view, context, cursor);
    view.setBackgroundColor(color);
}
于 2011-05-06T13:41:33.150 回答