2015-05-24 133 views
1

我想在图像没有显示时显示图像,如果内容有图像显示,我将图像放入可绘制文件夹中并使用数据库字段添加图像名称,如果内容没有图像我写在字段“不”而不是图像名称。我写了这个代码:如何在内容没有图像时显示图像并在显示图像时显示图像

ImageView img = (ImageView) findViewById(R.id.show_img); 
     if(content.getImg() == "not") { 
        img.setVisibility(View.GONE); 
       }else { 
        String image = content.getImg(); 
        int imageResource = c.getResources().getIdentifier(image, "drawable", 
c.getPackageName()); 
    img.setImageResource(imageResource); 
    Log.i(DBAdapter.TAG, image); 
       } 

在日志消息显示图像的名字,但在应用程序显示默认的src图片不显示文件夹绘制图像,而不是坤ImageView的,如果没有图像(在日志中写道这个30375:30375 W/EGL_genymotion)。

+1

content.getImg()返回什么? – dora

+0

你有没有小心,imagename不包括像.png这样的扩展名? – dora

+1

**“if(content.getImg()==”not“)”**:您无法使用'=='比较Java中的字符串。你需要使用'if(content.getImg()。equals(“not”))'' – Squonk

回答

0
ImageView img = (ImageView) findViewById(R.id.show_img); 
    if (content.getImg().equalsIgnoreCase("not")) { 
     img.setVisibility(View.GONE); 
    } else { 
     img.setVisibility(View.VISIBLE); 
     String image = content.getImg(); 
     int imageResource = c.getResources().getIdentifier(
       "drawable/" + image, null, c.getPackageName()); 
     img.setImageResource(imageResource); 
     Log.i(DBAdapter.TAG, image); 
    }