2015-05-24 209 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); 
    }