我想使用以下代码设置 ImageView 的背景:
String color = (cursor.getString(cursor.getColumnIndex(NotesDbAdapter.KEY_COLOR)));
ImageView background=(ImageView)view.findViewById(R.id.album_image);
DecodeTask task = (DecodeTask)background.getTag(R.id.album_image);
background.setScaleType(ImageView.ScaleType.CENTER_CROP);
String imageCheck = (cursor.getString(cursor.getColumnIndex(NotesDbAdapter.KEY_IMAGE)));
if (imageCheck != 0){
background.setImageBitmap(BitmapFactory.decodeFile(imageCheck));
}
else if (color != null && !color.isEmpty()){
if (color.equals("1")){
task = new DecodeTask(background);
background.setImageResource(R.drawable.bluebg);
}
else if (color.equals("2")){
task = new DecodeTask(background);
background.setImageResource(R.drawable.greenbg);
}
else if (color.equals("3")){
task = new DecodeTask(background);
background.setImageResource(R.drawable.yellowbg);
}
else if (color.equals("4")){
task = new DecodeTask(background);
background.setImageResource(R.drawable.redbg);
}
else if (color.equals("5")){
task = new DecodeTask(background);
background.setImageResource(R.drawable.purplebg);
}
}
但是,当 imageCheck 等于 0 时,它不会读取进一步的代码并设置图像的默认背景。我应该怎么办?
最佳答案
尝试改变
if(imageCheck != 0)
至
if(!imageCheck.equals("0"))
因为您正在比较字符串
关于java - 使用 else if 语句设置 imageview 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18064600/