2012-12-30 43 views
0

我有以下代碼:資源ID總是0使用則getIdentifier()

dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
     if (view.getId() == R.id.listitem_exercise_image) { 
      String image_1 = cursor.getString(columnIndex); 
      Log.d("MyApp", "image_1 = " + image_1); 
      int resourceId = getResources().getIdentifier(image_1 , "drawable", getPackageName()); 
      Log.d("MyApp", "image_1 id = " + String.valueOf(resourceId)); 
      return true; 
     } 

     return false; 
    } 
}); 

現在,在日誌中,其顯示的爲我Log.d

12-30 23:34:04.995: D/MyApp(8354): image_1 = abdominal_4_point_drawing_in_1.png 
12-30 23:34:05.015: D/MyApp(8354): image_1 id = 0 
12-30 23:34:05.165: D/MyApp(8354): image_1 = alternate_hammer_curl_1.png 
12-30 23:34:05.195: D/MyApp(8354): image_1 id = 0 
12-30 23:34:05.325: D/MyApp(8354): image_1 = alternate_incline_dumbbell_curl_1.png 
12-30 23:34:05.335: D/MyApp(8354): image_1 id = 0 

現在所有這些.png文件存在於「drawable」文件夾中。他們明確地位於該文件夾中。我知道這一點,因爲我是那個把他們放到那個文件夾中的人,而且我也只是雙重檢查。我也運行了一個Project Clean。

那麼爲什麼每個人都會將資源ID設置爲0?

+0

嗯,刪除你的舊問題重新發布它是皺眉了。您應該將新數據添加到現有問題中。很高興你找到了答案,但現在我明白爲什麼它不起作用。 – Sam

+0

謝謝,但我縮小了原因,並認爲它需要一個新的問題...虐待下一次嘗試編輯。 – scarhand

回答

3

資源標識符不以".png"結尾,擴展被剝離並完成其他一些轉換。現在,如果image_1其中"abdominal_4_point_drawing_in_1",並且存在生成R.drawable.abdominal_4_point_drawing_in_1的資源,它將起作用。

+1

......補貨。兩秒鐘。兩秒鐘。 +1,先生。 :P – Eric

+0

using'image_1 = image_1.replace(「。png」,「」);'修復了它。謝謝。 – scarhand