2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Androidで簡易的なxmlのデータを配列として扱う

Last updated at Posted at 2016-09-15

とてつもなく久々Androidを触ったので、色々メモしていきます。
eclipseからandroid studioの進化具合にびっくり。。

試した環境

Android Studio 2.1.3
Mac OS X 10.11.6

下記のようなxmlデータを用意

strings.xml

<resources>
    <string name="app_name">hogeApp</string>
    <string name="action_settings">Settings</string>
    <string name="section_format">Hello World from section: %1$d</string>

    <string-array name="my_array">
        <item>Aタブ</item>
        <item>Bタブ</item>
        <item>Cタブ</item>
    </string-array>

〜省略〜

xmlからデータを読み込む

MainActiviy.java

//〜省略〜

private String[] mArrTitles;

//〜省略〜

mArrTitles = getResources().getStringArray(R.array.my_array);

// あとはStringの配列と使えます。
// Log.d("hoge", Arrays.toString(mArrTitles));

Fragmentのタイトルとしてつかったり

MainActivity.java

//〜省略〜

        int index = 0;
        for (String title : mArrTitles) {
            adapter.addFrag(MyFragment.newInstance(index++), title);
        }
//〜省略〜
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?