Android > resource - ¹è¿ ¸®¼Ò½º <string-array>
µî·ÏÀÏ : 2017-07-05 16:54
Á¶È¸¼ö : 53,120
¹è¿ ¸®¼Ò½º´Â º¸Åë /res/values/arrays.xml ¿¡¼ °ü¸®ÇÑ´Ù.
arrays.xml ÆÄÀÏÀ» »õ·Î ¸¸µé°í, ¹è¿(string-array)À» µî·ÏÇÏ°í xml°ú java¿¡¼ ¸®¼Ò½º¸¦ È£ÃâÇÏ¿© º¸ÀÚ.
1. res(¸®¼Ò½º)¿¡ <string-array> µî·ÏÇϱâ
/res/values ¹Ø¿¡ arrays.xml ÆÄÀÏÀ» »õ·Î ¸¸µç´Ù.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="company">
<item>Samsung</item>
<item>Hyundai</item>
<item>LG</item>
<item>SK</item>
<item>KIA</item>
</string-array>
</resources>
- ¹è¿(string-array)·Î nameÀ» company·Î µî·Ï
- company ¹Ø¿¡ Samsung, Hyundai, LG, SK, KIA ¸¦ ¼Ó¼ºÀ¸·Î µî·ÏÇß´Ù.
2. layoutÀÇ xml¿¡¼ ¹è¿ ºÒ·¯¿À±â
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/ListView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/company" />
</RelativeLayout>
- <ListView> ¸¦ »ç¿ëÇÏ¿© <string-array> È£Ãâ
- id : ListView1 ( android:id="@+id/ListView1" )
- ¼Ó¼º : nameÀÌ companyÀÎ array ( android:entries="@array/company" )
½ÇÇàȸéÀ» º¸ÀÚ!
- ¸®¼Ò½º¿¡ µî·ÏµÈ companyÀÇ item µéÀÌ <ListView>ÇüÅ·ΠÃâ·ÂµÆ´Ù.
3. Activity.java ¿¡¼ <string-array> ¸®¼Ò½º ºÒ·¯¿À±â
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ¸®¼Ò½º¿¡¼ nameÀÌ companyÀÎ array¸¦ °¡Á®¿Í¼ String[] ¹è¿¿¡ ´ëÀÔ
String[] company = getResources().getStringArray(R.array.company);
// company¸¦ ArrayAdapter¿¡ µî·ÏÇÑ´Ù.
//(ListView´Â Adapter¸¦ ÅëÇؼ µî·ÏÇÑ´Ù.)
// simple_list_item_1Àº textview¸¦ ´ã°í ÀÖ´Â xml ÇüÅÂÀÌ´Ù.
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, company);
// ¸®¼Ò½º¿¡¼ id °¡ ListView1 ÀÎ ListView¸¦ °¡Á®¿Â´Ù.
// ListView¿¡ adapter¸¦ µî·ÏÇÑ´Ù.
ListView listView1 = (ListView)findViewById(R.id.ListView1);
listView1.setAdapter(adapter);
}
}
- getResource().getStringArray() ¸¦ »ç¿ëÇÏ¿© ¸®¼Ò½º¿¡¼ company <string-array>¸¦ ¹è¿ ÇüÅ·Π°¡Á®¿Â´Ù.
- ListView¿¡´Â ArrayAdapter ÇüÅ·Πµ¥ÀÌÅ͸¦ ´ëÀÔÇؾßÇϱ⿡ ArrayAdapter<String>¿¡ company¸¦ µî·ÏÇÑ´Ù.
- findViewById·Î ListView1 À» °¡Á®¿Â´Ù.
- ListView1¿¡ Adapter¸¦ µî·ÏÇÑ´Ù.
- ListView¿¡ µî·ÏÇÑ <string-array>¸¦ ±×´ë·Î µî·ÏÇ߱⿡ °á°ú´Â °°´Ù.