30
33

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.

Key, Valueの値を持ったSpinnerを作る(XMLから読み込む)

Last updated at Posted at 2014-09-13

##SpinnerにKey-Valueを設定する(XMLでKey, Valueの値を設定)

これがデファクトスタンダードなのかは不明。(一応これで正常に動作してます)
もっと良い方法があったら(あるはず)教えて下さい!

####アダプターを用意

KeyValuePairAdapter.java
/**
 * SpinnerにKey-Valueを設定するためのアダプタークラス
 * 
 * @author helmos
 */
public class KeyValuePairAdapter extends ArrayAdapter<Pair<String, String>> {

  /**
   * コンストラクタ
   * 
   * @param context
   *          スピナーを利用するアクティビティ
   * @param resourceId
   *          スピナー表示レイアウトのリソースID
   * @param list
   *          ドロップダウンリストに設定する配列
   */
  public KeyValuePairAdapter(Context context,
                             int resourceId,
                             ArrayList<Pair<String, String>> list) {
    super(context, resourceId, list);
  }

  // スピナー表示用
  @Override
  public View getView(int pos, View convertView, ViewGroup parent) {
    TextView textView = (TextView) super.getView(pos, convertView, parent);
    textView.setText(getItem(pos).second);
    return textView;
  }

  // スピナードロップダウン表示用
  @Override
  public View getDropDownView(int pos, View convertView, ViewGroup parent) {
    TextView textView = (TextView) super.getDropDownView(pos,
                                                         convertView,
                                                         parent);
    textView.setText(getItem(pos).second);
    return textView;
  }

}

####アクティビティ側

Activity.java
  @Override
  protected void onCreate(Bundle savedInstanceState) {

    /* 
       省略
    */

    // スピナー用のデータを取得
    ArrayList<Pair<String, String>> sortItemList = new ArrayList<Pair<String, String>>();
    Resources resources = getResources();
    TypedArray sortItemArray = resources.obtainTypedArray(R.array.sort_spinner_data);
    int arrayLength = sortItemArray.length();
    for (int i = 0; i < arrayLength; i++) {
      int id = sortItemArray.getResourceId(i, -1);
      if (id != -1) {
        String[] item = resources.getStringArray(id);
        sortItemList.add(new Pair<String, String>(item[0], item[1]));
      }
    }
    sortItemArray.recycle();

    // スピナーにアダプターを設定
    mSortSpinnerAdapter = new KeyValuePairAdapter(this,
                                                  android.R.layout.simple_spinner_item,
                                                  sortItemList);
    mSortSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mSpinner.setAdapter(mSortSpinnerAdapter);
  }

####設定するKeyとValueの値

arrays.xml
<resources>

    <string-array name="sort_spinner_item1">
        <item name="key">hoge</item>
        <item name="value">value1</item>
    </string-array>
    <string-array name="sort_spinner_item2">
        <item name="key">fuga</item>
        <item name="value">value2</item>
    </string-array>
    <string-array name="sort_spinner_item3">
        <item name="key">foo</item>
        <item name="value">value3</item>
    </string-array>

    <array name="sort_spinner_data">
        <item>@array/sort_spinner_item1</item>
        <item>@array/sort_spinner_item2</item>
        <item>@array/sort_spinner_item3</item>
    </array>

</resources>

(表示だけならここまで)


####補足

選んでるやつのKeyを取得
→AdapterのgetItem(int position)を使う

Activity.java

private void hoge {
  /* 省略 */

  // 選択されているPairアイテムを取得
  Pair<String, String> selectedItem = mSortSpinnerAdapter.getItem(mSpinner.getSelectedItemPosition());
  // key
  String key = selectedItem.first;
  // value
  String val = selectedItem.second;

  /* 省略 */
}
30
33
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
30
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?