2016-09-18 37 views
-2

字符串我有我的應用程序以這種方式工作:找翻譯

public static String formatForUI(Calendar today, Calendar date) { 
    if (today.get(Calendar.YEAR) == date.get(Calendar.YEAR)) { 
     if (today.get(Calendar.DAY_OF_YEAR) == date.get(Calendar.DAY_OF_YEAR)) { 
      return "Today"; 
     } 
     return currentYearDateFormat(date.getTimeInMillis()); 
    } else { 
     return format(date.getTimeInMillis()); 
    } 
} 

我想要的「今天」是一個字符串,所以我有它在的strings.xml

<resources> 
<string name="today">Today</string> 
</resources> 

而且我有es \ strings.xml

<resources> 
<string name="today">Hoy</string> 
</resources> 

我試過用getResources(),但它不起作用。我該怎麼做?

+0

「但它不起作用」定義不起作用。 – njzk2

+0

它說「不能解決的符號」 – Trowsing

回答

1

至於android中的很多東西,你需要一個Context。通常情況下,您的活動或服務或包含您的片段的活動。

定義期待一個Context參數的方法:

public static String formatForUI(Context context, Calendar today, Calendar date) { 

然後用右鍵:

return context.getString(R.string.today); 

(或context.getResources().getString(R.string.today),並與資源,你可以訪問字符串旁邊其他的東西)

+0

或只是'context.getString()' –

+0

@代碼學徒好點 – njzk2

0

這裏是我的解決方案:

  • 清理並建立你的Android項目應該工作。
  • 如果沒有,請嘗試重新啓動android studio。
  • 如果仍有錯誤,請將getcontext().getResources().getstring(int id)
+0

最後一顆子彈沒有任何意義。請編輯它。 –

+0

Hi @ Code-Apprentice感謝評論。我刪除了最後一個項目符號,因爲它沒有回答問題。 –