페이지

[안드로이드] stings 가변적으로 불러오기



  • strings.xml 경우


 strings.xml 에서 연속적인 ID 또는 일정한 패턴 이름 불러와서  텍스트 뷰에 나타낼 때, 일일이 쓰지 않고 한 문장으로 뒷자리 숫자만 바꿔서 불러올수 있다.


예를 들어, WeatherDescr_1, WeatherDescr_2, WeatherDescr_3 등등 연속적이거나 특정 값을 가질 경우


       int  id= getResources().getIdentifier("가변적 변수"+nummer, "string", getPackageName()(패키지 이름));


  • TextView ID 경우



int tempId = getResources().getIdentifier("temp" + i, "id", getPackageName());
forecast_temp = (TextView) findViewById(tempId);

activity_main.xml

  <TextView         android:id="@+id/temp0" />
  <TextView         android:id="@+id/temp1" />
불라부라..


Parameters
nameThe name of the desired resource.
defTypeOptional default resource type to find, if "type/" is not included in the name. Can be null to require an explicit type.
defPackageOptional default package to find, if "package:" is not included in the name. Can be null to require an explicit package.

Returns
  • int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)
예시>

Activity_main.java

int nummer=0;

int id = getResources().getIdentifier("WeatherDescr_"+nummer, "string", "com.sample.muster");

textview.setText(id);




strings.xml

<resources>
<!-- Weather condition Icon list             -->

<string name="WeatherDescr_1">맑음</string>
<string name="WeatherDescr_2">대체로 맑음</string>
<string name="WeatherDescr_3">대체로 흐림</string>
<string name="WeatherDescr_4">흐림</string>

</resources>

댓글 없음:

댓글 쓰기

[Kotlin] 안드로이드 내부저장소 캐시에 저장하기

안드로이드 내부에 임시적으로 데이터를 저장하기 위해 캐시에 저장한다. 따로 퍼미션이 필요없고 보안이 필요없는 부분에서 잠시 저장할 때 유리하다 내부적으로 저장소가 부족할때 자동으로 지워질수 있다는 경고 private fun saveCache(d...