質問

ることをここに宣言します私のスピナーは以下のようにという非常に静か 2つの文字列配列 array.xml タイトルや価値観)

<Spinner
    android:id="@+id/searchCriteria"
    android:entries="@array/searchBy"
    android:entryValues="@array/searchByValues" />

期待してい spinner.getSelectedItem() る配列を返し [title, value] それを返しまうタイトル文字列になります。では無視し android:entryValues?の行き方を教えてください値でない名作しているか確認してください。は この可XMLのみまたアダプターとい プログラム?

役に立ちましたか?

解決

むしろデュアルアレイ法よりも、その既知のタイプや使用のオブジェクトをプログラムであなたのArrayAdapterを埋めない理由。私はこれを行う同様の性質(下部にリンク)のチュートリアルを書きました。基本的な前提は、Javaオブジェクトの配列を作成に関するスピナーを伝え、その後、スピナークラスから直接、これらのオブジェクトを使用することです。

:私の例では、私は次のように定義された「状態」を表すオブジェクトを持っています
package com.katr.spinnerdemo;

public class State {

// Okay, full acknowledgment that public members are not a good idea, however
// this is a Spinner demo not an exercise in java best practices.
public int id = 0;
public String name = "";
public String abbrev = "";

// A simple constructor for populating our member variables for this tutorial.
public State( int _id, String _name, String _abbrev )
{
    id = _id;
    name = _name;
    abbrev = _abbrev;
}

// The toString method is extremely important to making this class work with a Spinner
// (or ListView) object because this is the method called when it is trying to represent
// this object within the control.  If you do not have a toString() method, you WILL
// get an exception.
public String toString()
{
    return( name + " (" + abbrev + ")" );
}
}

そして、次のようにあなたがこれらのクラスの配列を持つスピナーを移入することができます:

       // Step 1: Locate our spinner control and save it to the class for convenience
    //         You could get it every time, I'm just being lazy...   :-)
    spinner = (Spinner)this.findViewById(R.id.Spinner01);

    // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
          android.R.layout.simple_spinner_item, new State[] {   
                new State( 1, "Minnesota", "MN" ), 
                new State( 99, "Wisconsin", "WI" ), 
                new State( 53, "Utah", "UT" ), 
                new State( 153, "Texas", "TX" ) 
                });

    // Step 3: Tell the spinner about our adapter
    spinner.setAdapter(spinnerArrayAdapter);  

次のようにあなたが選択した項目を取得することができます:

State st = (State)spinner.getSelectedItem();

そして今、あなたと仕事に善意のJavaクラスを持っています。あなたがインターセプトしたい場合はスピナー値が変化したときに、ちょうどOnItemSelectedListenerを実装し、イベントを処理するための適切なメソッドを追加します。

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
{
    // Get the currently selected State object from the spinner
    State st = (State)spinner.getSelectedItem();

    // Now do something with it.
} 

public void onNothingSelected(AdapterView<?> parent ) 
{ 
}

現在のページ全体のチュートリアルを見つけることができます。 http://www.katr.com/article_android_spinner01.phpする

他のヒント

またここにいてもラベルと価値をスピンナー-こう"とか"また戻ってしまった:

  1. でをスピンナーを通常の方法で
  2. 定義2等しいサイズの配列におarray.xml ファイルです。一のためのラベル、価値観
  3. の設定とスピンナー android:entries="@array/labels"
  4. にコードを必要とする場合、値は次のようにこないチェーン)

    String selectedVal = getResources().getStringArray(R.array.values)[spinner
                             .getSelectedItemPosition()];
    

  5. そして、これら2つの配列と一致しなければいけれど数スロット位置

中止、打ち切り!私は私になったのか分からないけどSpinnerandroid:entryValues属性をサポートしていません。その1は、同じようなこと(ポップアップダイアログ内の項目の表示リスト)を行いListPreferenceから実際にあります。私は(残念ながら)になります必要なものについては、

SpinnerAdapterを使用
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top