android - Resource string읽어오기
2011. 11. 29. 16:02
@strings.xml
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="string_id">This is a sample test</string>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="string_id">This is a sample test</string>
@R.java
public static final class string {
public static final int string_id=0x7f050007;
public static final class string {
public static final int string_id=0x7f050007;
"This is a smaple test" 라는 string을 resource로부 읽어오기 위해서는 아래와 같은 코드가 가능하다.
1.
getString(string_id), or
getResource().getString(string_id);
2.
Context context = this;
context.getString(string_id);
위 두 경우의 차이점은 바로 요 것.getString(string_id), or
getResource().getString(string_id);
2.
Context context = this;
context.getString(string_id);
You might need to do like 2 if your code is not in an activity.
Activity가 아닌 class의 경우는 전달받은 context를 이용해서 resource에 접근해야 함.
import android.content.Context 만 했다고
getString을 그냥 사용하게 되면 cannot find symbol error를 만나게 된다.
'관심거리 > Android' 카테고리의 다른 글
startManagingCursor() (2) | 2012.02.26 |
---|---|
아는만큼만 말할 수 있다 (0) | 2012.02.26 |
CodePro AnalytiX 사용법 (0) | 2011.10.19 |