开发者

Can I change the background of a LinearLayout from a ListView row in the ViewBinder?

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:
    开发者_如何学C                ((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);


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.


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

BR, Christoffer


Override bindView in your CursorAdapter

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜