有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ImageView背景在行中

我想在列表行中设置一个图像资源的背景图像,但是下面的代码对背景没有影响,它根本不影响背景

@Override
        public void bindView(View view, Context context, Cursor cursor) {

            TextView mobileNo=(TextView)view.findViewById(R.id.text1);
            mobileNo.setText(cursor.getString(cursor.getColumnIndex(NotesDbAdapter.KEY_TITLE)));

            TextView frequency=(TextView)view.findViewById(R.id.date);
            frequency.setText(cursor.getString(cursor.getColumnIndex(NotesDbAdapter.KEY_COLOR)));
            String color = (cursor.getString(cursor.getColumnIndex(NotesDbAdapter.KEY_COLOR)));

            ImageView background=(ImageView)view.findViewById(R.id.album_image);
            if (color == "#57a2f6"){
                background.setImageResource(R.drawable.bluebg);
            }
            else if (color == "#00cdb1"){
                background.setImageResource(R.drawable.redbg);
            }

共 (1) 个答案

  1. # 1 楼答案

    您正在尝试将String color==进行比较,这是not valid in java如此 使用eaquals(){}

    所以你的代码应该是

    if (color.equals("#57a2f6")){
         background.setImageResource(R.drawable.bluebg);
        }
    else if (color.equals("#00cdb1")){
         background.setImageResource(R.drawable.redbg);
        }